ls.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K. Lange
  5. *
  6. * ls
  7. *
  8. * Lists files in a directory, with nice color
  9. * output like any modern ls should have.
  10. */
  11. #include <fcntl.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <dirent.h>
  18. #include <termios.h>
  19. #include <time.h>
  20. #include <pwd.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #define TRACE_APP_NAME "ls"
  25. //#include "lib/trace.h"
  26. #define TRACE(...)
  27. #include <toaru/list.h>
  28. #define MIN_COL_SPACING 2
  29. #define EXE_COLOR "1;32"
  30. #define DIR_COLOR "1;34"
  31. #define SYMLINK_COLOR "1;36"
  32. #define REG_COLOR "0"
  33. #define MEDIA_COLOR ""
  34. #define SYM_COLOR ""
  35. #define BROKEN_COLOR "1;"
  36. #define DEVICE_COLOR "1;33;40"
  37. #define SETUID_COLOR "37;41"
  38. #define DEFAULT_TERM_WIDTH 0
  39. #define DEFAULT_TERM_HEIGHT 0
  40. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  41. #define LINE_LEN 4096
  42. static int human_readable = 0;
  43. static int stdout_is_tty = 1;
  44. static int this_year = 0;
  45. static int show_hidden = 0;
  46. static int long_mode = 0;
  47. static int print_dir = 0;
  48. static int term_width = DEFAULT_TERM_WIDTH;
  49. static int term_height = DEFAULT_TERM_HEIGHT;
  50. struct tfile {
  51. char * name;
  52. struct stat statbuf;
  53. char * link;
  54. struct stat statbufl;
  55. };
  56. static const char * color_str(struct stat * sb) {
  57. if (S_ISDIR(sb->st_mode)) {
  58. /* Directory */
  59. return DIR_COLOR;
  60. } else if (S_ISLNK(sb->st_mode)) {
  61. /* Symbolic Link */
  62. return SYMLINK_COLOR;
  63. } else if (sb->st_mode & S_ISUID) {
  64. /* setuid - sudo, etc. */
  65. return SETUID_COLOR;
  66. } else if (sb->st_mode & 0111) {
  67. /* Executable */
  68. return EXE_COLOR;
  69. } else if (S_ISBLK(sb->st_mode) || S_ISCHR(sb->st_mode)) {
  70. /* Device file */
  71. return DEVICE_COLOR;
  72. } else {
  73. /* Regular file */
  74. return REG_COLOR;
  75. }
  76. }
  77. static int filecmp(const void * c1, const void * c2) {
  78. const struct tfile * d1 = *(const struct tfile **)c1;
  79. const struct tfile * d2 = *(const struct tfile **)c2;
  80. int a = S_ISDIR(d1->statbuf.st_mode);
  81. int b = S_ISDIR(d2->statbuf.st_mode);
  82. if (a == b) return strcmp(d1->name, d2->name);
  83. else if (a < b) return -1;
  84. else if (a > b) return 1;
  85. return 0; /* impossible ? */
  86. }
  87. static int filecmp_notypesort(const void * c1, const void * c2) {
  88. const struct tfile * d1 = *(const struct tfile **)c1;
  89. const struct tfile * d2 = *(const struct tfile **)c2;
  90. return strcmp(d1->name, d2->name);
  91. }
  92. static void print_entry(struct tfile * file, int colwidth) {
  93. const char * ansi_color_str = color_str(&file->statbuf);
  94. /* Print the file name */
  95. if (stdout_is_tty) {
  96. printf("\033[%sm%s\033[0m", ansi_color_str, file->name);
  97. } else {
  98. printf("%s", file->name);
  99. }
  100. /* Pad the rest of the column */
  101. for (int rem = colwidth - strlen(file->name); rem > 0; rem--) {
  102. printf(" ");
  103. }
  104. }
  105. static int print_username(char * _out, int uid) {
  106. TRACE("getpwuid");
  107. struct passwd * p = getpwuid(uid);
  108. int out = 0;
  109. if (p) {
  110. TRACE("p is set");
  111. out = sprintf(_out, "%s", p->pw_name);
  112. } else {
  113. TRACE("p is not set");
  114. out = sprintf(_out, "%d", uid);
  115. }
  116. endpwent();
  117. return out;
  118. }
  119. static int print_human_readable_size(char * _out, size_t s) {
  120. if (s >= 1<<20) {
  121. size_t t = s / (1 << 20);
  122. return sprintf(_out, "%d.%1dM", (int)t, (int)(s - t * (1 << 20)) / ((1 << 20) / 10));
  123. } else if (s >= 1<<10) {
  124. size_t t = s / (1 << 10);
  125. return sprintf(_out, "%d.%1dK", (int)t, (int)(s - t * (1 << 10)) / ((1 << 10) / 10));
  126. } else {
  127. return sprintf(_out, "%d", (int)s);
  128. }
  129. }
  130. static void update_column_widths(int * widths, struct tfile * file) {
  131. char tmp[256];
  132. int n;
  133. /* Links */
  134. TRACE("links");
  135. n = sprintf(tmp, "%d", file->statbuf.st_nlink);
  136. if (n > widths[0]) widths[0] = n;
  137. /* User */
  138. TRACE("user");
  139. n = print_username(tmp, file->statbuf.st_uid);
  140. if (n > widths[1]) widths[1] = n;
  141. /* Group */
  142. TRACE("group");
  143. n = print_username(tmp, file->statbuf.st_gid);
  144. if (n > widths[2]) widths[2] = n;
  145. /* File size */
  146. TRACE("file size");
  147. if (human_readable) {
  148. n = print_human_readable_size(tmp, file->statbuf.st_size);
  149. } else {
  150. n = sprintf(tmp, "%d", (int)file->statbuf.st_size);
  151. }
  152. if (n > widths[3]) widths[3] = n;
  153. }
  154. static void print_entry_long(int * widths, struct tfile * file) {
  155. const char * ansi_color_str = color_str(&file->statbuf);
  156. /* file permissions */
  157. if (S_ISLNK(file->statbuf.st_mode)) { printf("l"); }
  158. else if (S_ISCHR(file->statbuf.st_mode)) { printf("c"); }
  159. else if (S_ISBLK(file->statbuf.st_mode)) { printf("b"); }
  160. else if (S_ISDIR(file->statbuf.st_mode)) { printf("d"); }
  161. else { printf("-"); }
  162. printf( (file->statbuf.st_mode & S_IRUSR) ? "r" : "-");
  163. printf( (file->statbuf.st_mode & S_IWUSR) ? "w" : "-");
  164. if (file->statbuf.st_mode & S_ISUID) {
  165. printf("s");
  166. } else {
  167. printf( (file->statbuf.st_mode & S_IXUSR) ? "x" : "-");
  168. }
  169. printf( (file->statbuf.st_mode & S_IRGRP) ? "r" : "-");
  170. printf( (file->statbuf.st_mode & S_IWGRP) ? "w" : "-");
  171. printf( (file->statbuf.st_mode & S_IXGRP) ? "x" : "-");
  172. printf( (file->statbuf.st_mode & S_IROTH) ? "r" : "-");
  173. printf( (file->statbuf.st_mode & S_IWOTH) ? "w" : "-");
  174. printf( (file->statbuf.st_mode & S_IXOTH) ? "x" : "-");
  175. printf( " %*d ", widths[0], file->statbuf.st_nlink); /* number of links, not supported */
  176. char tmp[100];
  177. print_username(tmp, file->statbuf.st_uid);
  178. printf("%-*s ", widths[1], tmp);
  179. print_username(tmp, file->statbuf.st_gid);
  180. printf("%-*s ", widths[2], tmp);
  181. if (human_readable) {
  182. print_human_readable_size(tmp, file->statbuf.st_size);
  183. printf("%*s ", widths[3], tmp);
  184. } else {
  185. printf("%*d ", widths[3], (int)file->statbuf.st_size);
  186. }
  187. char time_buf[80];
  188. struct tm * timeinfo = localtime((time_t*)&file->statbuf.st_mtime);
  189. if (timeinfo->tm_year == this_year) {
  190. strftime(time_buf, 80, "%b %d %H:%M", timeinfo);
  191. } else {
  192. strftime(time_buf, 80, "%b %d %Y", timeinfo);
  193. }
  194. printf("%s ", time_buf);
  195. /* Print the file name */
  196. if (stdout_is_tty) {
  197. printf("\033[%sm%s\033[0m", ansi_color_str, file->name);
  198. if (S_ISLNK(file->statbuf.st_mode)) {
  199. const char * s = color_str(&file->statbufl);
  200. printf(" -> \033[%sm%s\033[0m", s, file->link);
  201. }
  202. } else {
  203. printf("%s", file->name);
  204. if (S_ISLNK(file->statbuf.st_mode)) {
  205. printf(" -> %s", file->link);
  206. }
  207. }
  208. printf("\n");
  209. }
  210. static void show_usage(int argc, char * argv[]) {
  211. printf(
  212. "ls - list files\n"
  213. "\n"
  214. "usage: %s [-lha] [path]\n"
  215. "\n"
  216. " -a \033[3mlist all files (including . files)\033[0m\n"
  217. " -l \033[3muse a long listing format\033[0m\n"
  218. " -h \033[3mhuman-readable file sizes\033[0m\n"
  219. " -? \033[3mshow this help text\033[0m\n"
  220. "\n", argv[0]);
  221. }
  222. static void display_tfiles(struct tfile ** ents_array, int numents) {
  223. if (long_mode) {
  224. TRACE("long mode display, column lengths");
  225. int widths[4] = {0,0,0,0};
  226. for (int i = 0; i < numents; i++) {
  227. update_column_widths(widths, ents_array[i]);
  228. }
  229. TRACE("actual printing");
  230. for (int i = 0; i < numents; i++) {
  231. print_entry_long(widths, ents_array[i]);
  232. }
  233. } else {
  234. /* Determine the gridding dimensions */
  235. int ent_max_len = 0;
  236. for (int i = 0; i < numents; i++) {
  237. ent_max_len = MAX(ent_max_len, (int)strlen(ents_array[i]->name));
  238. }
  239. int col_ext = ent_max_len + MIN_COL_SPACING;
  240. int cols = ((term_width - ent_max_len) / col_ext) + 1;
  241. /* Print the entries */
  242. for (int i = 0; i < numents;) {
  243. /* Columns */
  244. print_entry(ents_array[i++], ent_max_len);
  245. for (int j = 0; (i < numents) && (j < (cols-1)); j++) {
  246. printf(" ");
  247. print_entry(ents_array[i++], ent_max_len);
  248. }
  249. printf("\n");
  250. }
  251. }
  252. }
  253. static int display_dir(char * p) {
  254. /* Open the directory */
  255. DIR * dirp = opendir(p);
  256. if (dirp == NULL) {
  257. return 2;
  258. }
  259. if (print_dir) {
  260. printf("%s:\n", p);
  261. }
  262. /* Read the entries in the directory */
  263. list_t * ents_list = list_create();
  264. TRACE("reading entries");
  265. struct dirent * ent = readdir(dirp);
  266. while (ent != NULL) {
  267. if (show_hidden || (ent->d_name[0] != '.')) {
  268. struct tfile * f = malloc(sizeof(struct tfile));
  269. f->name = strdup(ent->d_name);
  270. char tmp[strlen(p)+strlen(ent->d_name)+2];
  271. sprintf(tmp, "%s/%s", p, ent->d_name);
  272. lstat(tmp, &f->statbuf);
  273. if (S_ISLNK(f->statbuf.st_mode)) {
  274. stat(tmp, &f->statbufl);
  275. f->link = malloc(4096);
  276. readlink(tmp, f->link, 4096);
  277. }
  278. list_insert(ents_list, (void *)f);
  279. }
  280. ent = readdir(dirp);
  281. }
  282. closedir(dirp);
  283. TRACE("copying");
  284. /* Now, copy those entries into an array (for sorting) */
  285. if (!ents_list->length) return 0;
  286. struct tfile ** file_arr = malloc(sizeof(struct tfile *) * ents_list->length);
  287. int index = 0;
  288. foreach(node, ents_list) {
  289. file_arr[index++] = (struct tfile *)node->value;
  290. }
  291. list_free(ents_list);
  292. TRACE("sorting");
  293. qsort(file_arr, index, sizeof(struct tfile *), filecmp_notypesort);
  294. TRACE("displaying");
  295. display_tfiles(file_arr, index);
  296. free(file_arr);
  297. return 0;
  298. }
  299. int main (int argc, char * argv[]) {
  300. /* Parse arguments */
  301. char * p = ".";
  302. if (argc > 1) {
  303. int c;
  304. while ((c = getopt(argc, argv, "ahl?")) != -1) {
  305. switch (c) {
  306. case 'a':
  307. show_hidden = 1;
  308. break;
  309. case 'h':
  310. human_readable = 1;
  311. break;
  312. case 'l':
  313. long_mode = 1;
  314. break;
  315. case '?':
  316. show_usage(argc, argv);
  317. return 0;
  318. }
  319. }
  320. if (optind < argc) {
  321. p = argv[optind];
  322. }
  323. if (optind + 1 < argc) {
  324. print_dir = 1;
  325. }
  326. }
  327. stdout_is_tty = isatty(STDOUT_FILENO);
  328. if (long_mode) {
  329. struct tm * timeinfo;
  330. struct timeval now;
  331. gettimeofday(&now, NULL); //time(NULL);
  332. timeinfo = localtime((time_t *)&now.tv_sec);
  333. this_year = timeinfo->tm_year;
  334. }
  335. if (stdout_is_tty) {
  336. TRACE("getting display size");
  337. struct winsize w;
  338. ioctl(1, TIOCGWINSZ, &w);
  339. term_width = w.ws_col;
  340. term_height = w.ws_row;
  341. term_width -= 1; /* And this just helps clean up our math */
  342. }
  343. int out = 0;
  344. if (argc == 1 || optind == argc) {
  345. TRACE("no file to look up");
  346. display_dir(p);
  347. } else {
  348. list_t * files = list_create();
  349. while (p) {
  350. struct tfile * f = malloc(sizeof(struct tfile));
  351. f->name = p;
  352. int t = stat(p, &f->statbuf);
  353. if (t < 0) {
  354. printf("ls: cannot access %s: No such file or directory\n", p);
  355. free(f);
  356. out = 2;
  357. } else {
  358. list_insert(files, f);
  359. }
  360. optind++;
  361. if (optind >= argc) p = NULL;
  362. else p = argv[optind];
  363. }
  364. if (!files->length) {
  365. /* No valid entries */
  366. return out;
  367. }
  368. struct tfile ** file_arr = malloc(sizeof(struct tfile *) * files->length);
  369. int index = 0;
  370. foreach(node, files) {
  371. file_arr[index++] = (struct tfile *)node->value;
  372. }
  373. list_free(files);
  374. qsort(file_arr, index, sizeof(struct tfile *), filecmp);
  375. int first_directory = index;
  376. for (int i = 0; i < index; ++i) {
  377. if (S_ISDIR(file_arr[i]->statbuf.st_mode)) {
  378. first_directory = i;
  379. break;
  380. }
  381. }
  382. if (first_directory) {
  383. display_tfiles(file_arr, first_directory);
  384. }
  385. for (int i = first_directory; i < index; ++i) {
  386. if (i != 0) {
  387. printf("\n");
  388. }
  389. display_dir(file_arr[i]->name);
  390. }
  391. }
  392. return out;
  393. }
  394. /*
  395. * vim: tabstop=4
  396. * vim: shiftwidth=4
  397. * vim: noexpandtab
  398. */