Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <grp.h>
- #include <pwd.h>
- #define ec_neg(s,m) \
- if ( (s) < 0 ) {perror(m); enegflag = 0;}
- #define ec_null(s,m) \
- if ( (s) == NULL ) {perror(m); enulflag = 0;}
- int main(int argc, char *argv[]){
- int i, enegflag=1, enulflag = 1;
- struct stat fileStat;
- struct passwd * ppp;
- struct group * ggg;
- if(argc<2){
- printf("Syntax : mystat file1 ... file N\n");
- printf("Prints the attributes of the list of files on stdout\n");
- exit(-1);
- }
- for(i=1;i<argc;i++){
- printf("Information for %s\n",argv[i]);
- printf("---------------------------\n");
- ec_neg(stat(argv[i],&fileStat),"Couldn't read info for this file\n");
- if(enegflag){
- printf("File Size: \t\t%ld bytes\n",(long)fileStat.st_size);
- printf("Number of Links: \t%d\n",fileStat.st_nlink);
- printf("File inode: \t\t%ld\n",(long)fileStat.st_ino);
- ppp = getpwuid(fileStat.st_uid);
- ec_null(ppp,"Couldn't get the uid info");
- ggg = getgrgid(fileStat.st_gid);
- ec_null(ggg,"Couldn't get the gid info");
- if(enulflag){
- printf("User ID: \t\t%s\n",ppp->pw_name);
- printf("Group ID: \t\t%s\n",ggg->gr_name);
- }
- printf("Last modification: \t%s",(asctime(localtime(&fileStat.st_mtime))));
- printf("Host device: \t\t%ld\n",(long)fileStat.st_dev);
- printf("File Permissions: \t");
- printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
- printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
- printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
- printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
- printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
- printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
- printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
- printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
- printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
- printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
- printf("\n");
- printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
- }
- printf("\n\n");
- enegflag = 1;
- enulflag = 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement