diff options
author | millert <millert@openbsd.org> | 2020-10-07 21:03:09 +0000 |
---|---|---|
committer | millert <millert@openbsd.org> | 2020-10-07 21:03:09 +0000 |
commit | bc2593c5afc2fbdaea8c85b3fa17064984d9da09 (patch) | |
tree | 537139949675ab49e69fdfd3a4d5d4aca7722ef0 /bin | |
parent | sys_getitimer(), sys_setitimer(): style(9), misc. cleanup (diff) | |
download | wireguard-openbsd-bc2593c5afc2fbdaea8c85b3fa17064984d9da09.tar.xz wireguard-openbsd-bc2593c5afc2fbdaea8c85b3fa17064984d9da09.zip |
If we are asked to print the total number of blocks, do so even if
we have no entries to print (either due to an empty directory or
an error). This makes the -l and -s options more consistent, and
matches the behavior of AT&T and GNU ls. From FreeBSD (das).
OK kn@
Diffstat (limited to 'bin')
-rw-r--r-- | bin/ls/ls.c | 28 | ||||
-rw-r--r-- | bin/ls/print.c | 11 |
2 files changed, 22 insertions, 17 deletions
diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 75fcd0815ab..125512ba6e7 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ls.c,v 1.53 2020/07/06 00:55:05 millert Exp $ */ +/* $OpenBSD: ls.c,v 1.54 2020/10/07 21:03:09 millert Exp $ */ /* $NetBSD: ls.c,v 1.18 1996/07/09 09:16:29 mycroft Exp $ */ /* @@ -355,7 +355,13 @@ traverse(int argc, char *argv[], int options) fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) err(1, NULL); - display(NULL, fts_children(ftsp, 0)); + /* + * We ignore errors from fts_children here since they will be + * replicated and signalled on the next call to fts_read() below. + */ + chp = fts_children(ftsp, 0); + if (chp != NULL) + display(NULL, chp); if (f_listdir) return; @@ -438,16 +444,6 @@ display(FTSENT *p, FTSENT *list) char buf[21]; /* 64 bits == 20 digits */ char *flags = NULL; - /* - * If list is NULL there are two possibilities: that the parent - * directory p has no children, or that fts_children() returned an - * error. We ignore the error case since it will be replicated - * on the next call to fts_read() on the post-order visit to the - * directory p, and will be signalled in traverse(). - */ - if (list == NULL) - return; - needstats = f_inode || f_longform || f_size; flen = 0; btotal = maxblock = maxinode = maxlen = maxnlink = 0; @@ -542,7 +538,13 @@ display(FTSENT *p, FTSENT *list) ++entries; } - if (!entries) + /* + * If there are no entries to display, we normally stop right + * here. However, we must continue if we have to display the + * total block count. In this case, we display the total only + * on the second (p != NULL) pass. + */ + if (!entries && (!(f_longform || f_size) || p == NULL)) return; d.list = list; diff --git a/bin/ls/print.c b/bin/ls/print.c index 959282c61a2..646b4277b81 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: print.c,v 1.38 2019/02/05 02:17:32 deraadt Exp $ */ +/* $OpenBSD: print.c,v 1.39 2020/10/07 21:03:09 millert Exp $ */ /* $NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $ */ /* @@ -87,7 +87,8 @@ printlong(DISPLAY *dp) NAMES *np; char buf[20]; - if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) + if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && + (f_longform || f_size)) (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); for (p = dp->list; p; p = p->fts_link) { @@ -198,7 +199,8 @@ printcol(DISPLAY *dp) if (num % numcols) ++numrows; - if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) + if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && + (f_longform || f_size)) (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); for (row = 0; row < numrows; ++row) { for (base = row, col = 0;;) { @@ -271,7 +273,8 @@ printacol(DISPLAY *dp) if ( (colwidth = compute_columns(dp, &numcols)) == 0) return; - if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) + if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && + (f_longform || f_size)) (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); col = 0; for (p = dp->list; p; p = p->fts_link) { |