ls.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (c) 1993 by David I. Bell
  3. * Permission is granted to use, distribute, or modify this source,
  4. * provided that this copyright notice remains intact.
  5. *
  6. * The "ls" built-in command.
  7. */
  8. #include "sash.h"
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/sysmacros.h>
  12. #include <dirent.h>
  13. #include <pwd.h>
  14. #include <grp.h>
  15. #include <time.h>
  16. #define LISTSIZE 256
  17. #define COLWIDTH 20
  18. #ifdef S_ISLNK
  19. #define LSTAT lstat
  20. #else
  21. #define LSTAT stat
  22. #endif
  23. /*
  24. * Flags for the LS command.
  25. */
  26. #define LSF_LONG 0x01
  27. #define LSF_DIR 0x02
  28. #define LSF_INODE 0x04
  29. #define LSF_MULT 0x08
  30. #define LSF_ALL 0x10
  31. #define LSF_COMPACT 0x20
  32. static char **list;
  33. static int listsize;
  34. static int listused;
  35. static int linelen = 0;
  36. static void lsfile();
  37. void
  38. do_ls(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42. char *cp;
  43. char *name;
  44. int flags;
  45. int i;
  46. DIR *dirp;
  47. BOOL endslash;
  48. char **newlist;
  49. struct dirent *dp;
  50. char fullname[PATHLEN];
  51. struct stat statbuf;
  52. static char *def[2];
  53. if (listsize == 0) {
  54. list = (char **) malloc(LISTSIZE * sizeof(char *));
  55. if (list == NULL) {
  56. fprintf(stderr, "No memory for ls buffer\n");
  57. return;
  58. }
  59. listsize = LISTSIZE;
  60. }
  61. listused = 0;
  62. flags = 0;
  63. if ((argc > 1) && (argv[1][0] == '-'))
  64. {
  65. argc--;
  66. cp = *(++argv) + 1;
  67. while (*cp) switch (*cp++) {
  68. case 'l': flags |= LSF_LONG; break;
  69. case 'd': flags |= LSF_DIR; break;
  70. case 'i': flags |= LSF_INODE; break;
  71. case 'a': flags |= LSF_ALL; break;
  72. case 'C': flags |= LSF_COMPACT; break;
  73. default:
  74. fprintf(stderr, "Unknown option -%c\n", cp[-1]);
  75. return;
  76. }
  77. }
  78. if ((flags & LSF_COMPACT) && (flags & ~LSF_COMPACT)) {
  79. fprintf(stderr, "Cannot do compact list with other options\n");
  80. return;
  81. }
  82. if (argc <= 1) {
  83. argc = 2;
  84. argv = def;
  85. argv[0] = "ls";
  86. argv[1] = ".";
  87. }
  88. if (argc > 2)
  89. flags |= LSF_MULT;
  90. while (argc-- > 1) {
  91. name = *(++argv);
  92. endslash = (*name && (name[strlen(name) - 1] == '/'));
  93. if (LSTAT(name, &statbuf) < 0) {
  94. perror(name);
  95. continue;
  96. }
  97. if ((flags & LSF_DIR) || (!S_ISDIR(statbuf.st_mode))) {
  98. lsfile(name, &statbuf, flags);
  99. continue;
  100. }
  101. /*
  102. * Do all the files in a directory.
  103. */
  104. dirp = opendir(name);
  105. if (dirp == NULL) {
  106. perror(name);
  107. continue;
  108. }
  109. if (flags & LSF_MULT)
  110. printf("\n%s:\n", name);
  111. while ((dp = readdir(dirp)) != NULL) {
  112. if ((dp->d_name[0] == '.') && !(flags & LSF_ALL))
  113. continue;
  114. fullname[0] = '\0';
  115. if ((*name != '.') || (name[1] != '\0')) {
  116. strcpy(fullname, name);
  117. if (!endslash)
  118. strcat(fullname, "/");
  119. }
  120. strcat(fullname, dp->d_name);
  121. if (listused >= listsize) {
  122. newlist = malloc((sizeof(char **)) * (listsize + LISTSIZE));
  123. if (newlist == NULL) {
  124. fprintf(stderr, "No memory for ls buffer\n");
  125. break;
  126. }
  127. memcpy(newlist, list, sizeof(char**) * listsize);
  128. free(list);
  129. listsize += LISTSIZE;
  130. }
  131. list[listused] = strdup(fullname);
  132. if (list[listused] == NULL) {
  133. fprintf(stderr, "No memory for filenames\n");
  134. break;
  135. }
  136. listused++;
  137. }
  138. closedir(dirp);
  139. /*
  140. * Sort the files.
  141. */
  142. qsort((char *) list, listused, sizeof(char *), namesort);
  143. /*
  144. * Now finally list the filenames.
  145. */
  146. for (i = 0; i < listused; i++) {
  147. name = list[i];
  148. if (LSTAT(name, &statbuf) < 0) {
  149. perror(name);
  150. free(name);
  151. continue;
  152. }
  153. cp = strrchr(name, '/');
  154. if (cp)
  155. cp++;
  156. else
  157. cp = name;
  158. lsfile(cp, &statbuf, flags);
  159. free(name);
  160. }
  161. listused = 0;
  162. }
  163. if (linelen)
  164. fputc('\n', stdout);
  165. }
  166. /*
  167. * Do an LS of a particular file name according to the flags.
  168. */
  169. static void
  170. lsfile(name, statbuf, flags)
  171. char *name;
  172. struct stat *statbuf;
  173. {
  174. char *cp;
  175. struct passwd *pwd;
  176. struct group *grp;
  177. int len;
  178. char buf[PATHLEN];
  179. static char username[12];
  180. static int userid;
  181. static BOOL useridknown;
  182. static char groupname[12];
  183. static int groupid;
  184. static BOOL groupidknown;
  185. cp = buf;
  186. *cp = '\0';
  187. if (flags & LSF_INODE) {
  188. sprintf(cp, "%5d ", statbuf->st_ino);
  189. cp += strlen(cp);
  190. }
  191. if (flags & LSF_LONG) {
  192. strcpy(cp, modestring(statbuf->st_mode));
  193. cp += strlen(cp);
  194. sprintf(cp, "%3d ", statbuf->st_nlink);
  195. cp += strlen(cp);
  196. if (!useridknown || (statbuf->st_uid != userid)) {
  197. /*pwd = getpwuid(statbuf->st_uid);
  198. if (pwd)
  199. strcpy(username, pwd->pw_name);
  200. else*/
  201. sprintf(username, "%d", statbuf->st_uid);
  202. userid = statbuf->st_uid;
  203. useridknown = TRUE;
  204. }
  205. sprintf(cp, "%-8s ", username);
  206. cp += strlen(cp);
  207. if (!groupidknown || (statbuf->st_gid != groupid)) {
  208. /*grp = getgrgid(statbuf->st_gid);
  209. if (grp)
  210. strcpy(groupname, grp->gr_name);
  211. else*/
  212. sprintf(groupname, "%d", statbuf->st_gid);
  213. groupid = statbuf->st_gid;
  214. groupidknown = TRUE;
  215. }
  216. sprintf(cp, "%-8s ", groupname);
  217. cp += strlen(cp);
  218. if (S_ISBLK(statbuf->st_mode) || S_ISCHR(statbuf->st_mode))
  219. sprintf(cp, "%3d, %3d ", major(statbuf->st_rdev),
  220. minor(statbuf->st_rdev));
  221. else
  222. sprintf(cp, "%8d ", statbuf->st_size);
  223. cp += strlen(cp);
  224. sprintf(cp, " %-12s ", timestring(statbuf->st_mtime));
  225. }
  226. fputs(buf, stdout);
  227. fputs(name, stdout);
  228. #ifdef S_ISLNK
  229. if ((flags & LSF_LONG) && S_ISLNK(statbuf->st_mode)) {
  230. len = readlink(name, buf, PATHLEN - 1);
  231. if (len >= 0) {
  232. buf[len] = '\0';
  233. printf(" -> %s", buf);
  234. }
  235. }
  236. #endif
  237. if (flags & LSF_COMPACT) {
  238. len = strlen(name);
  239. if (len < COLWIDTH) {
  240. for (; (len < COLWIDTH); len++)
  241. fputc(' ', stdout);
  242. linelen += COLWIDTH;
  243. } else {
  244. linelen = 80;
  245. }
  246. if (linelen >= 80) {
  247. fputc('\n', stdout);
  248. linelen = 0;
  249. }
  250. } else {
  251. fputc('\n', stdout);
  252. }
  253. }
  254. /* END CODE */