ls.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. #include <err.h>
  36. /*
  37. * ls - list files.
  38. * Usage: ls [-adlRs] [files]
  39. * -a Show files whose names begin with a dot.
  40. * -d Don't list contents of directories specified on the command line.
  41. * -l Long format listing.
  42. * -R Recurse into subdirectories found.
  43. * -s (with -l) Show block counts.
  44. */
  45. /* Flags for which options we're using. */
  46. static int aopt=0;
  47. static int dopt=0;
  48. static int lopt=0;
  49. static int Ropt=0;
  50. static int sopt=0;
  51. /* Process an option character. */
  52. static
  53. void
  54. option(int ch)
  55. {
  56. switch (ch) {
  57. case 'a': aopt=1; break;
  58. case 'd': dopt=1; break;
  59. case 'l': lopt=1; break;
  60. case 'R': Ropt=1; break;
  61. case 's': sopt=1; break;
  62. default:
  63. errx(1, "Unknown option -%c", ch);
  64. }
  65. }
  66. /*
  67. * Utility function to find the non-directory part of a pathname.
  68. */
  69. static
  70. const char *
  71. basename(const char *path)
  72. {
  73. const char *s;
  74. s = strrchr(path, '/');
  75. if (s) {
  76. return s+1;
  77. }
  78. return path;
  79. }
  80. /*
  81. * Utility function to check if a name refers to a directory.
  82. */
  83. static
  84. int
  85. isdir(const char *path)
  86. {
  87. struct stat buf;
  88. int fd;
  89. /* Assume stat() may not be implemented; use fstat */
  90. fd = open(path, O_RDONLY);
  91. if (fd<0) {
  92. err(1, "%s", path);
  93. }
  94. if (fstat(fd, &buf)<0) {
  95. err(1, "%s: fstat", path);
  96. }
  97. close(fd);
  98. return S_ISDIR(buf.st_mode);
  99. }
  100. /*
  101. * When listing one of several subdirectories, show the name of the
  102. * directory.
  103. */
  104. static
  105. void
  106. printheader(const char *file)
  107. {
  108. /* No blank line before the first header */
  109. static int first=1;
  110. if (first) {
  111. first = 0;
  112. }
  113. else {
  114. printf("\n");
  115. }
  116. printf("%s:\n", file);
  117. }
  118. /*
  119. * Show a single file.
  120. * We don't do the neat multicolumn listing that Unix ls does.
  121. */
  122. static
  123. void
  124. print(const char *path)
  125. {
  126. struct stat statbuf;
  127. const char *file;
  128. int typech;
  129. if (lopt || sopt) {
  130. int fd;
  131. fd = open(path, O_RDONLY);
  132. if (fd<0) {
  133. err(1, "%s", path);
  134. }
  135. if (fstat(fd, &statbuf)<0) {
  136. err(1, "%s: fstat", path);
  137. }
  138. close(fd);
  139. }
  140. file = basename(path);
  141. if (sopt) {
  142. printf("%3d ", statbuf.st_blocks);
  143. }
  144. if (lopt) {
  145. if (S_ISREG(statbuf.st_mode)) {
  146. typech = '-';
  147. }
  148. else if (S_ISDIR(statbuf.st_mode)) {
  149. typech = 'd';
  150. }
  151. else if (S_ISLNK(statbuf.st_mode)) {
  152. typech = 'l';
  153. }
  154. else if (S_ISCHR(statbuf.st_mode)) {
  155. typech = 'c';
  156. }
  157. else if (S_ISBLK(statbuf.st_mode)) {
  158. typech = 'b';
  159. }
  160. else {
  161. typech = '?';
  162. }
  163. printf("%crwx------ %2d root %-8llu",
  164. typech,
  165. statbuf.st_nlink,
  166. statbuf.st_size);
  167. }
  168. printf("%s\n", file);
  169. }
  170. /*
  171. * List a directory.
  172. */
  173. static
  174. void
  175. listdir(const char *path, int showheader)
  176. {
  177. int fd;
  178. char buf[1024];
  179. char newpath[1024];
  180. int len;
  181. if (showheader) {
  182. printheader(path);
  183. }
  184. /*
  185. * Open it.
  186. */
  187. fd = open(path, O_RDONLY);
  188. if (fd<0) {
  189. err(1, "%s", path);
  190. }
  191. /*
  192. * List the directory.
  193. */
  194. while ((len = getdirentry(fd, buf, sizeof(buf)-1)) > 0) {
  195. buf[len] = 0;
  196. /* Assemble the full name of the new item */
  197. snprintf(newpath, sizeof(newpath), "%s/%s", path, buf);
  198. if (aopt || buf[0]!='.') {
  199. /* Print it */
  200. print(newpath);
  201. }
  202. }
  203. if (len<0) {
  204. err(1, "%s: getdirentry", path);
  205. }
  206. /* Done */
  207. close(fd);
  208. }
  209. static
  210. void
  211. recursedir(const char *path)
  212. {
  213. int fd;
  214. char buf[1024];
  215. char newpath[1024];
  216. int len;
  217. /*
  218. * Open it.
  219. */
  220. fd = open(path, O_RDONLY);
  221. if (fd<0) {
  222. err(1, "%s", path);
  223. }
  224. /*
  225. * List the directory.
  226. */
  227. while ((len = getdirentry(fd, buf, sizeof(buf)-1)) > 0) {
  228. buf[len] = 0;
  229. /* Assemble the full name of the new item */
  230. snprintf(newpath, sizeof(newpath), "%s/%s", path, buf);
  231. if (!aopt && buf[0]=='.') {
  232. /* skip this one */
  233. continue;
  234. }
  235. if (!strcmp(buf, ".") || !strcmp(buf, "..")) {
  236. /* always skip these */
  237. continue;
  238. }
  239. if (!isdir(newpath)) {
  240. continue;
  241. }
  242. listdir(newpath, 1 /*showheader*/);
  243. if (Ropt) {
  244. recursedir(newpath);
  245. }
  246. }
  247. if (len<0) {
  248. err(1, "%s", path);
  249. }
  250. close(fd);
  251. }
  252. static
  253. void
  254. listitem(const char *path, int showheader)
  255. {
  256. if (!dopt && isdir(path)) {
  257. listdir(path, showheader || Ropt);
  258. if (Ropt) {
  259. recursedir(path);
  260. }
  261. }
  262. else {
  263. print(path);
  264. }
  265. }
  266. int
  267. main(int argc, char *argv[])
  268. {
  269. int i,j, items=0;
  270. /*
  271. * Go through the arguments and count how many non-option args.
  272. */
  273. for (i=1; i<argc; i++) {
  274. if (argv[i][0]!='-') {
  275. items++;
  276. }
  277. }
  278. /*
  279. * Now go through the options for real, processing them.
  280. */
  281. for (i=1; i<argc; i++) {
  282. if (argv[i][0]=='-') {
  283. /*
  284. * This word is an option.
  285. * Process all the option characters in it.
  286. */
  287. for (j=1; argv[i][j]; j++) {
  288. option(argv[i][j]);
  289. }
  290. }
  291. else {
  292. /*
  293. * This word isn't an option; list it.
  294. */
  295. listitem(argv[i], items>1);
  296. }
  297. }
  298. /*
  299. * If no filenames were specified to list, list the current
  300. * directory.
  301. */
  302. if (items==0) {
  303. listitem(".", 0);
  304. }
  305. return 0;
  306. }