Changeset a27e370 in mainline
- Timestamp:
- 2018-12-08T13:45:38Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0296ee1
- Parents:
- 87ba3ceb
- Location:
- uspace/app/bdsh/cmds/modules/ls
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/ls/ls.c
r87ba3ceb ra27e370 67 67 static unsigned int ls_start(ls_job_t *); 68 68 static void ls_print(struct dir_elem_t *); 69 static int ls_cmp(const void *, const void *); 69 static void 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 *); 70 72 static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **); 71 73 static unsigned int ls_recursive(const char *, DIR *); … … 79 81 ls->well_formatted = false; 80 82 ls->single_column = false; 81 83 ls->printer = ls_print; 82 84 return 1; 83 85 } … … 96 98 static void ls_print(struct dir_elem_t *de) 97 99 { 98 if (!ls.single_column && de->s.is_file) { 100 101 if (de->s.is_file) { 99 102 if (ls.well_formatted) { 100 103 cap_spec_t cap; … … 114 117 115 118 printf("%-40s\t%llu\n", de->name, (long long) de->s.size); 116 } else if ( !ls.single_column &&de->s.is_directory)119 } else if (de->s.is_directory) 117 120 printf("%-40s\t<dir>\n", de->name); 118 121 else … … 120 123 } 121 124 125 static void ls_print_single_column(struct dir_elem_t *de) 126 { 127 if (de->s.is_file) { 128 printf("%s\n", de->name); 129 } else { 130 printf("%s/\n", de->name); 131 } 132 } 133 122 134 /** Compare 2 directory elements. 123 135 * … … 131 143 * @return -1 if a < b, 1 otherwise. 132 144 */ 133 static int ls_cmp (const void *a, const void *b)145 static int ls_cmp_type_name(const void *a, const void *b) 134 146 { 135 147 struct dir_elem_t const *da = a; … … 142 154 else 143 155 return 1; 156 } 157 158 /** Compare directories/files per name 159 * 160 * This comparision ignores the type of 161 * the node. Sorted will strictly by name. 162 * 163 */ 164 static int ls_cmp_name(const void *a, const void *b) 165 { 166 struct dir_elem_t const *da = a; 167 struct dir_elem_t const *db = b; 168 169 return str_cmp(da->name, db->name); 144 170 } 145 171 … … 214 240 } 215 241 216 if (ls.sort) 217 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp); 242 if (ls.sort) { 243 int (*compar)(const void *, const void *); 244 compar = ls.single_column ? ls_cmp_name : ls_cmp_type_name; 245 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), compar); 246 } 218 247 219 248 for (i = 0; i < nbdirs; i++) 220 ls _print(&tosort[i]);249 ls.printer(&tosort[i]); 221 250 222 251 /* Populate the directory list. */ … … 404 433 case '1': 405 434 ls.single_column = true; 435 ls.printer = ls_print_single_column; 406 436 break; 407 437 } … … 430 460 switch (scope) { 431 461 case LS_FILE: 432 ls _print(&de);462 ls.printer(&de); 433 463 break; 434 464 case LS_DIR: -
uspace/app/bdsh/cmds/modules/ls/ls.h
r87ba3ceb ra27e370 8 8 #define LS_FILE 1 9 9 #define LS_DIR 2 10 11 typedef struct {12 /* Options set at runtime. */13 unsigned int recursive;14 unsigned int sort;15 16 bool single_column;17 bool well_formatted;18 } ls_job_t;19 10 20 11 /** Structure to represent a directory entry. … … 28 19 }; 29 20 21 typedef struct { 22 /* Options set at runtime. */ 23 unsigned int recursive; 24 unsigned int sort; 25 26 bool single_column; 27 bool well_formatted; 28 29 void (*printer)(struct dir_elem_t *); 30 } ls_job_t; 31 30 32 #endif
Note:
See TracChangeset
for help on using the changeset viewer.