Changes in uspace/app/bdsh/cmds/modules/ls/ls.c [c878693:59cce22] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/ls/ls.c
rc878693 r59cce22 42 42 #include <vfs/vfs.h> 43 43 #include <str.h> 44 #include <cap.h>45 44 46 45 #include "ls.h" … … 59 58 { "unsort", no_argument, 0, 'u' }, 60 59 { "recursive", no_argument, 0, 'r' }, 61 { "exact-size", no_argument, 0, 'e' },62 { "single-column", no_argument, 0, '1' },63 60 { 0, 0, 0, 0 } 64 61 }; … … 66 63 /* Prototypes for the ls command, excluding entry points. */ 67 64 static unsigned int ls_start(ls_job_t *); 68 static errno_t ls_print(struct dir_elem_t *); 69 static errno_t ls_print_single_column(struct dir_elem_t *); 70 static int ls_cmp_type_name(const void *, const void *); 71 static int ls_cmp_name(const void *, const void *); 65 static void ls_print(struct dir_elem_t *); 66 static int ls_cmp(const void *, const void *); 72 67 static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **); 73 68 static unsigned int ls_recursive(const char *, DIR *); … … 79 74 ls->sort = 1; 80 75 81 ls->exact_size = false;82 ls->single_column = false;83 ls->printer = ls_print;84 76 return 1; 85 77 } … … 96 88 * @param de Directory element. 97 89 */ 98 static errno_t ls_print(struct dir_elem_t *de) 99 { 100 int width = 13; 101 102 if (de->s.is_file) { 103 if (ls.exact_size) { 104 printf("%-40s\t%*llu\n", de->name, width, (long long) de->s.size); 105 return EOK; 106 } 107 108 cap_spec_t cap; 109 cap_from_blocks(de->s.size, 1, &cap); 110 cap_simplify(&cap); 111 112 char *rptr; 113 errno_t rc = cap_format(&cap, &rptr); 114 if (rc != EOK) { 115 return rc; 116 } 117 118 char *sep = str_rchr(rptr, ' '); 119 if (sep == NULL) { 120 free(rptr); 121 return ENOENT; 122 } 123 124 *sep = '\0'; 125 126 printf("%-40s\t%*s %2s\n", de->name, width - 3, rptr, sep + 1); 127 free(rptr); 128 } else if (de->s.is_directory) 129 printf("%-40s\t%*s\n", de->name, width, "<dir>"); 90 static void ls_print(struct dir_elem_t *de) 91 { 92 if (de->s.is_file) 93 printf("%-40s\t%llu\n", de->name, (long long) de->s.size); 94 else if (de->s.is_directory) 95 printf("%-40s\t<dir>\n", de->name); 130 96 else 131 97 printf("%-40s\n", de->name); 132 133 return EOK;134 }135 136 static errno_t ls_print_single_column(struct dir_elem_t *de)137 {138 if (de->s.is_file) {139 printf("%s\n", de->name);140 } else {141 printf("%s/\n", de->name);142 }143 144 return EOK;145 98 } 146 99 … … 156 109 * @return -1 if a < b, 1 otherwise. 157 110 */ 158 static int ls_cmp _type_name(const void *a, const void *b)111 static int ls_cmp(const void *a, const void *b) 159 112 { 160 113 struct dir_elem_t const *da = a; … … 167 120 else 168 121 return 1; 169 }170 171 /** Compare directories/files per name172 *173 * This comparision ignores the type of174 * the node. Sorted will strictly by name.175 *176 */177 static int ls_cmp_name(const void *a, const void *b)178 {179 struct dir_elem_t const *da = a;180 struct dir_elem_t const *db = b;181 182 return str_cmp(da->name, db->name);183 122 } 184 123 … … 253 192 } 254 193 255 if (ls.sort) { 256 int (*compar)(const void *, const void *); 257 compar = ls.single_column ? ls_cmp_name : ls_cmp_type_name; 258 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), compar); 259 } 260 261 for (i = 0; i < nbdirs; i++) { 262 if (ls.printer(&tosort[i]) != EOK) { 263 cli_error(CL_ENOMEM, "%s: Out of memory", cmdname); 264 goto out; 265 } 266 } 194 if (ls.sort) 195 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp); 196 197 for (i = 0; i < nbdirs; i++) 198 ls_print(&tosort[i]); 267 199 268 200 /* Populate the directory list. */ 269 if (ls.recursive ) {201 if (ls.recursive && nbdirs > 0) { 270 202 tmp = (struct dir_elem_t *) realloc(*dir_list_ptr, 271 203 nbdirs * sizeof(struct dir_elem_t)); … … 401 333 "If not path is given, the current working directory is used.\n" 402 334 "Options:\n" 403 " -h, --help A short option summary\n" 404 " -u, --unsort Do not sort directory entries\n" 405 " -r, --recursive List subdirectories recursively\n" 406 " -e, --exact-size File sizes will be unformatted (raw bytes count)\n" 407 " -1, --single-column Only the names will be returned\n", 335 " -h, --help A short option summary\n" 336 " -u, --unsort Do not sort directory entries\n" 337 " -r, --recursive List subdirectories recursively\n", 408 338 cmdname); 409 339 } … … 434 364 435 365 while (c != -1) { 436 c = getopt_long(argc, argv, "hur e1", long_options, &opt_ind);366 c = getopt_long(argc, argv, "hur", long_options, &opt_ind); 437 367 switch (c) { 438 368 case 'h': … … 445 375 ls.recursive = 1; 446 376 break; 447 case 'e':448 ls.exact_size = true;449 break;450 case '1':451 ls.single_column = true;452 ls.printer = ls_print_single_column;453 break;454 377 } 455 378 } … … 477 400 switch (scope) { 478 401 case LS_FILE: 479 if (ls.printer(&de) != EOK) { 480 cli_error(CL_ENOMEM, "%s: Out of memory", cmdname); 481 return CMD_FAILURE; 482 } 402 ls_print(&de); 483 403 break; 484 404 case LS_DIR:
Note:
See TracChangeset
for help on using the changeset viewer.