Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/stat Some corrections to stat to make it more reason...



details:   https://anonhg.NetBSD.org/src/rev/63e0f415b053
branches:  trunk
changeset: 367688:63e0f415b053
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Jun 22 18:20:30 2022 +0000

description:
Some corrections to stat to make it more reasonable, and comply with
the man page, in particular, in one of the EXAMPLES it is claimed:

     This example produces output very similar to that from find ... -ls
     (except that find(1) displays the time in a different format, and find(1)
     sometimes adds one or more spaces after the comma in “major,minor” for
     device nodes):

and then uses %9Z in the format, but conveniently does not use a
device file to demonstrate.

If it had, the result would have been:

   8033      0 brw-r-----    1 root              operator                  4,       51 Jun  8 02:44:45 2022 /dev/sd3

Note the spaces which were claimed to exist (sometimes) in find(1) output
but not with stat.   Oops.

Omitting those spaces seems like a good idea, so does using the same
field width for the result in both cases, rather than producing 9 chars
in the ordinary file case, and 19 (2 * 9 + 1) in the device case (for %9Z).

Now we will get:

   8033      0 brw-r-----    1 root              operator             4,51   Jun  8 02:44:45 2022 /dev/sd3

While here, also make the file number (%@) when reading stdin be 0, it is
not the 1st file in the arg list.

Some (very minor) KNF applied at the same time.

In stat.1 make these changes explicit.

diffstat:

 usr.bin/stat/stat.1 |  13 +++++++++++--
 usr.bin/stat/stat.c |  21 +++++++++++----------
 2 files changed, 22 insertions(+), 12 deletions(-)

diffs (111 lines):

diff -r c72b2de059f0 -r 63e0f415b053 usr.bin/stat/stat.1
--- a/usr.bin/stat/stat.1       Wed Jun 22 18:02:43 2022 +0000
+++ b/usr.bin/stat/stat.1       Wed Jun 22 18:20:30 2022 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: stat.1,v 1.41 2022/06/22 18:02:43 kre Exp $
+.\"    $NetBSD: stat.1,v 1.42 2022/06/22 18:20:30 kre Exp $
 .\"
 .\" Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -76,6 +76,9 @@
 .Sq path name )
 in this case is
 .Dq \&(stdin) .
+The file number
+.Pq Dq %@
+will be zero.
 .Pp
 Otherwise the information displayed is obtained by calling
 .Xr lstat 2
@@ -487,11 +490,17 @@
 .Fa st_rdev
 field for character or block special devices
 (that is,
-.Dq %Hr,%Lr )
+.Dq %Hr,%-Lr )
 and gives size output
 .Pq Fa st_size
 .Pq Dq %z
 for all others.
+A specified field width applies to the overall result
+(approximately half each for the two device file sub-fields),
+but precision, output format, and flags are used separately for
+each conversion made (but note the
+.Sq Fl
+in the %Lr conversion.)
 .El
 .El
 .Pp
diff -r c72b2de059f0 -r 63e0f415b053 usr.bin/stat/stat.c
--- a/usr.bin/stat/stat.c       Wed Jun 22 18:02:43 2022 +0000
+++ b/usr.bin/stat/stat.c       Wed Jun 22 18:20:30 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stat.c,v 1.47 2021/08/27 18:11:07 rillig Exp $ */
+/*     $NetBSD: stat.c,v 1.48 2022/06/22 18:20:30 kre Exp $ */
 
 /*
  * Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: stat.c,v 1.47 2021/08/27 18:11:07 rillig Exp $");
+__RCSID("$NetBSD: stat.c,v 1.48 2022/06/22 18:20:30 kre Exp $");
 #endif
 
 #if ! HAVE_NBTOOL_CONFIG_H
@@ -322,9 +322,10 @@
 
        errs = 0;
        do {
-               if (argc == 0)
+               if (argc == 0) {
+                       fn = 0;
                        rc = fstat(STDIN_FILENO, &st);
-               else if (usestat) {
+               } else if (usestat) {
                        /*
                         * Try stat() and if it fails, fall back to
                         * lstat() just in case we're examining a
@@ -334,8 +335,7 @@
                            errno == ENOENT &&
                            (rc = lstat(argv[0], &st)) == -1)
                                errno = ENOENT;
-               }
-               else
+               } else
                        rc = lstat(argv[0], &st);
 
                if (rc == -1) {
@@ -345,8 +345,7 @@
                                warn("%s: %s",
                                    argc == 0 ? "(stdin)" : argv[0],
                                    usestat ? "stat" : "lstat");
-               }
-               else
+               } else
                        output(&st, argv[0], statfmt, fn, nonl, quiet);
 
                argv++;
@@ -1030,17 +1029,19 @@
                        char majdev[20], mindev[20];
                        int l1, l2;
 
+                       if (size == 0)          /* avoid -1/2 */
+                               size++;         /* 1/2 == 0/2 so this is safe */
                        l1 = format1(st,
                            file,
                            fmt, flen,
                            majdev, sizeof(majdev),
-                           flags, size, prec,
+                           flags, (size - 1) / 2, prec,
                            ofmt, HIGH_PIECE, SHOW_st_rdev, quiet);
                        l2 = format1(st,
                            file,
                            fmt, flen,
                            mindev, sizeof(mindev),
-                           flags, size, prec,
+                           flags | FLAG_MINUS , size / 2, prec,
                            ofmt, LOW_PIECE, SHOW_st_rdev, quiet);
                        return (snprintf(buf, blen, "%.*s,%.*s",
                            l1, majdev, l2, mindev));


Home | Main Index | Thread Index | Old Index