1
0

ls2.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* ls2.c
  2. * purpose list contents of directory or directories
  3. * action if no args, use . else list files in args
  4. * note uses stat and pwd.h and grp.h
  5. * BUG: try ls2 /tmp
  6. */
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <dirent.h>
  10. #include <sys/stat.h>
  11. void do_ls(char[]);
  12. void dostat(char *);
  13. void show_file_info( char *, struct stat *);
  14. void mode_to_letters( int , char [] );
  15. char *uid_to_name( uid_t );
  16. char *gid_to_name( gid_t );
  17. main(int ac, char *av[])
  18. {
  19. if ( ac == 1 )
  20. do_ls( "." );
  21. else
  22. while ( --ac ){
  23. printf("%s:\n", *++av );
  24. do_ls( *av );
  25. }
  26. }
  27. void do_ls( char dirname[] )
  28. /*
  29. * list files in directory called dirname
  30. */
  31. {
  32. DIR *dir_ptr; /* the directory */
  33. struct dirent *direntp; /* each entry */
  34. if ( ( dir_ptr = opendir( dirname ) ) == NULL )
  35. fprintf(stderr,"ls1: cannot open %s\n", dirname);
  36. else
  37. {
  38. while ( ( direntp = readdir( dir_ptr ) ) != NULL )
  39. dostat( direntp->d_name );
  40. closedir(dir_ptr);
  41. }
  42. }
  43. void dostat( char *filename )
  44. {
  45. struct stat info;
  46. if ( stat(filename, &info) == -1 ) /* cannot stat */
  47. perror( filename ); /* say why */
  48. else /* else show info */
  49. show_file_info( filename, &info );
  50. }
  51. void show_file_info( char *filename, struct stat *info_p )
  52. /*
  53. * display the info about 'filename'. The info is stored in struct at *info_p
  54. */
  55. {
  56. char *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
  57. void mode_to_letters();
  58. char modestr[11];
  59. mode_to_letters( info_p->st_mode, modestr );
  60. printf( "%s" , modestr );
  61. printf( "%4d " , (int) info_p->st_nlink);
  62. printf( "%-8s " , uid_to_name(info_p->st_uid) );
  63. printf( "%-8s " , gid_to_name(info_p->st_gid) );
  64. printf( "%8ld " , (long)info_p->st_size);
  65. printf( "%.12s ", 4+ctime(&info_p->st_mtime));
  66. printf( "%s\n" , filename );
  67. }
  68. /*
  69. * utility functions
  70. */
  71. /*
  72. * This function takes a mode value and a char array
  73. * and puts into the char array the file type and the
  74. * nine letters that correspond to the bits in mode.
  75. * NOTE: It does not code setuid, setgid, and sticky
  76. * codes
  77. */
  78. void mode_to_letters( int mode, char str[] )
  79. {
  80. strcpy( str, "----------" ); /* default=no perms */
  81. if ( S_ISDIR(mode) ) str[0] = 'd'; /* directory? */
  82. if ( S_ISCHR(mode) ) str[0] = 'c'; /* char devices */
  83. if ( S_ISBLK(mode) ) str[0] = 'b'; /* block device */
  84. if ( mode & S_IRUSR ) str[1] = 'r'; /* 3 bits for user */
  85. if ( mode & S_IWUSR ) str[2] = 'w';
  86. if ( mode & S_IXUSR ) str[3] = 'x';
  87. if ( mode & S_IRGRP ) str[4] = 'r'; /* 3 bits for group */
  88. if ( mode & S_IWGRP ) str[5] = 'w';
  89. if ( mode & S_IXGRP ) str[6] = 'x';
  90. if ( mode & S_IROTH ) str[7] = 'r'; /* 3 bits for other */
  91. if ( mode & S_IWOTH ) str[8] = 'w';
  92. if ( mode & S_IXOTH ) str[9] = 'x';
  93. }
  94. #include <pwd.h>
  95. char *uid_to_name( uid_t uid )
  96. /*
  97. * returns pointer to username associated with uid, uses getpw()
  98. */
  99. {
  100. struct passwd *getpwuid(), *pw_ptr;
  101. static char numstr[10];
  102. if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
  103. sprintf(numstr,"%d", uid);
  104. return numstr;
  105. }
  106. else
  107. return pw_ptr->pw_name ;
  108. }
  109. #include <grp.h>
  110. char *gid_to_name( gid_t gid )
  111. /*
  112. * returns pointer to group number gid. used getgrgid(3)
  113. */
  114. {
  115. struct group *getgrgid(), *grp_ptr;
  116. static char numstr[10];
  117. if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
  118. sprintf(numstr,"%d", gid);
  119. return numstr;
  120. }
  121. else
  122. return grp_ptr->gr_name;
  123. }