PROGRAM 2
/*********************************************************************/ /* program name: program2.c */ /* brief description: file information display */ /* student's name: anthony f. ortiz */ /* assignment number: 2 */ /* course name: cs3560 */ /* quarter: spring 97 */ /* date program completed: april 25, 1997 */ /* date program due: may 1, 1997 */ /* version of compiler/interpreter used: c compiler at csuh */ /*********************************************************************/ /*****************************************************************************/ /* this program displays information about a list of files (inode number, */ /* permissions, important times, owner and group number and name, and if */ /* it is a regular file, the first 5 bytes in decimal and ascii). */ /*****************************************************************************/ /* header files */ #include #include #include #include #include #include #include int main (int argc, char *argv[]) { int i, j, ch; struct stat buf; FILE * infile; char * access_time; char * mod_time; char * change_time; /* print an error message if the user only typed the program name. */ if (argc <= 1) { printf ("type in a file name next time. \n"); } /* print file information. */ for (i = 1; i < argc; i++) { /* print the filename. */ printf ("\n---------------------- %s ----------------------", argv [i]); if (lstat (argv [i], & buf) < 0) { printf ("\n\nlstat error"); printf ("\n\n-------------------------------------------------"); printf ("-----\n"); continue; } /* print the inode information. */ printf ("\n\ninnode information: %d", buf.st_ino); /* print the file type. */ printf ("\nfile type: "); if (S_ISREG (buf.st_mode)) printf ("regular"); else if (S_ISDIR (buf.st_mode)) printf ("directory"); else if (S_ISCHR (buf.st_mode)) printf ("character special"); else if (S_ISBLK (buf.st_mode)) printf ("block special"); else if (S_ISFIFO (buf.st_mode)) printf ("fifo"); #ifdef S_ISLNK else if (S_ISLNK (buf.st_mode)) printf ("symbolic link"); #endif #ifdef I_SSOCK else if (S_ISSOCK (buf.st_mode)) printf ("socket"); #endif else printf ("unknown"); /* print the permissions. */ printf ("\nuser permissions: "); if (S_IRUSR & buf.st_mode) printf ("read, "); else printf ("can't read, "); if (S_IWUSR & buf.st_mode) printf ("write, "); else printf ("can't write, "); if (S_IXUSR & buf.st_mode) printf ("execute, "); else printf ("can't execute"); printf ("\ngroup permissions: "); if (S_IRGRP & buf.st_mode) printf ("read, "); else printf ("can't read, "); if (S_IWGRP & buf.st_mode) printf ("write, "); else printf ("can't write, "); if (S_IXGRP & buf.st_mode) printf ("execute, "); else printf ("can't execute"); printf ("\nother permissions: "); if (S_IROTH & buf.st_mode) printf ("read, "); else printf ("can't read, "); if (S_IWOTH & buf.st_mode) printf ("write, "); else printf ("can't write, "); if (S_IXOTH & buf.st_mode) printf ("execute, "); else printf ("can't execute"); printf ("\nsticky bit: "); if (S_ISVTX & buf.st_mode) printf ("set "); else printf ("not set "); printf ("\nset-user-id bit: "); if (S_ISUID & buf.st_mode) printf ("set "); else printf ("not set "); printf ("\nset-group-id bit: "); if (S_ISGID & buf.st_mode) printf ("set "); else printf ("not set"); /* print time of last modification, last change of inode information, and last access. */ mod_time = ctime(& buf.st_mtime); mod_time [24] = '\0'; printf ("\ntime of last modification: %s", mod_time); change_time = ctime (& buf.st_ctime); change_time [24] = '\0'; printf ("\ntime of last change of inode information: %s", change_time); access_time = ctime (& buf.st_atime); access_time [24] = '\0'; printf ("\ntime of last access: %s", access_time); /* print owner and group id number and name. */ printf ("\nowner id number: %d", buf.st_uid ); printf ("\nowner id name: %s", getpwuid (buf.st_uid) -> pw_name); printf ("\ngroud id number: %d", buf.st_gid); printf ("\ngroup id name: %s", getgrgid (buf.st_gid) -> gr_name); /* print first 5 bytes of a regular file in ascii and decimal. */ if (S_ISREG (buf.st_mode)) { printf ("\nread the first 5 bytes (ascii): "); infile = fopen (argv[i], "r"); if (infile == NULL) { printf ("can't open file"); } else { for (j = 0; j < 5; j++) { ch = fgetc (infile); if (ch >= 65 && ch <= 122) { printf (" %c ", ch); } else { printf (" /0 "); } } } printf ("\nread the first 5 bytes (decimal): "); rewind (infile); if (infile == NULL) { printf ("can't open file"); } else { for (j = 0; j < 5; j++) { ch = fgetc (infile); printf (" %d ", ch); } } fclose (infile); } printf ("\n\n------------------------------------------------------\n"); } return 0; } /* outfile: prog2.out */ ---------------------- cutcol ---------------------- innode information: 861876 file type: regular user permissions: read, write, execute, group permissions: can't read, can't write, can't execute other permissions: can't read, can't write, can't execute sticky bit: not set set-user-id bit: not set set-group-id bit: not set time of last modification: Fri Mar 12 12:47:32 1999 time of last change of inode information: Fri Mar 12 12:47:55 1999 time of last access: Fri Mar 12 12:49:01 1999 owner id number: 12650 owner id name: cs218-01 groud id number: 200 group id name: student read the first 5 bytes (ascii): /0 E L F /0 read the first 5 bytes (decimal): 127 69 76 70 1 ------------------------------------------------------
BACK TO CS3560 PAGE.