Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/stat Add a simple format that translates to the curr...



details:   https://anonhg.NetBSD.org/src/rev/e44986a67726
branches:  trunk
changeset: 526755:e44986a67726
user:      atatat <atatat%NetBSD.org@localhost>
date:      Thu May 09 17:52:03 2002 +0000

description:
Add a simple format that translates to the current file number, fix an
off by one error, and #define a bunch more things to make it clearer.

diffstat:

 usr.bin/stat/stat.1 |  22 +++++++++----
 usr.bin/stat/stat.c |  87 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 69 insertions(+), 40 deletions(-)

diffs (276 lines):

diff -r 066f01b48a17 -r e44986a67726 usr.bin/stat/stat.1
--- a/usr.bin/stat/stat.1       Thu May 09 17:12:36 2002 +0000
+++ b/usr.bin/stat/stat.1       Thu May 09 17:52:03 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: stat.1,v 1.3 2002/04/28 17:44:43 wiz Exp $
+.\"    $NetBSD: stat.1,v 1.4 2002/05/09 17:52:03 atatat Exp $
 .\"
 .\" Copyright (c) 2002 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -126,10 +126,12 @@
 is immediately followed by one of
 .Cm n ,
 .Cm t ,
+.Cm % ,
 or
-.Cm % ,
-then a newline character, a tab character, or a percent character is
-printed, otherwise the string is examined for the following:
+.Cm @ ,
+then a newline character, a tab character, a percent character,
+or the current file number is printed, otherwise the string is
+examined for the following:
 .Pp
 Any of the following optional flags:
 .Bl -tag -width Ds
@@ -225,7 +227,9 @@
 .Cm r
 or
 .Cm d ,
-the high bits for permissions from the string form of
+the ``user'' bits for permissions from the string form of
+.Cm p ,
+the file ``type'' bits from the numeric forms of
 .Cm p ,
 and the long output form of
 .Cm T .
@@ -234,7 +238,9 @@
 .Cm r
 or
 .Cm d ,
-the low bits for permissions from the string form of
+the ``other'' bits for permissions from the string form of
+.Cm p ,
+the ``user'', ``group'', and ``other'' bits from the numeric forms of
 .Cm p ,
 and the
 .Ic ls -F
@@ -244,8 +250,10 @@
 .Cm L
 for this is optional).
 .It Cm M
-``Middle'' -- specifies the middle bits for permissions from the
+``Middle'' -- specifies the ``group'' bits for permissions from the
 string output form of
+.Cm p ,
+or the ``suid'', ``sgid'', and ``sticky'' bits for the numeric forms of
 .Cm p .
 .El
 .It Cm datum
diff -r 066f01b48a17 -r e44986a67726 usr.bin/stat/stat.c
--- a/usr.bin/stat/stat.c       Thu May 09 17:12:36 2002 +0000
+++ b/usr.bin/stat/stat.c       Thu May 09 17:52:03 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stat.c,v 1.1 2002/04/27 16:37:19 atatat Exp $ */
+/*     $NetBSD: stat.c,v 1.2 2002/05/09 17:52:03 atatat Exp $ */
 
 /*
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: stat.c,v 1.1 2002/04/27 16:37:19 atatat Exp $");
+__RCSID("$NetBSD: stat.c,v 1.2 2002/05/09 17:52:03 atatat Exp $");
 #endif
 
 #include <sys/types.h>
@@ -75,18 +75,28 @@
 
 #define TIME_FORMAT    "%b %e %T %Y"
 
-#define FMT_POUND      0x01
-#define FMT_SPACE      0x02
-#define FMT_PLUS       0x04
-#define FMT_ZERO       0x08
-#define FMT_MINUS      0x10
+#define FLAG_POUND     0x01
+#define FLAG_SPACE     0x02
+#define FLAG_PLUS      0x04
+#define FLAG_ZERO      0x08
+#define FLAG_MINUS     0x10
 
 /*
- * These format characters must all be unique.
+ * These format characters must all be unique, except the magic one.
  */
+#define FMT_MAGIC      '%'
+#define FMT_DOT                '.'
+
 #define SIMPLE_NEWLINE 'n'
 #define SIMPLE_TAB     't'
 #define SIMPLE_PERCENT '%'
+#define SIMPLE_NUMBER  '@'
+
+#define FMT_POUND      '#'
+#define FMT_SPACE      ' '
+#define FMT_PLUS       '+'
+#define FMT_ZERO       '0'
+#define FMT_MINUS      '-'
 
 #define FMT_DECIMAL    'D'
 #define FMT_OCTAL      'O'
@@ -121,7 +131,7 @@
 
 void   usage(void);
 void   output(const struct stat *, const char *,
-           const char *, int);
+           const char *, int, int);
 int    format1(const struct stat *,    /* stat info */
            const char *,               /* the file name */
            const char *, int,          /* the format string itself */
@@ -144,7 +154,7 @@
 {
        struct stat st;
        int ch, rc, errs;
-       int lsF, fmtchar, usestat, nonl;
+       int lsF, fmtchar, usestat, fn, nonl;
        char *statfmt;
 
        lsF = 0;
@@ -186,6 +196,7 @@
 
        argc -= optind;
        argv += optind;
+       fn = 1;
 
        if (fmtchar == '\0') {
                if (lsF)
@@ -239,10 +250,11 @@
                        warn("%s: stat", argc == 0 ? "(stdin)" : argv[0]);
                }
                else
-                       output(&st, argv[0], statfmt, nonl);
+                       output(&st, argv[0], statfmt, fn, nonl);
 
                argv++;
                argc--;
+               fn++;
        } while (argc > 0);
 
        return (errs);
@@ -263,7 +275,7 @@
  */
 void
 output(const struct stat *st, const char *file,
-    const char *statfmt, int nonl)
+    const char *statfmt, int fn, int nonl)
 {
        int flags, size, prec, ofmt, hilo, what;
        char buf[4096], subbuf[MAXPATHLEN];
@@ -277,7 +289,7 @@
                /*
                 * Non-format characters go straight out.
                 */
-               if (*statfmt != '%') {
+               if (*statfmt != FMT_MAGIC) {
                        addchar(buf, &len, sizeof(buf), *statfmt, &nl);
                        statfmt++;
                        continue;
@@ -285,7 +297,7 @@
 
                /*
                 * The current format "substring" starts here,
-                * and then we skip the '%'.
+                * and then we skip the magic.
                 */
                subfmt = statfmt;
                statfmt++;
@@ -306,6 +318,15 @@
                        addchar(buf, &len, sizeof(buf), '%', &nl);
                        statfmt++;
                        continue;
+               case SIMPLE_NUMBER: {
+                       char num[12], *p;
+
+                       snprintf(num, sizeof(num), "%d", fn);
+                       for (p = &num[0]; *p; p++)
+                               addchar(buf, &len, sizeof(buf), *p, &nl);
+                       statfmt++;
+                       continue;
+               }
                }
 
                /*
@@ -332,16 +353,16 @@
                 */
                flags = 0;
                do {
-                       if      (*statfmt == '#')
-                               flags |= FMT_POUND;
-                       else if (*statfmt == ' ')
-                               flags |= FMT_SPACE;
-                       else if (*statfmt == '+')
-                               flags |= FMT_PLUS;
-                       else if (*statfmt == '0')
-                               flags |= FMT_ZERO;
-                       else if (*statfmt == '-')
-                               flags |= FMT_MINUS;
+                       if      (*statfmt == FMT_POUND)
+                               flags |= FLAG_POUND;
+                       else if (*statfmt == FMT_SPACE)
+                               flags |= FLAG_SPACE;
+                       else if (*statfmt == FMT_PLUS)
+                               flags |= FLAG_PLUS;
+                       else if (*statfmt == FMT_ZERO)
+                               flags |= FLAG_ZERO;
+                       else if (*statfmt == FMT_MINUS)
+                               flags |= FLAG_MINUS;
                        else
                                break;
                        statfmt++;
@@ -359,7 +380,7 @@
                }
 
                prec = -1;
-               if (*statfmt == '.') {
+               if (*statfmt == FMT_DOT) {
                        statfmt++;
 
                        prec = 0;
@@ -436,7 +457,7 @@
 
        (void)write(STDOUT_FILENO, buf, len);
        if (!nl && !nonl)
-               (void)write(STDOUT_FILENO, "\n", sizeof("\n"));
+               (void)write(STDOUT_FILENO, "\n", sizeof("\n") - 1);
 }
 
 /*
@@ -515,13 +536,13 @@
                        hilo = 0;
                }
                else if (hilo == MIDDLE_PIECE) {
-                       data &= 07777;
+                       data = (data >> 9) & 07;
                        sdata += 4;
                        sdata[3] = '\0';
                        hilo = 0;
                }
                else if (hilo == LOW_PIECE) {
-                       data &= 07777;
+                       data &= 0777;
                        sdata += 7;
                        sdata[3] = '\0';
                        hilo = 0;
@@ -743,15 +764,15 @@
         */
        lfmt[0] = '\0';
        (void)strcat(lfmt, "%");
-       if (flags & FMT_POUND)
+       if (flags & FLAG_POUND)
                (void)strcat(lfmt, "#");
-       if (flags & FMT_SPACE)
+       if (flags & FLAG_SPACE)
                (void)strcat(lfmt, " ");
-       if (flags & FMT_PLUS)
+       if (flags & FLAG_PLUS)
                (void)strcat(lfmt, "+");
-       if (flags & FMT_MINUS)
+       if (flags & FLAG_MINUS)
                (void)strcat(lfmt, "-");
-       if (flags & FMT_ZERO)
+       if (flags & FLAG_ZERO)
                (void)strcat(lfmt, "0");
 
        /*



Home | Main Index | Thread Index | Old Index