aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-23 20:43:06 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-23 20:43:06 +0100
commitccc1699e3ec5ae0ec43a24fcad58c110f6f8360c (patch)
tree1ef87004d9a6b04370a2886f3422e18f90b5ee36 /coreutils
parent92c6898e10bde9889aa61e1a4f7253245b8f592e (diff)
downloadbusybox-w32-ccc1699e3ec5ae0ec43a24fcad58c110f6f8360c.tar.gz
busybox-w32-ccc1699e3ec5ae0ec43a24fcad58c110f6f8360c.tar.bz2
busybox-w32-ccc1699e3ec5ae0ec43a24fcad58c110f6f8360c.zip
ls: get rid of opt_flags[], handle -l1c through option_mask32
function old new delta display_single 885 888 +3 scan_and_display_dirs_recur 496 486 -10 opt_flags 11 - -11 ls_main 618 553 -65 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 1/2 up/down: 3/-86) Total: -83 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/ls.c100
1 files changed, 33 insertions, 67 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index ac142c36b..f90784a1a 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -194,16 +194,8 @@ SPLIT_DIR = 1,
194SPLIT_SUBDIR = 2, 194SPLIT_SUBDIR = 2,
195 195
196/* Bits in G.all_fmt: */ 196/* Bits in G.all_fmt: */
197LIST_LONG = 1 << 0, /* long listing (-l and equivalents) */
198
199/* what files will be displayed */ 197/* what files will be displayed */
200DISP_DIRNAME = 1 << 1, /* 2 or more items? label directories */ 198DISP_DIRNAME = 1 << 9, /* 2 or more items? label directories */
201
202/* what is the overall style of the listing */
203STYLE_COLUMNAR = 1 << 2, /* many records per line */
204STYLE_LONG = 2 << 2, /* one record per line, extended info */
205STYLE_SINGLE = 3 << 2, /* one record per line */
206STYLE_MASK = STYLE_SINGLE,
207}; 199};
208 200
209/* -Cadi1l Std options, busybox always supports */ 201/* -Cadi1l Std options, busybox always supports */
@@ -231,11 +223,11 @@ static const char ls_options[] ALIGN1 =
231 IF_FEATURE_LS_WIDTH("T:w:") /* 2, 29 */ 223 IF_FEATURE_LS_WIDTH("T:w:") /* 2, 29 */
232; 224;
233enum { 225enum {
234 //OPT_C = (1 << 0), 226 OPT_C = (1 << 0),
235 OPT_a = (1 << 1), 227 OPT_a = (1 << 1),
236 OPT_d = (1 << 2), 228 OPT_d = (1 << 2),
237 OPT_i = (1 << 3), 229 OPT_i = (1 << 3),
238 //OPT_1 = (1 << 4), 230 OPT_1 = (1 << 4),
239 OPT_l = (1 << 5), 231 OPT_l = (1 << 5),
240 OPT_g = (1 << 6), 232 OPT_g = (1 << 6),
241 OPT_n = (1 << 7), 233 OPT_n = (1 << 7),
@@ -288,23 +280,6 @@ enum {
288 OPT_color = (1 << OPTBIT_color ) * ENABLE_FEATURE_LS_COLOR, 280 OPT_color = (1 << OPTBIT_color ) * ENABLE_FEATURE_LS_COLOR,
289}; 281};
290 282
291/* TODO: simple toggles may be stored as OPT_xxx bits instead */
292static const uint8_t opt_flags[] = {
293 STYLE_COLUMNAR, /* C */
294 0, /* a */
295 0, /* d */
296 0, /* i */
297 STYLE_SINGLE, /* 1 */
298 LIST_LONG | STYLE_LONG, /* l - by keeping it after -1, "ls -l -1" ignores -1 */
299 LIST_LONG | STYLE_LONG, /* g (don't show owner) - handled via OPT_g. assumes l */
300 LIST_LONG | STYLE_LONG, /* n (numeris uid/gid) - handled via OPT_n. assumes l */
301 0, /* s */
302 STYLE_COLUMNAR, /* x */
303 0xff
304 /* options after -x are not processed through opt_flags */
305};
306
307
308/* 283/*
309 * a directory entry and its stat info 284 * a directory entry and its stat info
310 */ 285 */
@@ -465,8 +440,8 @@ static unsigned calc_name_len(const char *name)
465} 440}
466 441
467/* Return the number of used columns. 442/* Return the number of used columns.
468 * Note that only STYLE_COLUMNAR uses return value. 443 * Note that only columnar output uses return value.
469 * STYLE_SINGLE and STYLE_LONG don't care. 444 * -l and -1 modes don't care.
470 * coreutils 7.2 also supports: 445 * coreutils 7.2 also supports:
471 * ls -b (--escape) = octal escapes (although it doesn't look like working) 446 * ls -b (--escape) = octal escapes (although it doesn't look like working)
472 * ls -N (--literal) = not escape at all 447 * ls -N (--literal) = not escape at all
@@ -499,13 +474,14 @@ static unsigned print_name(const char *name)
499} 474}
500 475
501/* Return the number of used columns. 476/* Return the number of used columns.
502 * Note that only STYLE_COLUMNAR uses return value, 477 * Note that only columnar output uses return value,
503 * STYLE_SINGLE and STYLE_LONG don't care. 478 * -l and -1 modes don't care.
504 */ 479 */
505static NOINLINE unsigned display_single(const struct dnode *dn) 480static NOINLINE unsigned display_single(const struct dnode *dn)
506{ 481{
507 unsigned column = 0; 482 unsigned column = 0;
508 char *lpath; 483 char *lpath;
484 int opt;
509#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR 485#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
510 struct stat statbuf; 486 struct stat statbuf;
511 char append; 487 char append;
@@ -514,27 +490,28 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
514#if ENABLE_FEATURE_LS_FILETYPES 490#if ENABLE_FEATURE_LS_FILETYPES
515 append = append_char(dn->dn_mode); 491 append = append_char(dn->dn_mode);
516#endif 492#endif
493 opt = option_mask32;
517 494
518 /* Do readlink early, so that if it fails, error message 495 /* Do readlink early, so that if it fails, error message
519 * does not appear *inside* the "ls -l" line */ 496 * does not appear *inside* the "ls -l" line */
520 lpath = NULL; 497 lpath = NULL;
521 if (G.all_fmt & LIST_LONG) 498 if (opt & OPT_l)
522 if (S_ISLNK(dn->dn_mode)) 499 if (S_ISLNK(dn->dn_mode))
523 lpath = xmalloc_readlink_or_warn(dn->fullname); 500 lpath = xmalloc_readlink_or_warn(dn->fullname);
524 501
525 if (option_mask32 & OPT_i) /* show inode# */ 502 if (opt & OPT_i) /* show inode# */
526 column += printf("%7llu ", (long long) dn->dn_ino); 503 column += printf("%7llu ", (long long) dn->dn_ino);
527//TODO: -h should affect -s too: 504//TODO: -h should affect -s too:
528 if (option_mask32 & OPT_s) /* show allocated blocks */ 505 if (opt & OPT_s) /* show allocated blocks */
529 column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1)); 506 column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1));
530 if (G.all_fmt & LIST_LONG) { 507 if (opt & OPT_l) {
531 /* long listing: show mode */ 508 /* long listing: show mode */
532 column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode)); 509 column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode));
533 /* long listing: show number of links */ 510 /* long listing: show number of links */
534 column += printf("%4lu ", (long) dn->dn_nlink); 511 column += printf("%4lu ", (long) dn->dn_nlink);
535 /* long listing: show user/group */ 512 /* long listing: show user/group */
536 if (option_mask32 & OPT_n) { 513 if (opt & OPT_n) {
537 if (option_mask32 & OPT_g) 514 if (opt & OPT_g)
538 column += printf("%-8u ", (int) dn->dn_gid); 515 column += printf("%-8u ", (int) dn->dn_gid);
539 else 516 else
540 column += printf("%-8u %-8u ", 517 column += printf("%-8u %-8u ",
@@ -543,7 +520,7 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
543 } 520 }
544#if ENABLE_FEATURE_LS_USERNAME 521#if ENABLE_FEATURE_LS_USERNAME
545 else { 522 else {
546 if (option_mask32 & OPT_g) { 523 if (opt & OPT_g) {
547 column += printf("%-8.8s ", 524 column += printf("%-8.8s ",
548 get_cached_groupname(dn->dn_gid)); 525 get_cached_groupname(dn->dn_gid));
549 } else { 526 } else {
@@ -555,11 +532,11 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
555#endif 532#endif
556#if ENABLE_SELINUX 533#if ENABLE_SELINUX
557 } 534 }
558 if (option_mask32 & OPT_Z) { 535 if (opt & OPT_Z) {
559 column += printf("%-32s ", dn->sid ? dn->sid : "?"); 536 column += printf("%-32s ", dn->sid ? dn->sid : "?");
560 freecon(dn->sid); 537 freecon(dn->sid);
561 } 538 }
562 if (G.all_fmt & LIST_LONG) { 539 if (opt & OPT_l) {
563#endif 540#endif
564 /* long listing: show size */ 541 /* long listing: show size */
565 if (S_ISBLK(dn->dn_mode) || S_ISCHR(dn->dn_mode)) { 542 if (S_ISBLK(dn->dn_mode) || S_ISCHR(dn->dn_mode)) {
@@ -567,7 +544,7 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
567 dn->dn_rdev_maj, 544 dn->dn_rdev_maj,
568 dn->dn_rdev_min); 545 dn->dn_rdev_min);
569 } else { 546 } else {
570 if (option_mask32 & OPT_h) { 547 if (opt & OPT_h) {
571 column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ", 548 column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ",
572 /* print size, show one fractional, use suffixes */ 549 /* print size, show one fractional, use suffixes */
573 make_human_readable_str(dn->dn_size, 1, 0) 550 make_human_readable_str(dn->dn_size, 1, 0)
@@ -578,7 +555,7 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
578 } 555 }
579#if ENABLE_FEATURE_LS_TIMESTAMPS 556#if ENABLE_FEATURE_LS_TIMESTAMPS
580 /* long listing: show {m,c,a}time */ 557 /* long listing: show {m,c,a}time */
581 if (option_mask32 & OPT_full_time) { /* --full-time */ 558 if (opt & OPT_full_time) { /* --full-time */
582 /* coreutils 8.4 ls --full-time prints: 559 /* coreutils 8.4 ls --full-time prints:
583 * 2009-07-13 17:49:27.000000000 +0200 560 * 2009-07-13 17:49:27.000000000 +0200
584 * we don't show fractional seconds. 561 * we don't show fractional seconds.
@@ -624,7 +601,7 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
624 if (lpath) { 601 if (lpath) {
625 printf(" -> "); 602 printf(" -> ");
626#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR 603#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
627 if ((option_mask32 & (OPT_F|OPT_p)) 604 if ((opt & (OPT_F|OPT_p))
628 || G_show_color 605 || G_show_color
629 ) { 606 ) {
630 mode_t mode = dn->dn_mode_stat; 607 mode_t mode = dn->dn_mode_stat;
@@ -648,7 +625,7 @@ static NOINLINE unsigned display_single(const struct dnode *dn)
648 } 625 }
649 } 626 }
650#if ENABLE_FEATURE_LS_FILETYPES 627#if ENABLE_FEATURE_LS_FILETYPES
651 if (option_mask32 & (OPT_F|OPT_p)) { 628 if (opt & (OPT_F|OPT_p)) {
652 if (append) { 629 if (append) {
653 putchar(append); 630 putchar(append);
654 column++; 631 column++;
@@ -664,9 +641,9 @@ static void display_files(struct dnode **dn, unsigned nfiles)
664 unsigned i, ncols, nrows, row, nc; 641 unsigned i, ncols, nrows, row, nc;
665 unsigned column; 642 unsigned column;
666 unsigned nexttab; 643 unsigned nexttab;
667 unsigned column_width = 0; /* used only by STYLE_COLUMNAR */ 644 unsigned column_width = 0; /* used only by coulmnal output */
668 645
669 if (G.all_fmt & STYLE_LONG) { /* STYLE_LONG or STYLE_SINGLE */ 646 if (option_mask32 & (OPT_l|OPT_1)) {
670 ncols = 1; 647 ncols = 1;
671 } else { 648 } else {
672 /* find the longest file name, use that as the column width */ 649 /* find the longest file name, use that as the column width */
@@ -1043,9 +1020,7 @@ static void scan_and_display_dirs_recur(struct dnode **dn, int first)
1043 } 1020 }
1044 subdnp = scan_one_dir((*dn)->fullname, &nfiles); 1021 subdnp = scan_one_dir((*dn)->fullname, &nfiles);
1045#if ENABLE_DESKTOP 1022#if ENABLE_DESKTOP
1046 if ((G.all_fmt & STYLE_MASK) == STYLE_LONG 1023 if (option_mask32 & (OPT_s|OPT_l)) {
1047 || (option_mask32 & OPT_s)
1048 ) {
1049 printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp)); 1024 printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
1050 } 1025 }
1051#endif 1026#endif
@@ -1124,8 +1099,10 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1124 /* process options */ 1099 /* process options */
1125 IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;) 1100 IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;)
1126 opt_complementary = 1101 opt_complementary =
1102 /* -n and -g imply -l */
1103 "nl:gl"
1127 /* --full-time implies -l */ 1104 /* --full-time implies -l */
1128 IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS("\xff""l")) 1105 IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS(":\xff""l"))
1129 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: 1106 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html:
1130 * in some pairs of opts, only last one takes effect: 1107 * in some pairs of opts, only last one takes effect:
1131 */ 1108 */
@@ -1139,7 +1116,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1139 /* -w NUM: */ 1116 /* -w NUM: */
1140 IF_FEATURE_LS_WIDTH(":w+"); 1117 IF_FEATURE_LS_WIDTH(":w+");
1141 opt = getopt32(argv, ls_options 1118 opt = getopt32(argv, ls_options
1142 IF_FEATURE_LS_WIDTH(, NULL, &G_terminal_width) 1119 IF_FEATURE_LS_WIDTH(, /*-T*/ NULL, /*-w*/ &G_terminal_width)
1143 IF_FEATURE_LS_COLOR(, &color_opt) 1120 IF_FEATURE_LS_COLOR(, &color_opt)
1144 ); 1121 );
1145#if 0 /* option bits debug */ 1122#if 0 /* option bits debug */
@@ -1152,16 +1129,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1152 if (opt & OPT_full_time ) bb_error_msg("--full-time"); 1129 if (opt & OPT_full_time ) bb_error_msg("--full-time");
1153 exit(0); 1130 exit(0);
1154#endif 1131#endif
1155 for (i = 0; opt_flags[i] != 0xff; i++) {
1156 if (opt & (1 << i)) {
1157 uint32_t flags = opt_flags[i];
1158
1159 if (flags & STYLE_MASK)
1160 G.all_fmt &= ~STYLE_MASK;
1161 1132
1162 G.all_fmt |= flags;
1163 }
1164 }
1165#if ENABLE_SELINUX 1133#if ENABLE_SELINUX
1166 if (opt & OPT_Z) { 1134 if (opt & OPT_Z) {
1167 if (!is_selinux_enabled()) 1135 if (!is_selinux_enabled())
@@ -1197,7 +1165,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1197 /* sort out which command line options take precedence */ 1165 /* sort out which command line options take precedence */
1198 if (ENABLE_FEATURE_LS_RECURSIVE && (opt & OPT_d)) 1166 if (ENABLE_FEATURE_LS_RECURSIVE && (opt & OPT_d))
1199 option_mask32 &= ~OPT_R; /* no recurse if listing only dir */ 1167 option_mask32 &= ~OPT_R; /* no recurse if listing only dir */
1200 if ((G.all_fmt & STYLE_MASK) != STYLE_LONG) { /* not -l? */ 1168 if (!(opt & OPT_l)) { /* not -l? */
1201 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { 1169 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
1202 /* when to sort by time? -t[cu] sorts by time even with -l */ 1170 /* when to sort by time? -t[cu] sorts by time even with -l */
1203 /* (this is achieved by opt_flags[] element for -t) */ 1171 /* (this is achieved by opt_flags[] element for -t) */
@@ -1210,8 +1178,8 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1210 } 1178 }
1211 1179
1212 /* choose a display format if one was not already specified by an option */ 1180 /* choose a display format if one was not already specified by an option */
1213 if (!(G.all_fmt & STYLE_MASK)) 1181 if (!(option_mask32 & (OPT_l|OPT_1|OPT_x|OPT_C)))
1214 G.all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNAR : STYLE_SINGLE); 1182 option_mask32 |= (isatty(STDOUT_FILENO) ? OPT_C : OPT_1);
1215 1183
1216 argv += optind; 1184 argv += optind;
1217 if (!argv[0]) 1185 if (!argv[0])
@@ -1226,9 +1194,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1226 do { 1194 do {
1227 cur = my_stat(*argv, *argv, 1195 cur = my_stat(*argv, *argv,
1228 /* follow links on command line unless -l, -s or -F: */ 1196 /* follow links on command line unless -l, -s or -F: */
1229 !((G.all_fmt & STYLE_MASK) == STYLE_LONG 1197 !(option_mask32 & (OPT_l|OPT_s|OPT_F))
1230 || (option_mask32 & (OPT_s|OPT_F))
1231 )
1232 /* ... or if -H: */ 1198 /* ... or if -H: */
1233 || (option_mask32 & OPT_H) 1199 || (option_mask32 & OPT_H)
1234 /* ... or if -L, but my_stat always follows links if -L */ 1200 /* ... or if -L, but my_stat always follows links if -L */