ls.c 11 KB

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