#include #include #include #include #include /* * stat2.c based on stat1.c but prints more stuff. * Chose one of two formats. * * note: need endpwent to close the database and free memory */ // #define TAGGED_STYLE #define LS_STYLE #define TEXTDATE #ifndef DATE_FMT #ifdef TEXTDATE #define DATE_FMT "%b %e %H:%M" /* text format */ #else #define DATE_FMT "%Y-%m-%d %H:%M" /* the default */ #endif #endif void dostat(char *); void show_file_info(char *, struct stat *); char *fmt_time(time_t); int main( int ac, char **av ) { while ( --ac ) dostat( *++av ); return 0; } void dostat( char *filename ) { struct stat info; if ( stat(filename, &info) == -1 ) /* cannot stat */ perror( filename ); /* say why */ else /* else show info */ show_file_info( filename, &info ); } void show_file_info( char *filename, struct stat *info_p ) /* * display the info about 'filename'. The info is stored in struct at *info_p */ { char *uid_to_name(), *ctime(), *gid_to_name(), *mode_to_letters(), m[12]; #ifdef TAGGED_STYLE printf("%s:\n", filename ); /* print name */ printf("\t mode: %s\n", mode_to_letters(info_p->st_mode,m) ); printf("\t links: %d\n", (int) info_p->st_nlink); /* links */ printf("\t owner: %s\n", uid_to_name(info_p->st_uid) ); printf("\t group: %s\n", gid_to_name(info_p->st_gid) ); printf("\t size: %ld\n",(long)info_p->st_size); /* size */ printf("\t mod: %s", ctime(&info_p->st_mtime)); printf("\taccess: %s", ctime(&info_p->st_atime)); #endif #ifdef LS_STYLE printf( "%s " , mode_to_letters(info_p->st_mode,m) ); printf( "%d " , (int) info_p->st_nlink); printf( "%s " , uid_to_name(info_p->st_uid) ); printf( "%s " , gid_to_name(info_p->st_gid) ); printf( "%ld " , (long)info_p->st_size); printf( "%s " , fmt_time( info_p->st_mtime ) ); printf( "%s\n" , filename ); #endif } /* * utility functions */ #define MAXDATELEN 100 char * fmt_time( time_t timeval ) /* * formats time for human consumption. * Uses localtime to convert the timeval into a struct of elements * (see localtime(3)) and uses strftime to format the data */ { static char result[MAXDATELEN]; struct tm *tp = localtime(&timeval); /* convert time */ strftime(result, MAXDATELEN, DATE_FMT, tp); /* format it */ return result; } /* * This function takes a mode value and a char array * and puts into the char array the file type and the * nine letters that correspond to the bits in mode. * NOTE: It does not code setuid, setgid, and sticky * codes */ char *mode_to_letters( int mode, char str[] ) { strcpy( str, "----------" ); /* default=no perms */ /* 123456789 positions in string */ if ( S_ISDIR(mode) ) str[0] = 'd'; /* directory? */ if ( S_ISCHR(mode) ) str[0] = 'c'; /* char devices */ if ( S_ISBLK(mode) ) str[0] = 'b'; /* block device */ if ( S_ISFIFO(mode) ) str[0] = 'p'; /* FIFO device */ /* if none of the above, leave as '-' */ if ( mode & S_IRUSR ) str[1] = 'r'; /* 3 bits for user */ if ( mode & S_IWUSR ) str[2] = 'w'; if ( mode & S_IXUSR ) str[3] = 'x'; if ( mode & S_IRGRP ) str[4] = 'r'; /* 3 bits for group */ if ( mode & S_IWGRP ) str[5] = 'w'; if ( mode & S_IXGRP ) str[6] = 'x'; if ( mode & S_IROTH ) str[7] = 'r'; /* 3 bits for other */ if ( mode & S_IWOTH ) str[8] = 'w'; if ( mode & S_IXOTH ) str[9] = 'x'; return str; } #include char *uid_to_name( uid_t uid ) /* * returns pointer to username associated with uid, uses getpw() */ { struct passwd *pw_ptr; static char numstr[100]; if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){ sprintf(numstr,"%d", uid); return numstr; } else return pw_ptr->pw_name ; } #include char *gid_to_name( gid_t gid ) /* * returns pointer to group number gid. used getgrgid(3) */ { struct group *grp_ptr; static char numstr[100]; if ( ( grp_ptr = getgrgid(gid) ) == NULL ){ sprintf(numstr,"%d", gid); return numstr; } else return grp_ptr->gr_name; }