diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2025-08-01 12:06:02 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2025-08-01 12:10:22 +0200 |
| commit | 4f43bda9472aeea372f6ab30bbef229b6c5f2c76 (patch) | |
| tree | c15c079c9ac67529ea104ef7d69271b558188576 | |
| parent | 0fea54a983dd72f8b725f8aeaf0494c7f86043f7 (diff) | |
| download | busybox-w32-4f43bda9472aeea372f6ab30bbef229b6c5f2c76.tar.gz busybox-w32-4f43bda9472aeea372f6ab30bbef229b6c5f2c76.tar.bz2 busybox-w32-4f43bda9472aeea372f6ab30bbef229b6c5f2c76.zip | |
ls: code shrink
Looks like gcc can figure this out by itself, but let's be explicit
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | coreutils/ls.c | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 9e4b83032..200baff03 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
| @@ -449,6 +449,13 @@ static char append_char(mode_t mode) | |||
| 449 | } | 449 | } |
| 450 | #endif | 450 | #endif |
| 451 | 451 | ||
| 452 | /* Return the number of used columns. | ||
| 453 | * Note that only columnar output uses return value. | ||
| 454 | * -l and -1 modes don't care. | ||
| 455 | * coreutils 7.2 also supports: | ||
| 456 | * ls -b (--escape) = octal escapes (although it doesn't look like working) | ||
| 457 | * ls -N (--literal) = not escape at all | ||
| 458 | */ | ||
| 452 | static unsigned calc_name_len(const char *name) | 459 | static unsigned calc_name_len(const char *name) |
| 453 | { | 460 | { |
| 454 | unsigned len; | 461 | unsigned len; |
| @@ -458,6 +465,7 @@ static unsigned calc_name_len(const char *name) | |||
| 458 | return strlen(name); | 465 | return strlen(name); |
| 459 | 466 | ||
| 460 | if (!(option_mask32 & OPT_Q)) { | 467 | if (!(option_mask32 & OPT_Q)) { |
| 468 | /* the most likely branch: "ls" to tty (it auto-enables -q behavior) */ | ||
| 461 | printable_string2(&uni_stat, name); | 469 | printable_string2(&uni_stat, name); |
| 462 | return uni_stat.unicode_width; | 470 | return uni_stat.unicode_width; |
| 463 | } | 471 | } |
| @@ -467,16 +475,16 @@ static unsigned calc_name_len(const char *name) | |||
| 467 | unsigned char ch = (unsigned char)*name; | 475 | unsigned char ch = (unsigned char)*name; |
| 468 | if (ch < ' ' || ch > 0x7e) { | 476 | if (ch < ' ' || ch > 0x7e) { |
| 469 | ch -= 7; | 477 | ch -= 7; |
| 470 | if ((signed char)ch >= 0 && ch <= 6) { | 478 | if (ch <= 6) { |
| 471 | // quote chars 7..13 as \a,b,t,n,v,f,r | 479 | /* quote chars 7..13 as \a,b,t,n,v,f,r */ |
| 472 | len++; | 480 | goto two; |
| 473 | goto next; | ||
| 474 | } | 481 | } |
| 475 | // other chars <32 or >126 as \ooo octal | 482 | /* other chars <32 or >126 as \ooo octal */ |
| 476 | len += 3; | 483 | len += 3; |
| 477 | goto next; | 484 | goto next; |
| 478 | } | 485 | } |
| 479 | if (*name == '"' || *name == '\\') { | 486 | if (*name == '"' || *name == '\\') { |
| 487 | two: | ||
| 480 | len++; | 488 | len++; |
| 481 | } | 489 | } |
| 482 | next: | 490 | next: |
| @@ -484,14 +492,6 @@ static unsigned calc_name_len(const char *name) | |||
| 484 | } | 492 | } |
| 485 | return len; | 493 | return len; |
| 486 | } | 494 | } |
| 487 | |||
| 488 | /* Return the number of used columns. | ||
| 489 | * Note that only columnar output uses return value. | ||
| 490 | * -l and -1 modes don't care. | ||
| 491 | * coreutils 7.2 also supports: | ||
| 492 | * ls -b (--escape) = octal escapes (although it doesn't look like working) | ||
| 493 | * ls -N (--literal) = not escape at all | ||
| 494 | */ | ||
| 495 | static unsigned print_name(const char *name) | 495 | static unsigned print_name(const char *name) |
| 496 | { | 496 | { |
| 497 | unsigned len; | 497 | unsigned len; |
| @@ -503,6 +503,7 @@ static unsigned print_name(const char *name) | |||
| 503 | } | 503 | } |
| 504 | 504 | ||
| 505 | if (!(option_mask32 & OPT_Q)) { | 505 | if (!(option_mask32 & OPT_Q)) { |
| 506 | /* the most likely branch: "ls" to tty (it auto-enables -q behavior) */ | ||
| 506 | name = printable_string2(&uni_stat, name); | 507 | name = printable_string2(&uni_stat, name); |
| 507 | fputs_stdout(name); | 508 | fputs_stdout(name); |
| 508 | return uni_stat.unicode_width; | 509 | return uni_stat.unicode_width; |
| @@ -515,15 +516,14 @@ static unsigned print_name(const char *name) | |||
| 515 | if (ch < ' ' || ch > 0x7e) { | 516 | if (ch < ' ' || ch > 0x7e) { |
| 516 | putchar('\\'); | 517 | putchar('\\'); |
| 517 | ch -= 7; | 518 | ch -= 7; |
| 518 | if ((signed char)ch >= 0 && ch <= 6) { | 519 | if (ch <= 6) { |
| 519 | // quote chars 7..13 as \a,b,t,n,v,f,r | 520 | /* quote chars 7..13 as \a,b,t,n,v,f,r */ |
| 520 | ch = c_escape_conv_str07[1 + 3 * ch]; | 521 | ch = c_escape_conv_str07[1 + 3 * ch]; |
| 521 | len++; | 522 | goto two; |
| 522 | goto put_ch; | ||
| 523 | } | 523 | } |
| 524 | // other chars <32 or >126 as \ooo octal | 524 | /* other chars <32 or >126 as \ooo octal */ |
| 525 | ch = (unsigned char)*name; | 525 | ch = (unsigned char)*name; |
| 526 | putchar('0' + ((ch>>6) & 7)); | 526 | putchar('0' + (ch>>6)); |
| 527 | putchar('0' + ((ch>>3) & 7)); | 527 | putchar('0' + ((ch>>3) & 7)); |
| 528 | ch = '0' + (ch & 7); | 528 | ch = '0' + (ch & 7); |
| 529 | len += 3; | 529 | len += 3; |
| @@ -531,6 +531,7 @@ static unsigned print_name(const char *name) | |||
| 531 | } | 531 | } |
| 532 | if (ch == '"' || ch == '\\') { | 532 | if (ch == '"' || ch == '\\') { |
| 533 | putchar('\\'); | 533 | putchar('\\'); |
| 534 | two: | ||
| 534 | len++; | 535 | len++; |
| 535 | } | 536 | } |
| 536 | put_ch: | 537 | put_ch: |
