diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-10-28 12:37:16 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-10-28 12:37:16 +0000 |
commit | 5c7596058dedb99ab0fbe773cc7ac61a2ffe9fbf (patch) | |
tree | 8fa1fa4abeb29eb5ca5829a084a07fae55bc679b /coreutils/ls.c | |
parent | 8c35d65c43216bb840326ac7476a180e2ae36fe9 (diff) | |
download | busybox-w32-5c7596058dedb99ab0fbe773cc7ac61a2ffe9fbf.tar.gz busybox-w32-5c7596058dedb99ab0fbe773cc7ac61a2ffe9fbf.tar.bz2 busybox-w32-5c7596058dedb99ab0fbe773cc7ac61a2ffe9fbf.zip |
ls: cleanup part 1
Diffstat (limited to 'coreutils/ls.c')
-rw-r--r-- | coreutils/ls.c | 448 |
1 files changed, 179 insertions, 269 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 40e922846..f31a49682 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
@@ -98,18 +98,18 @@ enum { | |||
98 | #define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS) | 98 | #define TIME_ACCESS ((1U<<24) * ENABLE_FEATURE_LS_TIMESTAMPS) |
99 | #define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS) | 99 | #define TIME_MASK ((3U<<23) * ENABLE_FEATURE_LS_TIMESTAMPS) |
100 | 100 | ||
101 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS | 101 | #if ENABLE_FEATURE_LS_FOLLOWLINKS |
102 | #define FOLLOW_LINKS (1U<<25) | 102 | #define FOLLOW_LINKS (1U<<25) |
103 | #endif | 103 | #endif |
104 | #ifdef CONFIG_FEATURE_HUMAN_READABLE | 104 | #if ENABLE_FEATURE_HUMAN_READABLE |
105 | #define LS_DISP_HR (1U<<26) | 105 | #define LS_DISP_HR (1U<<26) |
106 | #endif | 106 | #endif |
107 | 107 | ||
108 | #define LIST_SHORT (LIST_FILENAME) | 108 | #define LIST_SHORT (LIST_FILENAME) |
109 | #define LIST_ISHORT (LIST_INO | LIST_FILENAME) | 109 | //#define LIST_ISHORT (LIST_INO | LIST_FILENAME) |
110 | #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \ | 110 | #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \ |
111 | LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK) | 111 | LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK) |
112 | #define LIST_ILONG (LIST_INO | LIST_LONG) | 112 | //#define LIST_ILONG (LIST_INO | LIST_LONG) |
113 | 113 | ||
114 | #define SPLIT_DIR 1 | 114 | #define SPLIT_DIR 1 |
115 | #define SPLIT_FILE 0 | 115 | #define SPLIT_FILE 0 |
@@ -123,22 +123,23 @@ enum { | |||
123 | #endif | 123 | #endif |
124 | 124 | ||
125 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ | 125 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ |
126 | #ifdef CONFIG_FEATURE_LS_COLOR | 126 | #if ENABLE_FEATURE_LS_COLOR |
127 | 127 | ||
128 | static int show_color = 0; | 128 | static int show_color; |
129 | 129 | ||
130 | /* long option entry used only for --color, which has no short option | 130 | /* long option entry used only for --color, which has no short option |
131 | * equivalent. */ | 131 | * equivalent. */ |
132 | static const struct option ls_color_opt[] = | 132 | static const struct option ls_color_opt[] = { |
133 | { | 133 | { "color", optional_argument, NULL, 1 }, |
134 | {"color", optional_argument, NULL, 1}, | 134 | { NULL, 0, NULL, 0 } |
135 | {NULL, 0, NULL, 0} | ||
136 | }; | 135 | }; |
137 | 136 | ||
138 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ | 137 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ |
139 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) | 138 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) |
140 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ | 139 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ |
141 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) | 140 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) |
141 | #else | ||
142 | enum { show_color = 0 }; | ||
142 | #endif | 143 | #endif |
143 | 144 | ||
144 | /* | 145 | /* |
@@ -149,9 +150,7 @@ struct dnode { /* the basic node */ | |||
149 | char *fullname; /* the dir entry name */ | 150 | char *fullname; /* the dir entry name */ |
150 | int allocated; | 151 | int allocated; |
151 | struct stat dstat; /* the file stat info */ | 152 | struct stat dstat; /* the file stat info */ |
152 | #ifdef CONFIG_SELINUX | 153 | USE_SELINUX(security_context_t sid;) |
153 | security_context_t sid; | ||
154 | #endif | ||
155 | struct dnode *next; /* point at the next node */ | 154 | struct dnode *next; /* point at the next node */ |
156 | }; | 155 | }; |
157 | typedef struct dnode dnode_t; | 156 | typedef struct dnode dnode_t; |
@@ -160,14 +159,16 @@ static struct dnode **list_dir(const char *); | |||
160 | static struct dnode **dnalloc(int); | 159 | static struct dnode **dnalloc(int); |
161 | static int list_single(struct dnode *); | 160 | static int list_single(struct dnode *); |
162 | 161 | ||
163 | static unsigned int all_fmt; | 162 | static unsigned all_fmt; |
164 | 163 | ||
165 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 164 | #if ENABLE_FEATURE_AUTOWIDTH |
166 | static unsigned terminal_width = TERMINAL_WIDTH; | ||
167 | static unsigned tabstops = COLUMN_GAP; | 165 | static unsigned tabstops = COLUMN_GAP; |
166 | static unsigned terminal_width = TERMINAL_WIDTH; | ||
168 | #else | 167 | #else |
169 | #define tabstops COLUMN_GAP | 168 | enum { |
170 | #define terminal_width TERMINAL_WIDTH | 169 | tabstops = COLUMN_GAP, |
170 | terminal_width = TERMINAL_WIDTH, | ||
171 | }; | ||
171 | #endif | 172 | #endif |
172 | 173 | ||
173 | static int status = EXIT_SUCCESS; | 174 | static int status = EXIT_SUCCESS; |
@@ -176,15 +177,12 @@ static struct dnode *my_stat(char *fullname, char *name) | |||
176 | { | 177 | { |
177 | struct stat dstat; | 178 | struct stat dstat; |
178 | struct dnode *cur; | 179 | struct dnode *cur; |
179 | #ifdef CONFIG_SELINUX | 180 | USE_SELINUX(security_context_t sid = NULL;) |
180 | security_context_t sid=NULL; | ||
181 | #endif | ||
182 | 181 | ||
183 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS | 182 | if (ENABLE_FEATURE_LS_FOLLOWLINKS && (all_fmt & FOLLOW_LINKS)) { |
184 | if (all_fmt & FOLLOW_LINKS) { | 183 | #if ENABLE_SELINUX |
185 | #ifdef CONFIG_SELINUX | ||
186 | if (is_selinux_enabled()) { | 184 | if (is_selinux_enabled()) { |
187 | getfilecon(fullname,&sid); | 185 | getfilecon(fullname, &sid); |
188 | } | 186 | } |
189 | #endif | 187 | #endif |
190 | if (stat(fullname, &dstat)) { | 188 | if (stat(fullname, &dstat)) { |
@@ -192,12 +190,10 @@ static struct dnode *my_stat(char *fullname, char *name) | |||
192 | status = EXIT_FAILURE; | 190 | status = EXIT_FAILURE; |
193 | return 0; | 191 | return 0; |
194 | } | 192 | } |
195 | } else | 193 | } else { |
196 | #endif | 194 | #if ENABLE_SELINUX |
197 | { | 195 | if (is_selinux_enabled()) { |
198 | #ifdef CONFIG_SELINUX | 196 | lgetfilecon(fullname,&sid); |
199 | if (is_selinux_enabled()) { | ||
200 | lgetfilecon(fullname,&sid); | ||
201 | } | 197 | } |
202 | #endif | 198 | #endif |
203 | if (lstat(fullname, &dstat)) { | 199 | if (lstat(fullname, &dstat)) { |
@@ -207,24 +203,21 @@ static struct dnode *my_stat(char *fullname, char *name) | |||
207 | } | 203 | } |
208 | } | 204 | } |
209 | 205 | ||
210 | cur = (struct dnode *) xmalloc(sizeof(struct dnode)); | 206 | cur = xmalloc(sizeof(struct dnode)); |
211 | cur->fullname = fullname; | 207 | cur->fullname = fullname; |
212 | cur->name = name; | 208 | cur->name = name; |
213 | cur->dstat = dstat; | 209 | cur->dstat = dstat; |
214 | #ifdef CONFIG_SELINUX | 210 | USE_SELINUX(cur->sid = sid;) |
215 | cur->sid = sid; | ||
216 | #endif | ||
217 | return cur; | 211 | return cur; |
218 | } | 212 | } |
219 | 213 | ||
220 | /*----------------------------------------------------------------------*/ | 214 | /*----------------------------------------------------------------------*/ |
221 | #ifdef CONFIG_FEATURE_LS_COLOR | 215 | #if ENABLE_FEATURE_LS_COLOR |
222 | static char fgcolor(mode_t mode) | 216 | static char fgcolor(mode_t mode) |
223 | { | 217 | { |
224 | /* Check wheter the file is existing (if so, color it red!) */ | 218 | /* Check wheter the file is existing (if so, color it red!) */ |
225 | if (errno == ENOENT) { | 219 | if (errno == ENOENT) |
226 | return '\037'; | 220 | return '\037'; |
227 | } | ||
228 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) | 221 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
229 | return COLOR(0xF000); /* File is executable ... */ | 222 | return COLOR(0xF000); /* File is executable ... */ |
230 | return COLOR(mode); | 223 | return COLOR(mode); |
@@ -240,7 +233,7 @@ static char bgcolor(mode_t mode) | |||
240 | #endif | 233 | #endif |
241 | 234 | ||
242 | /*----------------------------------------------------------------------*/ | 235 | /*----------------------------------------------------------------------*/ |
243 | #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR) | 236 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
244 | static char append_char(mode_t mode) | 237 | static char append_char(mode_t mode) |
245 | { | 238 | { |
246 | if (!(all_fmt & LIST_FILETYPE)) | 239 | if (!(all_fmt & LIST_FILETYPE)) |
@@ -257,25 +250,28 @@ static char append_char(mode_t mode) | |||
257 | 250 | ||
258 | /*----------------------------------------------------------------------*/ | 251 | /*----------------------------------------------------------------------*/ |
259 | 252 | ||
260 | #define countdirs(A,B) count_dirs((A), (B), 1) | 253 | #define countdirs(A, B) count_dirs((A), (B), 1) |
261 | #define countsubdirs(A,B) count_dirs((A), (B), 0) | 254 | #define countsubdirs(A, B) count_dirs((A), (B), 0) |
262 | 255 | ||
263 | static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) | 256 | static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) |
264 | { | 257 | { |
265 | int i, dirs; | 258 | int i, dirs; |
266 | 259 | ||
267 | if (dn == NULL || nfiles < 1) | 260 | if (!dn) |
268 | return (0); | 261 | return 0; |
269 | dirs = 0; | 262 | dirs = 0; |
270 | for (i = 0; i < nfiles; i++) { | 263 | for (i = 0; i < nfiles; i++) { |
271 | if (S_ISDIR(dn[i]->dstat.st_mode) | 264 | char *name; |
272 | && (notsubdirs || | 265 | if (!S_ISDIR(dn[i]->dstat.st_mode)) |
273 | ((dn[i]->name[0] != '.') || (dn[i]->name[1] | 266 | continue; |
274 | && ((dn[i]->name[1] != '.') | 267 | name = dn[i]->name; |
275 | || dn[i]->name[2]))))) | 268 | if (notsubdirs |
269 | || name[0]!='.' || (name[1] && (name[1]!='.' || name[2])) | ||
270 | ) { | ||
276 | dirs++; | 271 | dirs++; |
272 | } | ||
277 | } | 273 | } |
278 | return (dirs); | 274 | return dirs; |
279 | } | 275 | } |
280 | 276 | ||
281 | static int countfiles(struct dnode **dnp) | 277 | static int countfiles(struct dnode **dnp) |
@@ -284,27 +280,24 @@ static int countfiles(struct dnode **dnp) | |||
284 | struct dnode *cur; | 280 | struct dnode *cur; |
285 | 281 | ||
286 | if (dnp == NULL) | 282 | if (dnp == NULL) |
287 | return (0); | 283 | return 0; |
288 | nfiles = 0; | 284 | nfiles = 0; |
289 | for (cur = dnp[0]; cur->next != NULL; cur = cur->next) | 285 | for (cur = dnp[0]; cur->next != NULL; cur = cur->next) |
290 | nfiles++; | 286 | nfiles++; |
291 | nfiles++; | 287 | nfiles++; |
292 | return (nfiles); | 288 | return nfiles; |
293 | } | 289 | } |
294 | 290 | ||
295 | /* get memory to hold an array of pointers */ | 291 | /* get memory to hold an array of pointers */ |
296 | static struct dnode **dnalloc(int num) | 292 | static struct dnode **dnalloc(int num) |
297 | { | 293 | { |
298 | struct dnode **p; | ||
299 | |||
300 | if (num < 1) | 294 | if (num < 1) |
301 | return (NULL); | 295 | return NULL; |
302 | 296 | ||
303 | p = (struct dnode **) xzalloc(num * sizeof(struct dnode *)); | 297 | return xzalloc(num * sizeof(struct dnode *)); |
304 | return (p); | ||
305 | } | 298 | } |
306 | 299 | ||
307 | #ifdef CONFIG_FEATURE_LS_RECURSIVE | 300 | #if ENABLE_FEATURE_LS_RECURSIVE |
308 | static void dfree(struct dnode **dnp, int nfiles) | 301 | static void dfree(struct dnode **dnp, int nfiles) |
309 | { | 302 | { |
310 | int i; | 303 | int i; |
@@ -314,14 +307,14 @@ static void dfree(struct dnode **dnp, int nfiles) | |||
314 | 307 | ||
315 | for (i = 0; i < nfiles; i++) { | 308 | for (i = 0; i < nfiles; i++) { |
316 | struct dnode *cur = dnp[i]; | 309 | struct dnode *cur = dnp[i]; |
317 | if(cur->allocated) | 310 | if (cur->allocated) |
318 | free(cur->fullname); /* free the filename */ | 311 | free(cur->fullname); /* free the filename */ |
319 | free(cur); /* free the dnode */ | 312 | free(cur); /* free the dnode */ |
320 | } | 313 | } |
321 | free(dnp); /* free the array holding the dnode pointers */ | 314 | free(dnp); /* free the array holding the dnode pointers */ |
322 | } | 315 | } |
323 | #else | 316 | #else |
324 | #define dfree(...) | 317 | #define dfree(...) do {} while(0) |
325 | #endif | 318 | #endif |
326 | 319 | ||
327 | static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which) | 320 | static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which) |
@@ -330,7 +323,7 @@ static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which) | |||
330 | struct dnode **dnp; | 323 | struct dnode **dnp; |
331 | 324 | ||
332 | if (dn == NULL || nfiles < 1) | 325 | if (dn == NULL || nfiles < 1) |
333 | return (NULL); | 326 | return NULL; |
334 | 327 | ||
335 | /* count how many dirs and regular files there are */ | 328 | /* count how many dirs and regular files there are */ |
336 | if (which == SPLIT_SUBDIR) | 329 | if (which == SPLIT_SUBDIR) |
@@ -347,29 +340,29 @@ static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which) | |||
347 | /* copy the entrys into the file or dir array */ | 340 | /* copy the entrys into the file or dir array */ |
348 | for (d = i = 0; i < nfiles; i++) { | 341 | for (d = i = 0; i < nfiles; i++) { |
349 | if (S_ISDIR(dn[i]->dstat.st_mode)) { | 342 | if (S_ISDIR(dn[i]->dstat.st_mode)) { |
350 | if (which & (SPLIT_DIR|SPLIT_SUBDIR)) { | 343 | char *name; |
351 | if ((which & SPLIT_DIR) | 344 | if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) |
352 | || ((dn[i]->name[0] != '.') | 345 | continue; |
353 | || (dn[i]->name[1] | 346 | name = dn[i]->name; |
354 | && ((dn[i]->name[1] != '.') | 347 | if ((which & SPLIT_DIR) |
355 | || dn[i]->name[2])))) { | 348 | || name[0]!='.' || (name[1] && (name[1]!='.' || name[2])) |
356 | dnp[d++] = dn[i]; | 349 | ) { |
357 | } | 350 | dnp[d++] = dn[i]; |
358 | } | 351 | } |
359 | } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) { | 352 | } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) { |
360 | dnp[d++] = dn[i]; | 353 | dnp[d++] = dn[i]; |
361 | } | 354 | } |
362 | } | 355 | } |
363 | return (dnp); | 356 | return dnp; |
364 | } | 357 | } |
365 | 358 | ||
366 | /*----------------------------------------------------------------------*/ | 359 | /*----------------------------------------------------------------------*/ |
367 | #ifdef CONFIG_FEATURE_LS_SORTFILES | 360 | #if ENABLE_FEATURE_LS_SORTFILES |
368 | static int sortcmp(const void *a, const void *b) | 361 | static int sortcmp(const void *a, const void *b) |
369 | { | 362 | { |
370 | struct dnode *d1 = *(struct dnode **)a; | 363 | struct dnode *d1 = *(struct dnode **)a; |
371 | struct dnode *d2 = *(struct dnode **)b; | 364 | struct dnode *d2 = *(struct dnode **)b; |
372 | unsigned int sort_opts = all_fmt & SORT_MASK; | 365 | unsigned sort_opts = all_fmt & SORT_MASK; |
373 | int dif; | 366 | int dif; |
374 | 367 | ||
375 | dif = 0; /* assume SORT_NAME */ | 368 | dif = 0; /* assume SORT_NAME */ |
@@ -396,7 +389,7 @@ static int sortcmp(const void *a, const void *b) | |||
396 | if (all_fmt & SORT_ORDER_REVERSE) { | 389 | if (all_fmt & SORT_ORDER_REVERSE) { |
397 | dif = -dif; | 390 | dif = -dif; |
398 | } | 391 | } |
399 | return (dif); | 392 | return dif; |
400 | } | 393 | } |
401 | 394 | ||
402 | /*----------------------------------------------------------------------*/ | 395 | /*----------------------------------------------------------------------*/ |
@@ -406,7 +399,7 @@ static void dnsort(struct dnode **dn, int size) | |||
406 | } | 399 | } |
407 | #else | 400 | #else |
408 | #define sortcmp(a, b) 0 | 401 | #define sortcmp(a, b) 0 |
409 | #define dnsort(dn, size) | 402 | #define dnsort(dn, size) do {} while(0) |
410 | #endif | 403 | #endif |
411 | 404 | ||
412 | 405 | ||
@@ -427,7 +420,7 @@ static void showfiles(struct dnode **dn, int nfiles) | |||
427 | /* find the longest file name- use that as the column width */ | 420 | /* find the longest file name- use that as the column width */ |
428 | for (i = 0; i < nfiles; i++) { | 421 | for (i = 0; i < nfiles; i++) { |
429 | int len = strlen(dn[i]->name) + | 422 | int len = strlen(dn[i]->name) + |
430 | #ifdef CONFIG_SELINUX | 423 | #if ENABLE_SELINUX |
431 | ((all_fmt & LIST_CONTEXT) ? 33 : 0) + | 424 | ((all_fmt & LIST_CONTEXT) ? 33 : 0) + |
432 | #endif | 425 | #endif |
433 | ((all_fmt & LIST_INO) ? 8 : 0) + | 426 | ((all_fmt & LIST_INO) ? 8 : 0) + |
@@ -457,14 +450,12 @@ static void showfiles(struct dnode **dn, int nfiles) | |||
457 | if (i < nfiles) { | 450 | if (i < nfiles) { |
458 | if (column > 0) { | 451 | if (column > 0) { |
459 | nexttab -= column; | 452 | nexttab -= column; |
460 | while (nexttab--) { | 453 | printf("%*s", nexttab, ""); |
461 | putchar(' '); | 454 | column += nexttab; |
462 | column++; | 455 | } |
463 | } | ||
464 | } | ||
465 | nexttab = column + column_width; | 456 | nexttab = column + column_width; |
466 | column += list_single(dn[i]); | 457 | column += list_single(dn[i]); |
467 | } | 458 | } |
468 | } | 459 | } |
469 | putchar('\n'); | 460 | putchar('\n'); |
470 | column = 0; | 461 | column = 0; |
@@ -523,14 +514,14 @@ static struct dnode **list_dir(const char *path) | |||
523 | int i, nfiles; | 514 | int i, nfiles; |
524 | 515 | ||
525 | if (path == NULL) | 516 | if (path == NULL) |
526 | return (NULL); | 517 | return NULL; |
527 | 518 | ||
528 | dn = NULL; | 519 | dn = NULL; |
529 | nfiles = 0; | 520 | nfiles = 0; |
530 | dir = warn_opendir(path); | 521 | dir = warn_opendir(path); |
531 | if (dir == NULL) { | 522 | if (dir == NULL) { |
532 | status = EXIT_FAILURE; | 523 | status = EXIT_FAILURE; |
533 | return (NULL); /* could not open the dir */ | 524 | return NULL; /* could not open the dir */ |
534 | } | 525 | } |
535 | while ((entry = readdir(dir)) != NULL) { | 526 | while ((entry = readdir(dir)) != NULL) { |
536 | char *fullname; | 527 | char *fullname; |
@@ -547,8 +538,10 @@ static struct dnode **list_dir(const char *path) | |||
547 | } | 538 | } |
548 | fullname = concat_path_file(path, entry->d_name); | 539 | fullname = concat_path_file(path, entry->d_name); |
549 | cur = my_stat(fullname, strrchr(fullname, '/') + 1); | 540 | cur = my_stat(fullname, strrchr(fullname, '/') + 1); |
550 | if (!cur) | 541 | if (!cur) { |
542 | // FIXME: free(fullname); ? | ||
551 | continue; | 543 | continue; |
544 | } | ||
552 | cur->allocated = 1; | 545 | cur->allocated = 1; |
553 | cur->next = dn; | 546 | cur->next = dn; |
554 | dn = cur; | 547 | dn = cur; |
@@ -560,14 +553,14 @@ static struct dnode **list_dir(const char *path) | |||
560 | ** allocate memory for an array to hold dnode pointers | 553 | ** allocate memory for an array to hold dnode pointers |
561 | */ | 554 | */ |
562 | if (dn == NULL) | 555 | if (dn == NULL) |
563 | return (NULL); | 556 | return NULL; |
564 | dnp = dnalloc(nfiles); | 557 | dnp = dnalloc(nfiles); |
565 | for (i = 0, cur = dn; i < nfiles; i++) { | 558 | for (i = 0, cur = dn; i < nfiles; i++) { |
566 | dnp[i] = cur; /* save pointer to node in array */ | 559 | dnp[i] = cur; /* save pointer to node in array */ |
567 | cur = cur->next; | 560 | cur = cur->next; |
568 | } | 561 | } |
569 | 562 | ||
570 | return (dnp); | 563 | return dnp; |
571 | } | 564 | } |
572 | 565 | ||
573 | /*----------------------------------------------------------------------*/ | 566 | /*----------------------------------------------------------------------*/ |
@@ -575,22 +568,22 @@ static int list_single(struct dnode *dn) | |||
575 | { | 568 | { |
576 | int i, column = 0; | 569 | int i, column = 0; |
577 | 570 | ||
578 | #ifdef CONFIG_FEATURE_LS_USERNAME | 571 | #if ENABLE_FEATURE_LS_USERNAME |
579 | char scratch[16]; | 572 | char scratch[16]; |
580 | #endif | 573 | #endif |
581 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | 574 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
582 | char *filetime; | 575 | char *filetime; |
583 | time_t ttime, age; | 576 | time_t ttime, age; |
584 | #endif | 577 | #endif |
585 | #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) | 578 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
586 | struct stat info; | 579 | struct stat info; |
587 | char append; | 580 | char append; |
588 | #endif | 581 | #endif |
589 | 582 | ||
590 | if (dn->fullname == NULL) | 583 | if (dn->fullname == NULL) |
591 | return (0); | 584 | return 0; |
592 | 585 | ||
593 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | 586 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
594 | ttime = dn->dstat.st_mtime; /* the default time */ | 587 | ttime = dn->dstat.st_mtime; /* the default time */ |
595 | if (all_fmt & TIME_ACCESS) | 588 | if (all_fmt & TIME_ACCESS) |
596 | ttime = dn->dstat.st_atime; | 589 | ttime = dn->dstat.st_atime; |
@@ -598,21 +591,17 @@ static int list_single(struct dnode *dn) | |||
598 | ttime = dn->dstat.st_ctime; | 591 | ttime = dn->dstat.st_ctime; |
599 | filetime = ctime(&ttime); | 592 | filetime = ctime(&ttime); |
600 | #endif | 593 | #endif |
601 | #ifdef CONFIG_FEATURE_LS_FILETYPES | 594 | #if ENABLE_FEATURE_LS_FILETYPES |
602 | append = append_char(dn->dstat.st_mode); | 595 | append = append_char(dn->dstat.st_mode); |
603 | #endif | 596 | #endif |
604 | 597 | ||
605 | for (i = 0; i <= 31; i++) { | 598 | for (i = 0; i <= 31; i++) { |
606 | switch (all_fmt & (1 << i)) { | 599 | switch (all_fmt & (1 << i)) { |
607 | case LIST_INO: | 600 | case LIST_INO: |
608 | column += printf("%7ld ", (long int) dn->dstat.st_ino); | 601 | column += printf("%7ld ", (long) dn->dstat.st_ino); |
609 | break; | 602 | break; |
610 | case LIST_BLOCKS: | 603 | case LIST_BLOCKS: |
611 | #if _FILE_OFFSET_BITS == 64 | 604 | column += printf("%4"OFF_FMT" ", (off_t) dn->dstat.st_blocks >> 1); |
612 | column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1); | ||
613 | #else | ||
614 | column += printf("%4ld ", dn->dstat.st_blocks >> 1); | ||
615 | #endif | ||
616 | break; | 605 | break; |
617 | case LIST_MODEBITS: | 606 | case LIST_MODEBITS: |
618 | column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); | 607 | column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); |
@@ -621,7 +610,7 @@ static int list_single(struct dnode *dn) | |||
621 | column += printf("%4ld ", (long) dn->dstat.st_nlink); | 610 | column += printf("%4ld ", (long) dn->dstat.st_nlink); |
622 | break; | 611 | break; |
623 | case LIST_ID_NAME: | 612 | case LIST_ID_NAME: |
624 | #ifdef CONFIG_FEATURE_LS_USERNAME | 613 | #if ENABLE_FEATURE_LS_USERNAME |
625 | bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch)); | 614 | bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch)); |
626 | printf("%-8.8s ", scratch); | 615 | printf("%-8.8s ", scratch); |
627 | bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch)); | 616 | bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch)); |
@@ -638,22 +627,15 @@ static int list_single(struct dnode *dn) | |||
638 | column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev), | 627 | column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev), |
639 | (int) minor(dn->dstat.st_rdev)); | 628 | (int) minor(dn->dstat.st_rdev)); |
640 | } else { | 629 | } else { |
641 | #ifdef CONFIG_FEATURE_HUMAN_READABLE | 630 | if (ENABLE_FEATURE_HUMAN_READABLE && (all_fmt & LS_DISP_HR)) { |
642 | if (all_fmt & LS_DISP_HR) { | ||
643 | column += printf("%9s ", | 631 | column += printf("%9s ", |
644 | make_human_readable_str(dn->dstat.st_size, 1, 0)); | 632 | make_human_readable_str(dn->dstat.st_size, 1, 0)); |
645 | } else | 633 | } else { |
646 | #endif | 634 | column += printf("%9"OFF_FMT" ", (off_t) dn->dstat.st_size); |
647 | { | ||
648 | #if _FILE_OFFSET_BITS == 64 | ||
649 | column += printf("%9lld ", (long long) dn->dstat.st_size); | ||
650 | #else | ||
651 | column += printf("%9ld ", dn->dstat.st_size); | ||
652 | #endif | ||
653 | } | 635 | } |
654 | } | 636 | } |
655 | break; | 637 | break; |
656 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | 638 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
657 | case LIST_FULLTIME: | 639 | case LIST_FULLTIME: |
658 | printf("%24.24s ", filetime); | 640 | printf("%24.24s ", filetime); |
659 | column += 25; | 641 | column += 25; |
@@ -672,7 +654,7 @@ static int list_single(struct dnode *dn) | |||
672 | } | 654 | } |
673 | break; | 655 | break; |
674 | #endif | 656 | #endif |
675 | #ifdef CONFIG_SELINUX | 657 | #if ENABLE_SELINUX |
676 | case LIST_CONTEXT: | 658 | case LIST_CONTEXT: |
677 | { | 659 | { |
678 | char context[80]; | 660 | char context[80]; |
@@ -692,19 +674,15 @@ static int list_single(struct dnode *dn) | |||
692 | break; | 674 | break; |
693 | #endif | 675 | #endif |
694 | case LIST_FILENAME: | 676 | case LIST_FILENAME: |
695 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
696 | errno = 0; | 677 | errno = 0; |
697 | if (show_color && !lstat(dn->fullname, &info)) { | 678 | if (show_color && !lstat(dn->fullname, &info)) { |
698 | printf("\033[%d;%dm", bgcolor(info.st_mode), | 679 | printf("\033[%d;%dm", bgcolor(info.st_mode), |
699 | fgcolor(info.st_mode)); | 680 | fgcolor(info.st_mode)); |
700 | } | 681 | } |
701 | #endif | ||
702 | column += printf("%s", dn->name); | 682 | column += printf("%s", dn->name); |
703 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
704 | if (show_color) { | 683 | if (show_color) { |
705 | printf("\033[0m"); | 684 | printf("\033[0m"); |
706 | } | 685 | } |
707 | #endif | ||
708 | break; | 686 | break; |
709 | case LIST_SYMLINK: | 687 | case LIST_SYMLINK: |
710 | if (S_ISLNK(dn->dstat.st_mode)) { | 688 | if (S_ISLNK(dn->dstat.st_mode)) { |
@@ -717,24 +695,20 @@ static int list_single(struct dnode *dn) | |||
717 | append = append_char(info.st_mode); | 695 | append = append_char(info.st_mode); |
718 | } | 696 | } |
719 | #endif | 697 | #endif |
720 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
721 | if (show_color) { | 698 | if (show_color) { |
722 | errno = 0; | 699 | errno = 0; |
723 | printf("\033[%d;%dm", bgcolor(info.st_mode), | 700 | printf("\033[%d;%dm", bgcolor(info.st_mode), |
724 | fgcolor(info.st_mode)); | 701 | fgcolor(info.st_mode)); |
725 | } | 702 | } |
726 | #endif | ||
727 | column += printf("%s", lpath) + 4; | 703 | column += printf("%s", lpath) + 4; |
728 | #ifdef CONFIG_FEATURE_LS_COLOR | ||
729 | if (show_color) { | 704 | if (show_color) { |
730 | printf("\033[0m"); | 705 | printf("\033[0m"); |
731 | } | 706 | } |
732 | #endif | ||
733 | free(lpath); | 707 | free(lpath); |
734 | } | 708 | } |
735 | } | 709 | } |
736 | break; | 710 | break; |
737 | #ifdef CONFIG_FEATURE_LS_FILETYPES | 711 | #if ENABLE_FEATURE_LS_FILETYPES |
738 | case LIST_FILETYPE: | 712 | case LIST_FILETYPE: |
739 | if (append != '\0') { | 713 | if (append != '\0') { |
740 | printf("%1c", append); | 714 | printf("%1c", append); |
@@ -759,57 +733,15 @@ static int list_single(struct dnode *dn) | |||
759 | /* "[-]K", SELinux mandated options, busybox optionally supports */ | 733 | /* "[-]K", SELinux mandated options, busybox optionally supports */ |
760 | /* "[-]e", I think we made this one up */ | 734 | /* "[-]e", I think we made this one up */ |
761 | 735 | ||
762 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | 736 | static const char ls_options[] = "Cadil1gnsxAk" |
763 | # define LS_STR_TIMESTAMPS "cetu" | 737 | USE_FEATURE_LS_TIMESTAMPS("cetu") |
764 | #else | 738 | USE_FEATURE_LS_SORTFILES("SXrv") |
765 | # define LS_STR_TIMESTAMPS "" | 739 | USE_FEATURE_LS_FILETYPES("Fp") |
766 | #endif | 740 | USE_FEATURE_LS_FOLLOWLINKS("L") |
767 | 741 | USE_FEATURE_LS_RECURSIVE("R") | |
768 | #ifdef CONFIG_FEATURE_LS_FILETYPES | 742 | USE_FEATURE_HUMAN_READABLE("h") |
769 | # define LS_STR_FILETYPES "Fp" | 743 | USE_SELINUX("K") |
770 | #else | 744 | USE_FEATURE_AUTOWIDTH("T:w:"); |
771 | # define LS_STR_FILETYPES "" | ||
772 | #endif | ||
773 | |||
774 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS | ||
775 | # define LS_STR_FOLLOW_LINKS "L" | ||
776 | #else | ||
777 | # define LS_STR_FOLLOW_LINKS "" | ||
778 | #endif | ||
779 | |||
780 | #ifdef CONFIG_FEATURE_LS_RECURSIVE | ||
781 | # define LS_STR_RECURSIVE "R" | ||
782 | #else | ||
783 | # define LS_STR_RECURSIVE "" | ||
784 | #endif | ||
785 | |||
786 | #ifdef CONFIG_FEATURE_HUMAN_READABLE | ||
787 | # define LS_STR_HUMAN_READABLE "h" | ||
788 | #else | ||
789 | # define LS_STR_HUMAN_READABLE "" | ||
790 | #endif | ||
791 | |||
792 | #ifdef CONFIG_SELINUX | ||
793 | # define LS_STR_SELINUX "K" | ||
794 | #else | ||
795 | # define LS_STR_SELINUX "" | ||
796 | #endif | ||
797 | |||
798 | #ifdef CONFIG_FEATURE_AUTOWIDTH | ||
799 | # define LS_STR_AUTOWIDTH "T:w:" | ||
800 | #else | ||
801 | # define LS_STR_AUTOWIDTH "" | ||
802 | #endif | ||
803 | |||
804 | static const char ls_options[]="Cadil1gnsxAk" \ | ||
805 | LS_STR_TIMESTAMPS \ | ||
806 | USE_FEATURE_LS_SORTFILES("SXrv") \ | ||
807 | LS_STR_FILETYPES \ | ||
808 | LS_STR_FOLLOW_LINKS \ | ||
809 | LS_STR_RECURSIVE \ | ||
810 | LS_STR_HUMAN_READABLE \ | ||
811 | LS_STR_SELINUX \ | ||
812 | LS_STR_AUTOWIDTH; | ||
813 | 745 | ||
814 | #define LIST_MASK_TRIGGER 0 | 746 | #define LIST_MASK_TRIGGER 0 |
815 | #define STYLE_MASK_TRIGGER STYLE_MASK | 747 | #define STYLE_MASK_TRIGGER STYLE_MASK |
@@ -817,52 +749,52 @@ static const char ls_options[]="Cadil1gnsxAk" \ | |||
817 | #define DISP_MASK_TRIGGER DISP_ROWS | 749 | #define DISP_MASK_TRIGGER DISP_ROWS |
818 | 750 | ||
819 | static const unsigned opt_flags[] = { | 751 | static const unsigned opt_flags[] = { |
820 | LIST_SHORT | STYLE_COLUMNS, /* C */ | 752 | LIST_SHORT | STYLE_COLUMNS, /* C */ |
821 | DISP_HIDDEN | DISP_DOT, /* a */ | 753 | DISP_HIDDEN | DISP_DOT, /* a */ |
822 | DISP_NOLIST, /* d */ | 754 | DISP_NOLIST, /* d */ |
823 | LIST_INO, /* i */ | 755 | LIST_INO, /* i */ |
824 | LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */ | 756 | LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */ |
825 | LIST_SHORT | STYLE_SINGLE, /* 1 */ | 757 | LIST_SHORT | STYLE_SINGLE, /* 1 */ |
826 | 0, /* g - ingored */ | 758 | 0, /* g - ingored */ |
827 | LIST_ID_NUMERIC, /* n */ | 759 | LIST_ID_NUMERIC, /* n */ |
828 | LIST_BLOCKS, /* s */ | 760 | LIST_BLOCKS, /* s */ |
829 | DISP_ROWS, /* x */ | 761 | DISP_ROWS, /* x */ |
830 | DISP_HIDDEN, /* A */ | 762 | DISP_HIDDEN, /* A */ |
831 | #ifdef CONFIG_SELINUX | 763 | #if ENABLE_SELINUX |
832 | LIST_CONTEXT, /* k */ | 764 | LIST_CONTEXT, /* k */ |
833 | #else | 765 | #else |
834 | 0, /* k - ingored */ | 766 | 0, /* k - ingored */ |
835 | #endif | 767 | #endif |
836 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | 768 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
837 | TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */ | 769 | TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */ |
838 | LIST_FULLTIME, /* e */ | 770 | LIST_FULLTIME, /* e */ |
839 | ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */ | 771 | ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */ |
840 | TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */ | 772 | TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */ |
841 | #endif | 773 | #endif |
842 | #ifdef CONFIG_FEATURE_LS_SORTFILES | 774 | #if ENABLE_FEATURE_LS_SORTFILES |
843 | SORT_SIZE, /* S */ | 775 | SORT_SIZE, /* S */ |
844 | SORT_EXT, /* X */ | 776 | SORT_EXT, /* X */ |
845 | SORT_ORDER_REVERSE, /* r */ | 777 | SORT_ORDER_REVERSE, /* r */ |
846 | SORT_VERSION, /* v */ | 778 | SORT_VERSION, /* v */ |
847 | #endif | 779 | #endif |
848 | #ifdef CONFIG_FEATURE_LS_FILETYPES | 780 | #if ENABLE_FEATURE_LS_FILETYPES |
849 | LIST_FILETYPE | LIST_EXEC, /* F */ | 781 | LIST_FILETYPE | LIST_EXEC, /* F */ |
850 | LIST_FILETYPE, /* p */ | 782 | LIST_FILETYPE, /* p */ |
851 | #endif | 783 | #endif |
852 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS | 784 | #if ENABLE_FEATURE_LS_FOLLOWLINKS |
853 | FOLLOW_LINKS, /* L */ | 785 | FOLLOW_LINKS, /* L */ |
854 | #endif | 786 | #endif |
855 | #ifdef CONFIG_FEATURE_LS_RECURSIVE | 787 | #if ENABLE_FEATURE_LS_RECURSIVE |
856 | DISP_RECURSIVE, /* R */ | 788 | DISP_RECURSIVE, /* R */ |
857 | #endif | 789 | #endif |
858 | #ifdef CONFIG_FEATURE_HUMAN_READABLE | 790 | #if ENABLE_FEATURE_HUMAN_READABLE |
859 | LS_DISP_HR, /* h */ | 791 | LS_DISP_HR, /* h */ |
860 | #endif | 792 | #endif |
861 | #ifdef CONFIG_SELINUX | 793 | #if ENABLE_SELINUX |
862 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ | 794 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ |
863 | #endif | 795 | #endif |
864 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 796 | #if ENABLE_FEATURE_AUTOWIDTH |
865 | 0, 0, /* T, w - ignored */ | 797 | 0, 0, /* T, w - ignored */ |
866 | #endif | 798 | #endif |
867 | (1U<<31) | 799 | (1U<<31) |
868 | }; | 800 | }; |
@@ -885,110 +817,89 @@ int ls_main(int argc, char **argv) | |||
885 | int ac; | 817 | int ac; |
886 | int i; | 818 | int i; |
887 | char **av; | 819 | char **av; |
888 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 820 | #if ENABLE_FEATURE_AUTOWIDTH |
889 | char *tabstops_str = NULL; | 821 | char *tabstops_str = NULL; |
890 | char *terminal_width_str = NULL; | 822 | char *terminal_width_str = NULL; |
891 | #endif | 823 | #endif |
892 | #ifdef CONFIG_FEATURE_LS_COLOR | 824 | #if ENABLE_FEATURE_LS_COLOR |
893 | char *color_opt; | 825 | char *color_opt; |
894 | #endif | 826 | #endif |
895 | 827 | ||
896 | all_fmt = LIST_SHORT | | 828 | all_fmt = LIST_SHORT | |
897 | (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD)); | 829 | (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD)); |
898 | 830 | ||
899 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 831 | #if ENABLE_FEATURE_AUTOWIDTH |
900 | /* Obtain the terminal width. */ | 832 | /* Obtain the terminal width. */ |
901 | get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL); | 833 | get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL); |
902 | /* Go one less... */ | 834 | /* Go one less... */ |
903 | terminal_width--; | 835 | terminal_width--; |
904 | #endif | 836 | #endif |
905 | 837 | ||
906 | #ifdef CONFIG_FEATURE_LS_COLOR | 838 | #if ENABLE_FEATURE_LS_COLOR |
907 | applet_long_options = ls_color_opt; | 839 | applet_long_options = ls_color_opt; |
908 | #endif | 840 | #endif |
909 | 841 | ||
910 | /* process options */ | 842 | /* process options */ |
911 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 843 | #if ENABLE_FEATURE_AUTOWIDTH |
912 | opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str | 844 | opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str |
913 | #ifdef CONFIG_FEATURE_LS_COLOR | 845 | #if ENABLE_FEATURE_LS_COLOR |
914 | , &color_opt | 846 | , &color_opt |
915 | #endif | 847 | #endif |
916 | ); | 848 | ); |
917 | if (tabstops_str) { | 849 | if (tabstops_str) |
918 | tabstops = xatou(tabstops_str); | 850 | tabstops = xatou(tabstops_str); |
919 | } | 851 | if (terminal_width_str) |
920 | if (terminal_width_str) { | ||
921 | terminal_width = xatou(terminal_width_str); | 852 | terminal_width = xatou(terminal_width_str); |
922 | } | ||
923 | #else | 853 | #else |
924 | opt = getopt32(argc, argv, ls_options | 854 | opt = getopt32(argc, argv, ls_options |
925 | #ifdef CONFIG_FEATURE_LS_COLOR | 855 | #if ENABLE_FEATURE_LS_COLOR |
926 | , &color_opt | 856 | , &color_opt |
927 | #endif | 857 | #endif |
928 | ); | 858 | ); |
929 | #endif | 859 | #endif |
930 | for (i = 0; opt_flags[i] != (1U<<31); i++) { | 860 | for (i = 0; opt_flags[i] != (1U<<31); i++) { |
931 | if (opt & (1 << i)) { | 861 | if (opt & (1 << i)) { |
932 | unsigned int flags = opt_flags[i]; | 862 | unsigned flags = opt_flags[i]; |
933 | 863 | ||
934 | if (flags & LIST_MASK_TRIGGER) { | 864 | if (flags & LIST_MASK_TRIGGER) |
935 | all_fmt &= ~LIST_MASK; | 865 | all_fmt &= ~LIST_MASK; |
936 | } | 866 | if (flags & STYLE_MASK_TRIGGER) |
937 | if (flags & STYLE_MASK_TRIGGER) { | ||
938 | all_fmt &= ~STYLE_MASK; | 867 | all_fmt &= ~STYLE_MASK; |
939 | } | 868 | if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) |
940 | if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) { | ||
941 | all_fmt &= ~SORT_MASK; | 869 | all_fmt &= ~SORT_MASK; |
942 | } | 870 | if (flags & DISP_MASK_TRIGGER) |
943 | if (flags & DISP_MASK_TRIGGER) { | ||
944 | all_fmt &= ~DISP_MASK; | 871 | all_fmt &= ~DISP_MASK; |
945 | } | 872 | if (flags & TIME_MASK) |
946 | if (flags & TIME_MASK) { | ||
947 | all_fmt &= ~TIME_MASK; | 873 | all_fmt &= ~TIME_MASK; |
948 | } | 874 | if (flags & LIST_CONTEXT) |
949 | if (flags & LIST_CONTEXT) { | ||
950 | all_fmt |= STYLE_SINGLE; | 875 | all_fmt |= STYLE_SINGLE; |
951 | } | 876 | if (ENABLE_FEATURE_HUMAN_READABLE && opt == 'l') |
952 | #ifdef CONFIG_FEATURE_HUMAN_READABLE | ||
953 | if (opt == 'l') { | ||
954 | all_fmt &= ~LS_DISP_HR; | 877 | all_fmt &= ~LS_DISP_HR; |
955 | } | ||
956 | #endif | ||
957 | all_fmt |= flags; | 878 | all_fmt |= flags; |
958 | } | 879 | } |
959 | } | 880 | } |
960 | 881 | ||
961 | #ifdef CONFIG_FEATURE_LS_COLOR | 882 | #if ENABLE_FEATURE_LS_COLOR |
962 | { | 883 | /* find color bit value - last position for short getopt */ |
963 | /* find color bit value - last position for short getopt */ | 884 | if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) { |
964 | 885 | char *p = getenv("LS_COLORS"); | |
965 | #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT | 886 | /* LS_COLORS is unset, or (not empty && not "none") ? */ |
966 | char *p; | 887 | if (!p || (p[0] && strcmp(p, "none"))) |
967 | 888 | show_color = 1; | |
968 | if ((p = getenv ("LS_COLORS")) != NULL && | 889 | } |
969 | (*p == '\0' || (strcmp(p, "none") == 0))) { | 890 | if (opt & (1 << i)) { /* next flag after short options */ |
970 | ; | 891 | if (!color_opt || !strcmp("always", color_opt)) |
971 | } else if (isatty(STDOUT_FILENO)) { | 892 | show_color = 1; |
893 | else if (color_opt && !strcmp("never", color_opt)) | ||
894 | show_color = 0; | ||
895 | else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO)) | ||
972 | show_color = 1; | 896 | show_color = 1; |
973 | } | ||
974 | #endif | ||
975 | |||
976 | if((opt & (1 << i))) { /* next flag after short options */ | ||
977 | if (color_opt == NULL || strcmp("always", color_opt) == 0) | ||
978 | show_color = 1; | ||
979 | else if (color_opt != NULL && strcmp("never", color_opt) == 0) | ||
980 | show_color = 0; | ||
981 | else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO)) | ||
982 | show_color = 1; | ||
983 | } | ||
984 | } | 897 | } |
985 | #endif | 898 | #endif |
986 | 899 | ||
987 | /* sort out which command line options take precedence */ | 900 | /* sort out which command line options take precedence */ |
988 | #ifdef CONFIG_FEATURE_LS_RECURSIVE | 901 | if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST)) |
989 | if (all_fmt & DISP_NOLIST) | ||
990 | all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ | 902 | all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ |
991 | #endif | ||
992 | if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { | 903 | if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { |
993 | if (all_fmt & TIME_CHANGE) | 904 | if (all_fmt & TIME_CHANGE) |
994 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME; | 905 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME; |
@@ -997,10 +908,9 @@ int ls_main(int argc, char **argv) | |||
997 | } | 908 | } |
998 | if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */ | 909 | if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */ |
999 | all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); | 910 | all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); |
1000 | #ifdef CONFIG_FEATURE_LS_USERNAME | 911 | if (ENABLE_FEATURE_LS_USERNAME) |
1001 | if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC)) | 912 | if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC)) |
1002 | all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ | 913 | all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ |
1003 | #endif | ||
1004 | 914 | ||
1005 | /* choose a display format */ | 915 | /* choose a display format */ |
1006 | if (!(all_fmt & STYLE_MASK)) | 916 | if (!(all_fmt & STYLE_MASK)) |
@@ -1015,7 +925,7 @@ int ls_main(int argc, char **argv) | |||
1015 | */ | 925 | */ |
1016 | ac = argc - optind; /* how many cmd line args are left */ | 926 | ac = argc - optind; /* how many cmd line args are left */ |
1017 | if (ac < 1) { | 927 | if (ac < 1) { |
1018 | static const char * const dotdir[] = { "." }; | 928 | static const char *const dotdir[] = { "." }; |
1019 | 929 | ||
1020 | av = (char **) dotdir; | 930 | av = (char **) dotdir; |
1021 | ac = 1; | 931 | ac = 1; |
@@ -1027,7 +937,7 @@ int ls_main(int argc, char **argv) | |||
1027 | if (ac > 1) | 937 | if (ac > 1) |
1028 | all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ | 938 | all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ |
1029 | 939 | ||
1030 | /* stuff the command line file names into an dnode array */ | 940 | /* stuff the command line file names into a dnode array */ |
1031 | dn = NULL; | 941 | dn = NULL; |
1032 | for (oi = 0; oi < ac; oi++) { | 942 | for (oi = 0; oi < ac; oi++) { |
1033 | cur = my_stat(av[oi], av[oi]); | 943 | cur = my_stat(av[oi], av[oi]); |
@@ -1040,8 +950,8 @@ int ls_main(int argc, char **argv) | |||
1040 | } | 950 | } |
1041 | 951 | ||
1042 | /* now that we know how many files there are | 952 | /* now that we know how many files there are |
1043 | ** allocate memory for an array to hold dnode pointers | 953 | ** allocate memory for an array to hold dnode pointers |
1044 | */ | 954 | */ |
1045 | dnp = dnalloc(nfiles); | 955 | dnp = dnalloc(nfiles); |
1046 | for (i = 0, cur = dn; i < nfiles; i++) { | 956 | for (i = 0, cur = dn; i < nfiles; i++) { |
1047 | dnp[i] = cur; /* save pointer to node in array */ | 957 | dnp[i] = cur; /* save pointer to node in array */ |
@@ -1072,5 +982,5 @@ int ls_main(int argc, char **argv) | |||
1072 | } | 982 | } |
1073 | if (ENABLE_FEATURE_CLEAN_UP) | 983 | if (ENABLE_FEATURE_CLEAN_UP) |
1074 | dfree(dnp, nfiles); | 984 | dfree(dnp, nfiles); |
1075 | return (status); | 985 | return status; |
1076 | } | 986 | } |