Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/stat A new command line option, -q, suppresses *stat...



details:   https://anonhg.NetBSD.org/src/rev/274fa775d829
branches:  trunk
changeset: 533739:274fa775d829
user:      atatat <atatat%NetBSD.org@localhost>
date:      Mon Jul 08 18:48:42 2002 +0000

description:
A new command line option, -q, suppresses *stat(2) failure messages.
Output is now done via stdio, instead of with my stupid homegrown
buffering (I don't even know why I did it that way in the first
place).  Also, from Johan Karlsson <johan%freebsd.org@localhost>, eliminate a
spurious newline if no output was generated, and link stat(1) to
readlink(1) for similarity to OpenBSD.

diffstat:

 usr.bin/stat/Makefile |   2 +
 usr.bin/stat/stat.1   |  15 ++++++-
 usr.bin/stat/stat.c   |  95 +++++++++++++++++++++++++++++++-------------------
 3 files changed, 73 insertions(+), 39 deletions(-)

diffs (truncated from 310 to 300 lines):

diff -r be67f6d9cc36 -r 274fa775d829 usr.bin/stat/Makefile
--- a/usr.bin/stat/Makefile     Mon Jul 08 18:43:54 2002 +0000
+++ b/usr.bin/stat/Makefile     Mon Jul 08 18:48:42 2002 +0000
@@ -1,5 +1,7 @@
 #      $NetBSD
 
 PROG=  stat
+LINKS= ${BINDIR}/stat ${BINDIR}/readlink
+MLINKS=        stat.1 readlink.1
 
 .include <bsd.prog.mk>
diff -r be67f6d9cc36 -r 274fa775d829 usr.bin/stat/stat.1
--- a/usr.bin/stat/stat.1       Mon Jul 08 18:43:54 2002 +0000
+++ b/usr.bin/stat/stat.1       Mon Jul 08 18:48:42 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: stat.1,v 1.4 2002/05/09 17:52:03 atatat Exp $
+.\"    $NetBSD: stat.1,v 1.5 2002/07/08 18:48:42 atatat Exp $
 .\"
 .\" Copyright (c) 2002 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -38,7 +38,8 @@
 .Dt STAT 1
 .Os
 .Sh NAME
-.Nm stat
+.Nm stat ,
+.Nm readlink
 .Nd display file status
 .Sh SYNOPSIS
 .Nm
@@ -52,6 +53,9 @@
 .Oc
 .Op Fl t Ar timefmt
 .Op Ar
+.Nm readlink
+.Op Fl n
+.Op Ar
 .Sh DESCRIPTION
 The
 .Nm
@@ -63,6 +67,13 @@
 .Nm
 displays information about the file descriptor for standard input.
 .Pp
+When invoked as
+.Nm readlink ,
+only the target of the symbolic link is printed.  If the given argument
+is not a symbolic link,
+.Nm readlink
+will print nothing and exit with an error.
+.Pp
 The information displayed is obtained by calling
 .Xr lstat 2
 with the given argument and evaluating the returned structure.
diff -r be67f6d9cc36 -r 274fa775d829 usr.bin/stat/stat.c
--- a/usr.bin/stat/stat.c       Mon Jul 08 18:43:54 2002 +0000
+++ b/usr.bin/stat/stat.c       Mon Jul 08 18:48:42 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stat.c,v 1.3 2002/05/31 16:45:16 atatat Exp $ */
+/*     $NetBSD: stat.c,v 1.4 2002/07/08 18:48:42 atatat Exp $ */
 
 /*
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -38,13 +38,14 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: stat.c,v 1.3 2002/05/31 16:45:16 atatat Exp $");
+__RCSID("$NetBSD: stat.c,v 1.4 2002/07/08 18:48:42 atatat Exp $");
 #endif
 
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <err.h>
 #include <string.h>
 #include <stdio.h>
@@ -129,9 +130,9 @@
 #define SHOW_filename  'N'
 #define SHOW_sizerdev  'Z'
 
-void   usage(void);
+void   usage(const char *);
 void   output(const struct stat *, const char *,
-           const char *, int, int);
+           const char *, int, int, int);
 int    format1(const struct stat *,    /* stat info */
            const char *,               /* the file name */
            const char *, int,          /* the format string itself */
@@ -140,31 +141,45 @@
            int, int);
 
 char *timefmt;
+int linkfail;
 
-#define addchar(b, n, l, c, nl) \
+#define addchar(s, c, nl) \
        do { \
-               if ((*(n)) < (l)) { \
-                       (b)[(*(n))++] = (c); \
-                       (*nl) = ((c) == '\n'); \
-               } \
+               (void)fputc((c), (s)); \
+               (*nl) = ((c) == '\n'); \
        } while (0/*CONSTCOND*/)
 
 int
 main(int argc, char *argv[])
 {
        struct stat st;
-       int ch, rc, errs;
-       int lsF, fmtchar, usestat, fn, nonl;
-       char *statfmt;
+       int ch, rc, errs, am_readlink;
+       int lsF, fmtchar, usestat, fn, nonl, quiet;
+       char *statfmt, *options, *synopsis;
 
+       am_readlink = 0;
        lsF = 0;
        fmtchar = '\0';
        usestat = 0;
        nonl = 0;
+       quiet = 0;
+       linkfail = 0;
        statfmt = NULL;
        timefmt = NULL;
 
-       while ((ch = getopt(argc, argv, "f:FlLnrst:x")) != -1)
+       if (strcmp(getprogname(), "readlink") == 0) {
+               am_readlink = 1;
+               options = "n";
+               synopsis = "[-n] [file ...]";
+               statfmt = "%Y";
+               fmtchar = 'f';
+               quiet = 1;
+       } else {
+               options = "f:FlLnqrst:x";
+               synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
+       }
+
+       while ((ch = getopt(argc, argv, options)) != -1)
                switch (ch) {
                case 'F':
                        lsF = 1;
@@ -175,6 +190,9 @@
                case 'n':
                        nonl = 1;
                        break;
+               case 'q':
+                       quiet = 1;
+                       break;
                case 'f':
                        statfmt = optarg;
                        /* FALLTHROUGH */
@@ -191,7 +209,7 @@
                        timefmt = optarg;
                        break;
                default:
-                       usage();
+                       usage(synopsis);
                }
 
        argc -= optind;
@@ -229,7 +247,7 @@
                        timefmt = "%c";
                break;
        default:
-               usage();
+               usage(synopsis);
                /*NOTREACHED*/
        }
 
@@ -247,26 +265,27 @@
 
                if (rc == -1) {
                        errs = 1;
-                       warn("%s: stat", argc == 0 ? "(stdin)" : argv[0]);
+                       linkfail = 1;
+                       if (!quiet)
+                               warn("%s: stat",
+                                   argc == 0 ? "(stdin)" : argv[0]);
                }
                else
-                       output(&st, argv[0], statfmt, fn, nonl);
+                       output(&st, argv[0], statfmt, fn, nonl, quiet);
 
                argv++;
                argc--;
                fn++;
        } while (argc > 0);
 
-       return (errs);
+       return (am_readlink ? linkfail : errs);
 }
 
 void
-usage(void)
+usage(const char *synopsis)
 {
 
-       (void)fprintf(stderr,
-           "usage: %s [-FlLnrsx] [-f format] [-t timefmt] [file ...]\n",
-           getprogname());
+       (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
        exit(1);
 }
 
@@ -275,22 +294,21 @@
  */
 void
 output(const struct stat *st, const char *file,
-    const char *statfmt, int fn, int nonl)
+    const char *statfmt, int fn, int nonl, int quiet)
 {
        int flags, size, prec, ofmt, hilo, what;
-       char buf[4096], subbuf[MAXPATHLEN];
+       char buf[MAXPATHLEN];
        const char *subfmt;
        int nl, t, i;
-       size_t len;
 
-       len = 0;
+       nl = 1;
        while (*statfmt != '\0') {
 
                /*
                 * Non-format characters go straight out.
                 */
                if (*statfmt != FMT_MAGIC) {
-                       addchar(buf, &len, sizeof(buf), *statfmt, &nl);
+                       addchar(stdout, *statfmt, &nl);
                        statfmt++;
                        continue;
                }
@@ -307,15 +325,15 @@
                 */
                switch (*statfmt) {
                case SIMPLE_NEWLINE:
-                       addchar(buf, &len, sizeof(buf), '\n', &nl);
+                       addchar(stdout, '\n', &nl);
                        statfmt++;
                        continue;
                case SIMPLE_TAB:
-                       addchar(buf, &len, sizeof(buf), '\t', &nl);
+                       addchar(stdout, '\t', &nl);
                        statfmt++;
                        continue;
                case SIMPLE_PERCENT:
-                       addchar(buf, &len, sizeof(buf), '%', &nl);
+                       addchar(stdout, '%', &nl);
                        statfmt++;
                        continue;
                case SIMPLE_NUMBER: {
@@ -323,7 +341,7 @@
 
                        snprintf(num, sizeof(num), "%d", fn);
                        for (p = &num[0]; *p; p++)
-                               addchar(buf, &len, sizeof(buf), *p, &nl);
+                               addchar(stdout, *p, &nl);
                        statfmt++;
                        continue;
                }
@@ -442,11 +460,11 @@
                t = format1(st,
                     file,
                     subfmt, statfmt - subfmt,
-                    subbuf, sizeof(subbuf),
+                    buf, sizeof(buf),
                     flags, size, prec, ofmt, hilo, what);
 
-               for (i = 0; i < t && i < sizeof(subbuf); i++)
-                       addchar(buf, &len, sizeof(buf), subbuf[i], &nl);
+               for (i = 0; i < t && i < sizeof(buf); i++)
+                       addchar(stdout, buf[i], &nl);
 
                continue;
 
@@ -455,9 +473,9 @@
                    (int)(statfmt - subfmt + 1), subfmt);
        }
 
-       (void)write(STDOUT_FILENO, buf, len);
        if (!nl && !nonl)
-               (void)write(STDOUT_FILENO, "\n", sizeof("\n") - 1);
+               (void)fputc('\n', stdout);
+       (void)fflush(stdout);
 }
 
 /*
@@ -659,14 +677,17 @@
                        snprintf(path, sizeof(path), " -> ");
                        l = readlink(file, path + 4, sizeof(path) - 4);
                        if (l == -1) {
+                               linkfail = 1;
                                l = 0;
                                path[0] = '\0';
                        }
                        path[l + 4] = '\0';



Home | Main Index | Thread Index | Old Index