From 4f43bda9472aeea372f6ab30bbef229b6c5f2c76 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 1 Aug 2025 12:06:02 +0200 Subject: ls: code shrink Looks like gcc can figure this out by itself, but let's be explicit Signed-off-by: Denys Vlasenko --- coreutils/ls.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'coreutils') 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) } #endif +/* Return the number of used columns. + * Note that only columnar output uses return value. + * -l and -1 modes don't care. + * coreutils 7.2 also supports: + * ls -b (--escape) = octal escapes (although it doesn't look like working) + * ls -N (--literal) = not escape at all + */ static unsigned calc_name_len(const char *name) { unsigned len; @@ -458,6 +465,7 @@ static unsigned calc_name_len(const char *name) return strlen(name); if (!(option_mask32 & OPT_Q)) { + /* the most likely branch: "ls" to tty (it auto-enables -q behavior) */ printable_string2(&uni_stat, name); return uni_stat.unicode_width; } @@ -467,16 +475,16 @@ static unsigned calc_name_len(const char *name) unsigned char ch = (unsigned char)*name; if (ch < ' ' || ch > 0x7e) { ch -= 7; - if ((signed char)ch >= 0 && ch <= 6) { - // quote chars 7..13 as \a,b,t,n,v,f,r - len++; - goto next; + if (ch <= 6) { + /* quote chars 7..13 as \a,b,t,n,v,f,r */ + goto two; } - // other chars <32 or >126 as \ooo octal + /* other chars <32 or >126 as \ooo octal */ len += 3; goto next; } if (*name == '"' || *name == '\\') { + two: len++; } next: @@ -484,14 +492,6 @@ static unsigned calc_name_len(const char *name) } return len; } - -/* Return the number of used columns. - * Note that only columnar output uses return value. - * -l and -1 modes don't care. - * coreutils 7.2 also supports: - * ls -b (--escape) = octal escapes (although it doesn't look like working) - * ls -N (--literal) = not escape at all - */ static unsigned print_name(const char *name) { unsigned len; @@ -503,6 +503,7 @@ static unsigned print_name(const char *name) } if (!(option_mask32 & OPT_Q)) { + /* the most likely branch: "ls" to tty (it auto-enables -q behavior) */ name = printable_string2(&uni_stat, name); fputs_stdout(name); return uni_stat.unicode_width; @@ -515,15 +516,14 @@ static unsigned print_name(const char *name) if (ch < ' ' || ch > 0x7e) { putchar('\\'); ch -= 7; - if ((signed char)ch >= 0 && ch <= 6) { - // quote chars 7..13 as \a,b,t,n,v,f,r + if (ch <= 6) { + /* quote chars 7..13 as \a,b,t,n,v,f,r */ ch = c_escape_conv_str07[1 + 3 * ch]; - len++; - goto put_ch; + goto two; } - // other chars <32 or >126 as \ooo octal + /* other chars <32 or >126 as \ooo octal */ ch = (unsigned char)*name; - putchar('0' + ((ch>>6) & 7)); + putchar('0' + (ch>>6)); putchar('0' + ((ch>>3) & 7)); ch = '0' + (ch & 7); len += 3; @@ -531,6 +531,7 @@ static unsigned print_name(const char *name) } if (ch == '"' || ch == '\\') { putchar('\\'); + two: len++; } put_ch: -- cgit v1.2.3-55-g6feb