Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/fstat Add -O to print offsets; align columns properly.



details:   https://anonhg.NetBSD.org/src/rev/47aa76bd778f
branches:  trunk
changeset: 965212:47aa76bd778f
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Sep 06 17:08:22 2019 +0000

description:
Add -O to print offsets; align columns properly.

diffstat:

 usr.bin/fstat/fstat.1 |  37 ++++++++++++-------
 usr.bin/fstat/fstat.c |  97 ++++++++++++++++++++++++++++++++------------------
 usr.bin/fstat/fstat.h |   3 +-
 usr.bin/fstat/misc.c  |  46 +++++++++++++----------
 4 files changed, 112 insertions(+), 71 deletions(-)

diffs (truncated from 424 to 300 lines):

diff -r 54b99d0296bf -r 47aa76bd778f usr.bin/fstat/fstat.1
--- a/usr.bin/fstat/fstat.1     Fri Sep 06 16:28:53 2019 +0000
+++ b/usr.bin/fstat/fstat.1     Fri Sep 06 17:08:22 2019 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: fstat.1,v 1.34 2017/07/03 21:34:57 wiz Exp $
+.\"    $NetBSD: fstat.1,v 1.35 2019/09/06 17:08:22 christos Exp $
 .\"
 .\" Copyright (c) 1987, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     from: @(#)fstat.1      8.3 (Berkeley) 2/25/94
 .\"
-.Dd December 15, 2013
+.Dd September 6, 2019
 .Dt FSTAT 1
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd display status of open files
 .Sh SYNOPSIS
 .Nm
-.Op Fl Afnv
+.Op Fl AfnOv
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Op Fl p Ar pid
@@ -54,7 +54,7 @@
 reports on all open files in the system.
 .Pp
 Options:
-.Bl -tag -width Ds
+.Bl -tag -width XXXXXXXXXX
 .It Fl A
 Add an output column with the address of the kernel object (vnode or file),
 that can be matched with
@@ -72,12 +72,15 @@
 Please see the
 .Sx BUGS
 section for issues with this option.
-.It Fl M
-Extract values associated with the name list from the specified core
+.It Fl M Ar core
+Extract values associated with the name list from the specified
+.Ar core
 instead of the default
 .Pa /dev/kmem .
-.It Fl N
-Extract the name list from the specified system instead of the default
+.It Fl N Ar system
+Extract the name list from the specified
+.Ar system
+instead of the default
 .Pa /netbsd .
 .It Fl n
 Numerical format.
@@ -88,10 +91,14 @@
 in
 .Pa /dev ;
 and print the mode of the file in octal instead of symbolic form.
-.It Fl p
-Report all files open by the specified process.
-.It Fl u
-Report all files open by the specified user.
+.It Fl O
+Report file offsets instead of sizes.
+.It Fl p Ar process
+Report all files open by the specified
+.Ar process .
+.It Fl u Ar user
+Report all files open by the specified
+.Ar user .
 .It Fl v
 Verbose mode.
 Print error messages upon failures to locate particular
@@ -161,9 +168,11 @@
 .Xr strmode 3 ) ;
 otherwise, the mode is printed
 as an octal number.
-.It Li SZ\&|DV
+.It Li SZ\&|DV or OFFS
 If the file is not a character or block special file, prints the size of
-the file in bytes.
+the file in bytes (or the offset if the
+.Fl O
+is specified).
 Otherwise, if the
 .Fl n
 flag is not specified, prints
diff -r 54b99d0296bf -r 47aa76bd778f usr.bin/fstat/fstat.c
--- a/usr.bin/fstat/fstat.c     Fri Sep 06 16:28:53 2019 +0000
+++ b/usr.bin/fstat/fstat.c     Fri Sep 06 17:08:22 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fstat.c,v 1.112 2019/02/03 03:19:29 mrg Exp $  */
+/*     $NetBSD: fstat.c,v 1.113 2019/09/06 17:08:22 christos Exp $     */
 
 /*-
  * Copyright (c) 1988, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)fstat.c    8.3 (Berkeley) 5/2/95";
 #else
-__RCSID("$NetBSD: fstat.c,v 1.112 2019/02/03 03:19:29 mrg Exp $");
+__RCSID("$NetBSD: fstat.c,v 1.113 2019/09/06 17:08:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -134,6 +134,7 @@
 static int     checkfile; /* true if restricting to particular files or filesystems */
 static int     nflg;   /* (numerical) display f.s. and rdev as dev_t */
 static int     Aflg;   /* prefix with address of file structure */
+static int     Oflg;   /* print offset instead of size */
 int    vflg;   /* display errors in locating kernel data objects etc... */
 
 static fdfile_t **ofiles; /* buffer of pointers to file structures */
@@ -171,12 +172,12 @@
     uint16_t, bool);
 #endif
 static const char *at_addrstr(char *, size_t, const struct sockaddr_at *);
-static void    socktrans(struct socket *, int);
+static void    socktrans(struct file *, struct socket *, int);
 static void    misctrans(struct file *, int);
 static int     ufs_filestat(struct vnode *, struct filestat *);
 static void    usage(void) __dead;
 static const char   *vfilestat(struct vnode *, struct filestat *);
-static void    vtrans(struct vnode *, int, int, long);
+static void    vtrans(struct file *, struct vnode *, int, int, long);
 static void    ftrans(fdfile_t *, int);
 static void    ptrans(struct file *, struct pipe *, int);
 static void    kdriver_init(void);
@@ -196,7 +197,7 @@
        arg = 0;
        what = KERN_PROC_ALL;
        nlistf = memf = NULL;
-       while ((ch = getopt(argc, argv, "fnAp:u:vN:M:")) != -1)
+       while ((ch = getopt(argc, argv, "fnAOp:u:vN:M:")) != -1)
                switch((char)ch) {
                case 'f':
                        fsflg = 1;
@@ -213,6 +214,9 @@
                case 'A':
                        Aflg = 1;
                        break;
+               case 'O':
+                       Oflg = 1;
+                       break;
                case 'p':
                        if (pflg++)
                                usage();
@@ -284,11 +288,14 @@
        if (Aflg)
                (void)printf("%-*s ", 2*(int)(sizeof(void*)), "ADDR");
        if (nflg)
-               (void)printf("%s",
-"USER     CMD          PID   FD  DEV     INUM  MODE  SZ|DV R/W");
+               (void)printf(
+"USER     CMD          PID   FD    DEV       INUM   MODE  %s R/W",
+    Oflg ? " OFFS" : "SZ|DV" );
        else
-               (void)printf("%s",
-"USER     CMD          PID   FD MOUNT       INUM MODE         SZ|DV R/W");
+               (void)printf(
+"USER     CMD          PID   FD  MOUNT         INUM MODE         %s R/W",
+    Oflg ? " OFFS" : "SZ|DV" );
+
        if (checkfile && fsflg == 0)
                (void)printf(" NAME\n");
        else
@@ -450,11 +457,11 @@
         * root directory vnode, if one
         */
        if (cwdi.cwdi_rdir)
-               vtrans(cwdi.cwdi_rdir, RDIR, FREAD, (long)cwdi.cwdi_rdir);
+               vtrans(NULL, cwdi.cwdi_rdir, RDIR, FREAD, (long)cwdi.cwdi_rdir);
        /*
         * current working directory vnode
         */
-       vtrans(cwdi.cwdi_cdir, CDIR, FREAD, (long)cwdi.cwdi_cdir);
+       vtrans(NULL, cwdi.cwdi_cdir, CDIR, FREAD, (long)cwdi.cwdi_cdir);
 #if 0
        /*
         * Disable for now, since p->p_tracep appears to point to a ktr_desc *
@@ -507,10 +514,10 @@
                        2*(int)(sizeof(void*)), (long)fdfile.ff_file);
        switch (file.f_type) {
        case DTYPE_VNODE:
-               vtrans(file.f_data, i, file.f_flag, (long)fdfile.ff_file);
+               vtrans(&file, file.f_data, i, file.f_flag, (long)fdfile.ff_file);
                break;
        case DTYPE_SOCKET:
-               socktrans(file.f_data, i);
+               socktrans(&file, file.f_data, i);
                break;
        case DTYPE_PIPE:
                if (checkfile == 0)
@@ -634,7 +641,37 @@
 }
 
 static void
-vtrans(struct vnode *vp, int i, int flag, long addr)
+vprint(struct vnode *vn, struct filestat *fst)
+{
+       switch (vn->v_type) {
+       case VBLK:
+       case VCHR: {
+               const char *name;
+
+               if (nflg || ((name = devname(fst->rdev, vn->v_type == VCHR ? 
+                   S_IFCHR : S_IFBLK)) == NULL))
+                       (void)printf("  %s,%-2llu",
+                           kdriver_search(vn->v_type, major(fst->rdev)),
+                           (unsigned long long)minor(fst->rdev));
+               else
+                       (void)printf(" %6s", name);
+               break;
+       }
+       default:
+               (void)printf(" %6lld", (long long)fst->size);
+       }
+}
+
+void
+oprint(struct file *fp, const char *str)
+{
+       if (Oflg)
+               (void)printf(" %6lld", (long long)(fp ? fp->f_offset : 0));
+       fputs(str, stdout);
+}
+
+static void
+vtrans(struct file *fp, struct vnode *vp, int i, int flag, long addr)
 {
        struct vnode vn;
        char mode[15], rw[3];
@@ -658,32 +695,20 @@
                return;
        }
        if (nflg)
-               (void)printf("  %2llu,%-2llu",
+               (void)printf("  %3llu,%-2llu",
                    (unsigned long long)major(fst.fsid),
                    (unsigned long long)minor(fst.fsid));
        else
                (void)printf("  %-8s", getmnton(vn.v_mount));
        if (nflg)
-               (void)snprintf(mode, sizeof mode, "%o", fst.mode);
+               (void)snprintf(mode, sizeof mode, "%6o", fst.mode);
        else
                strmode(fst.mode, mode);
-       (void)printf("  %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
-       switch (vn.v_type) {
-       case VBLK:
-       case VCHR: {
-               char *name;
-
-               if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 
-                   S_IFCHR : S_IFBLK)) == NULL))
-                       (void)printf("  %s,%-2llu",
-                           kdriver_search(vn.v_type, major(fst.rdev)),
-                           (unsigned long long)minor(fst.rdev));
-               else
-                       (void)printf(" %6s", name);
-               break;
-       }
-       default:
-               (void)printf(" %6lld", (long long)fst.size);
+       (void)printf("  %8"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
+       if (Oflg) {
+               oprint(fp, "");
+       } else {
+               vprint(&vn, &fst);
        }
        rw[0] = '\0';
        if (flag & FREAD)
@@ -996,7 +1021,7 @@
 }
 
 static void
-socktrans(struct socket *sock, int i)
+socktrans(struct file *f, struct socket *sock, int i)
 {
        static const char *stypename[] = {
                "unused",       /* 0 */
@@ -1209,7 +1234,7 @@
                    lbuf);
        else if (so.so_pcb)
                printf(" %jx", (uintmax_t)(uintptr_t)so.so_pcb);
-       (void)printf("\n");
+       oprint(f, "\n");
        return;
 bad:
        (void)printf("* error\n");
@@ -1235,7 +1260,7 @@
                (fp->f_flag & FWRITE) ? "w" : "r",
                (fp->f_flag & FNONBLOCK) ? "n" : "",
                (cp.pipe_state & PIPE_ASYNC) ? "a" : "");
-       (void)printf("\n");
+       oprint(fp, "\n");
        return;
 bad:



Home | Main Index | Thread Index | Old Index