Advertisement
DanFloyd

Mystat

May 11th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <time.h>
  9. #include <grp.h>
  10. #include <pwd.h>
  11.  
  12. #define ec_neg(s,m) \
  13. if ( (s) < 0 ) {perror(m); enegflag = 0;}
  14. #define ec_null(s,m) \
  15. if ( (s) == NULL ) {perror(m); enulflag = 0;}
  16.  
  17. int main(int argc, char *argv[]){
  18.  
  19.     int i, enegflag=1, enulflag = 1;
  20.     struct stat fileStat;
  21.     struct passwd * ppp;
  22.     struct group * ggg;
  23.    
  24.     if(argc<2){
  25.         printf("Syntax : mystat file1 ... file N\n");
  26.         printf("Prints the attributes of the list of files on stdout\n");
  27.         exit(-1);
  28.     }
  29.    
  30.     for(i=1;i<argc;i++){
  31.         printf("Information for %s\n",argv[i]);
  32.         printf("---------------------------\n");
  33.         ec_neg(stat(argv[i],&fileStat),"Couldn't read info for this file\n");
  34.         if(enegflag){
  35.             printf("File Size: \t\t%ld bytes\n",(long)fileStat.st_size);
  36.             printf("Number of Links: \t%d\n",fileStat.st_nlink);
  37.             printf("File inode: \t\t%ld\n",(long)fileStat.st_ino);
  38.             ppp = getpwuid(fileStat.st_uid);
  39.             ec_null(ppp,"Couldn't get the uid info");
  40.             ggg = getgrgid(fileStat.st_gid);
  41.             ec_null(ggg,"Couldn't get the gid info");
  42.             if(enulflag){
  43.                 printf("User ID: \t\t%s\n",ppp->pw_name);
  44.                 printf("Group ID: \t\t%s\n",ggg->gr_name);
  45.             }
  46.             printf("Last modification: \t%s",(asctime(localtime(&fileStat.st_mtime))));
  47.             printf("Host device: \t\t%ld\n",(long)fileStat.st_dev);
  48.             printf("File Permissions: \t");
  49.             printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
  50.             printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
  51.             printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
  52.             printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
  53.             printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
  54.             printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
  55.             printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
  56.             printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
  57.             printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
  58.             printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
  59.             printf("\n");
  60.             printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
  61.         }
  62.         printf("\n\n");
  63.         enegflag = 1;
  64.         enulflag = 1;
  65.     }
  66.    
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement