summaryrefslogtreecommitdiff
path: root/coreutils/ls.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2025-08-01 12:06:02 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2025-08-01 12:10:22 +0200
commit4f43bda9472aeea372f6ab30bbef229b6c5f2c76 (patch)
treec15c079c9ac67529ea104ef7d69271b558188576 /coreutils/ls.c
parent0fea54a983dd72f8b725f8aeaf0494c7f86043f7 (diff)
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>
Diffstat (limited to 'coreutils/ls.c')
-rw-r--r--coreutils/ls.c39
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)
}
#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: