Module Name:    src
Committed By:   kre
Date:           Wed Jun 22 18:20:30 UTC 2022

Modified Files:
        src/usr.bin/stat: stat.1 stat.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/usr.bin/stat/stat.1
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/stat/stat.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/stat/stat.1
diff -u src/usr.bin/stat/stat.1:1.41 src/usr.bin/stat/stat.1:1.42
--- src/usr.bin/stat/stat.1:1.41	Wed Jun 22 18:02:43 2022
+++ src/usr.bin/stat/stat.1	Wed Jun 22 18:20:30 2022
@@ -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 @@ The
 .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 @@ from the
 .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

Index: src/usr.bin/stat/stat.c
diff -u src/usr.bin/stat/stat.c:1.47 src/usr.bin/stat/stat.c:1.48
--- src/usr.bin/stat/stat.c:1.47	Fri Aug 27 18:11:07 2021
+++ src/usr.bin/stat/stat.c	Wed Jun 22 18:20:30 2022
@@ -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 @@ main(int argc, char *argv[])
 
 	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 @@ main(int argc, char *argv[])
 			    errno == ENOENT &&
 			    (rc = lstat(argv[0], &st)) == -1)
 				errno = ENOENT;
-		}
-		else
+		} else
 			rc = lstat(argv[0], &st);
 
 		if (rc == -1) {
@@ -345,8 +345,7 @@ main(int argc, char *argv[])
 				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 @@ format1(const struct stat *st,
 			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));

Reply via email to