aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-08-27 23:07:04 -0500
committerRob Landley <rob@landley.net>2016-08-27 23:07:04 -0500
commitbad56744355b2e212797d05ee0ebd763cc8211de (patch)
tree43741a8be6aa19b8a36b50dbf408c83e1adfd7f1
parentf9da4355525d4e5229788b0dc53a291cfc76bfe5 (diff)
downloadtoybox-bad56744355b2e212797d05ee0ebd763cc8211de.tar.gz
Remove some redundancy and unnecessary xprintf().
(We should use xprintf and similar to flush and check if stdout went away once per line. No point flushing partway through a line, so in ls only use it on things outputting a newline char.)
-rw-r--r--toys/posix/ls.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index c7e2da5a..73c4b7b3 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -20,7 +20,7 @@ config LS
-c use ctime for timestamps -d directory, not contents
-i inode number -k block sizes in kilobytes
-p put a '/' after dir names -q unprintable chars as '?'
- -s size (in blocks) -u use access time for timestamps
+ -s storage used (512 byte units) -u use access time for timestamps
-A list all files but . and .. -H follow command line symlinks
-L follow symlinks -R recursively list files in subdirs
-F append /dir *exe @sym |FIFO -Z security context
@@ -126,6 +126,12 @@ static int numlen(long long ll)
return snprintf(0, 0, "%llu", ll);
}
+static int print_with_h(char *s, long long value, int units)
+{
+ if (toys.optflags&FLAG_h) return human_readable(s, value*units, 0);
+ else return sprintf(s, "%lld", value);
+}
+
// Figure out size of printable entry fields for display indent/wrap
static void entrylen(struct dirtree *dt, unsigned *len)
@@ -148,18 +154,10 @@ static void entrylen(struct dirtree *dt, unsigned *len)
// cheating slightly here: assuming minor is always 3 digits to avoid
// tracking another column
len[5] = numlen(dev_major(st->st_rdev))+5;
- } else if (flags & FLAG_h) {
- human_readable(tmp, st->st_size, 0);
- len[5] = strwidth(tmp);
- } else len[5] = numlen(st->st_size);
+ } else len[5] = print_with_h(tmp, st->st_size, 1);
}
- if (flags & FLAG_s) {
- if (flags & FLAG_h) {
- human_readable(tmp, st->st_blocks*512, 0);
- len[6] = strwidth(tmp);
- } else len[6] = numlen(st->st_blocks);
- } else len[6] = 0;
+ len[6] = (flags & FLAG_s) ? print_with_h(tmp, st->st_blocks, 512) : 0;
len[7] = (flags & FLAG_Z) ? strwidth((char *)dt->extra) : 0;
}
@@ -360,10 +358,8 @@ static void listfiles(int dirfd, struct dirtree *indir)
}
totpad = totals[1]+!!totals[1]+totals[6]+!!totals[6]+totals[7]+!!totals[7];
if ((flags&(FLAG_h|FLAG_l|FLAG_o|FLAG_n|FLAG_g|FLAG_s)) && indir->parent) {
- if (flags&FLAG_h) {
- human_readable(tmp, blocks*512, 0);
- xprintf("total %s\n", tmp);
- } else xprintf("total %llu\n", blocks);
+ print_with_h(tmp, blocks, 512);
+ xprintf("total %s\n", tmp);
}
}
@@ -427,14 +423,11 @@ static void listfiles(int dirfd, struct dirtree *indir)
}
width += *len;
- if (flags & FLAG_i)
- xprintf("%*lu ", totals[1], (unsigned long)st->st_ino);
+ if (flags & FLAG_i) printf("%*lu ", totals[1], (unsigned long)st->st_ino);
if (flags & FLAG_s) {
- if (flags & FLAG_h) {
- human_readable(tmp, st->st_blocks*512, 0);
- xprintf("%*s ", totals[6], tmp);
- } else xprintf("%*lu ", totals[6], (unsigned long)st->st_blocks);
+ print_with_h(tmp, st->st_blocks, 512);
+ printf("%*s ", totals[6], tmp);
}
if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
@@ -469,15 +462,15 @@ static void listfiles(int dirfd, struct dirtree *indir)
if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
printf("% *d,% 4d", totals[5]-4, dev_major(st->st_rdev),
dev_minor(st->st_rdev));
- else if (flags&FLAG_h) {
- human_readable(tmp, st->st_size, 0);
- xprintf("%*s", totals[5]+1, tmp);
- } else printf("% *lld", totals[5]+1, (long long)st->st_size);
+ else {
+ print_with_h(tmp, st->st_size, 0);
+ printf("%*s", totals[5]+1, tmp);
+ }
// print time, always in --time-style=long-iso
tm = localtime(&(st->st_mtime));
strftime(tmp, sizeof(tmp), "%F %H:%M", tm);
- xprintf(" %s ", tmp);
+ printf(" %s ", tmp);
} else if (flags & FLAG_Z)
printf("%-*s ", (int)totals[7], (char *)sort[next]->extra);
@@ -488,7 +481,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
ss = sort[next]->name;
crunch_str(&ss, INT_MAX, stdout, TT.escmore, crunch_qb);
- if (color) xprintf("\033[0m");
+ if (color) printf("\033[0m");
if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode)) {
printf(" -> ");
@@ -510,7 +503,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Pad columns
if (flags & (FLAG_C|FLAG_x)) {
curcol = colsizes[curcol]-(*len)-totpad;
- if (curcol < 255) xprintf("%s", toybuf+255-curcol);
+ if (curcol < 255) printf("%s", toybuf+255-curcol);
}
}