aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2025-08-01 01:02:43 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2025-08-01 01:02:43 +0200
commit4f3a56dc12a2126bdacff73f1cfe586d06e800c0 (patch)
tree863d23f2fc3d2bf6b77213ef729a6ebda7101244 /coreutils
parentbb18473216253b8602ce081dd944f854aad9e572 (diff)
downloadbusybox-w32-4f3a56dc12a2126bdacff73f1cfe586d06e800c0.tar.gz
busybox-w32-4f3a56dc12a2126bdacff73f1cfe586d06e800c0.tar.bz2
busybox-w32-4f3a56dc12a2126bdacff73f1cfe586d06e800c0.zip
ls: fix -Q to match GNU
function old new delta print_name 137 229 +92 display_files 375 402 +27 c_escape_conv_str00 - 24 +24 display 1476 1485 +9 conv_str 33 - -33 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 3/0 up/down: 152/-33) Total: 119 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/ls.c50
1 files changed, 38 insertions, 12 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index c725be92d..9e4b83032 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -457,19 +457,29 @@ static unsigned calc_name_len(const char *name)
457 if (!(option_mask32 & (OPT_q|OPT_Q))) 457 if (!(option_mask32 & (OPT_q|OPT_Q)))
458 return strlen(name); 458 return strlen(name);
459 459
460 name = printable_string2(&uni_stat, name);
461
462 if (!(option_mask32 & OPT_Q)) { 460 if (!(option_mask32 & OPT_Q)) {
461 printable_string2(&uni_stat, name);
463 return uni_stat.unicode_width; 462 return uni_stat.unicode_width;
464 } 463 }
465 464
466 // TODO: quote chars 7..13 as \a,b,t,n,v,f,r 465 len = 2 + strlen(name);
467 // other chars <32 or >127 as \ooo octal
468 len = 2 + uni_stat.unicode_width;
469 while (*name) { 466 while (*name) {
467 unsigned char ch = (unsigned char)*name;
468 if (ch < ' ' || ch > 0x7e) {
469 ch -= 7;
470 if ((signed char)ch >= 0 && ch <= 6) {
471 // quote chars 7..13 as \a,b,t,n,v,f,r
472 len++;
473 goto next;
474 }
475 // other chars <32 or >126 as \ooo octal
476 len += 3;
477 goto next;
478 }
470 if (*name == '"' || *name == '\\') { 479 if (*name == '"' || *name == '\\') {
471 len++; 480 len++;
472 } 481 }
482 next:
473 name++; 483 name++;
474 } 484 }
475 return len; 485 return len;
@@ -492,23 +502,39 @@ static unsigned print_name(const char *name)
492 return strlen(name); 502 return strlen(name);
493 } 503 }
494 504
495 name = printable_string2(&uni_stat, name);
496
497 if (!(option_mask32 & OPT_Q)) { 505 if (!(option_mask32 & OPT_Q)) {
506 name = printable_string2(&uni_stat, name);
498 fputs_stdout(name); 507 fputs_stdout(name);
499 return uni_stat.unicode_width; 508 return uni_stat.unicode_width;
500 } 509 }
501 510
502 // TODO: quote chars 7..13 as \a,b,t,n,v,f,r 511 len = 2 + strlen(name);
503 // other chars <32 or >127 as \ooo octal
504 len = 2 + uni_stat.unicode_width;
505 putchar('"'); 512 putchar('"');
506 while (*name) { 513 while (*name) {
507 if (*name == '"' || *name == '\\') { 514 unsigned char ch = (unsigned char)*name;
515 if (ch < ' ' || ch > 0x7e) {
516 putchar('\\');
517 ch -= 7;
518 if ((signed char)ch >= 0 && ch <= 6) {
519 // quote chars 7..13 as \a,b,t,n,v,f,r
520 ch = c_escape_conv_str07[1 + 3 * ch];
521 len++;
522 goto put_ch;
523 }
524 // other chars <32 or >126 as \ooo octal
525 ch = (unsigned char)*name;
526 putchar('0' + ((ch>>6) & 7));
527 putchar('0' + ((ch>>3) & 7));
528 ch = '0' + (ch & 7);
529 len += 3;
530 goto put_ch;
531 }
532 if (ch == '"' || ch == '\\') {
508 putchar('\\'); 533 putchar('\\');
509 len++; 534 len++;
510 } 535 }
511 putchar(*name); 536 put_ch:
537 putchar(ch);
512 name++; 538 name++;
513 } 539 }
514 putchar('"'); 540 putchar('"');