22 September, 2010

C Program to check the filetype in Unix

Here is a C program that can be used to check the filetype of a specified file... It can check and output weather the file is regular/device/block/fifo file... 


  #include<stdio.h>                                 
  #include<string.h>                                             
  #include<sys/stat.h>                                              
  #include<sys/types.h>
                                                                                   
  int main ()
 {   
    char name[100];
    char temp[100];
    struct stat p;
    printf("\nEnter filename :  ");
    scanf("%s",name);
    stat(name,&p);    
    if(S_ISREG(p.st_mode))
        printf("\nregular file");
    else if(S_ISDIR(p.st_mode))
        printf("\ndirectory file");
        else if(S_ISLNK(p.st_mode))
        printf("\nlink file");
        else if(S_ISFIFO(p.st_mode))
        printf("\nfifo file");
        else if(S_ISCHR(p.st_mode))
        printf("\ncharacter file");
        else if(S_ISBLK(p.st_mode))
        printf("\nblock file");
    else
        printf("\nUnknown type");
    printf("\n\n");
   }                                        

0 comments:

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger