Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/pstat Make flag bit to character conversion table d...



details:   https://anonhg.NetBSD.org/src/rev/07b314dba4e7
branches:  trunk
changeset: 522624:07b314dba4e7
user:      enami <enami%NetBSD.org@localhost>
date:      Sat Feb 23 01:06:41 2002 +0000

description:
Make flag bit to character conversion table driven.

diffstat:

 usr.sbin/pstat/pstat.c |  285 +++++++++++++++++++++++-------------------------
 1 files changed, 137 insertions(+), 148 deletions(-)

diffs (truncated from 469 to 300 lines):

diff -r f9453a48edd8 -r 07b314dba4e7 usr.sbin/pstat/pstat.c
--- a/usr.sbin/pstat/pstat.c    Fri Feb 22 21:26:47 2002 +0000
+++ b/usr.sbin/pstat/pstat.c    Sat Feb 23 01:06:41 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pstat.c,v 1.70 2002/02/22 11:25:37 enami Exp $ */
+/*     $NetBSD: pstat.c,v 1.71 2002/02/23 01:06:41 enami Exp $ */
 
 /*-
  * Copyright (c) 1980, 1991, 1993, 1994
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)pstat.c    8.16 (Berkeley) 5/9/95";
 #else
-__RCSID("$NetBSD: pstat.c,v 1.70 2002/02/22 11:25:37 enami Exp $");
+__RCSID("$NetBSD: pstat.c,v 1.71 2002/02/23 01:06:41 enami Exp $");
 #endif
 #endif /* not lint */
 
@@ -111,7 +111,7 @@
 kvm_t  *kd;
 
 struct {
-       int m_flag;
+       u_int m_flag;
        const char *m_name;
 } mnt_flags[] = {
        { MNT_RDONLY, "rdonly" },
@@ -146,6 +146,11 @@
        { 0 }
 };
 
+struct flagbit_desc {
+       u_int fd_flags;
+       char fd_mark;
+};
+
 #define        SVAR(var) __STRING(var) /* to force expansion */
 #define        KGET(idx, var)                                                  \
        KGET1(idx, &var, sizeof(var), SVAR(var))
@@ -182,6 +187,7 @@
 
 void   filemode __P((void));
 int    getfiles __P((char **, int *));
+int    getflags __P((const struct flagbit_desc *, char *, u_int));
 struct mount *
        getmnt __P((struct mount *));
 char * kinfo_vnodes __P((int *));
@@ -194,7 +200,6 @@
 int    nfs_print __P((struct vnode *, int));
 void   ttymode __P((void));
 void   ttyprt __P((struct tty *));
-void   ufs_getflags __P((struct vnode *, struct inode *, char *));
 void   ufs_header __P((void));
 int    ufs_print __P((struct vnode *, int));
 int    ext2fs_print __P((struct vnode *, int));
@@ -291,12 +296,14 @@
                ttymode();
        if (swapflag || totalflag)
                list_swap(0, kflag, 0, totalflag, 1);
-       exit (0);
+       exit(0);
 }
 
 #define        VPTRSZ  sizeof(struct vnode *)
 #define        VNODESZ sizeof(struct vnode)
-#define        PTRSTRWIDTH ((int)sizeof(void *) * 2)
+#define        PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string
+                                                when pointer is printed
+                                                in hexadecimal. */
 
 void
 vnodemode()
@@ -365,6 +372,43 @@
        free(e_vnodebase);
 }
 
+int
+getflags(fd, p, flags)
+       const struct flagbit_desc *fd;
+       char *p;
+       u_int flags;
+{
+       char *q = p;
+
+       if (flags == 0) {
+               *p++ = '-';
+               *p = '\0';
+               return (0);
+       }
+
+       for (; fd->fd_flags != 0; fd++)
+               if ((flags & fd->fd_flags) != 0)
+                       *p++ = fd->fd_mark;
+       *p = '\0';
+       return (p - q);
+}
+
+const struct flagbit_desc vnode_flags[] = {
+       { VROOT,        'R' },
+       { VTEXT,        'T' },
+       { VSYSTEM,      'S' },
+       { VISTTY,       'I' },
+       { VEXECMAP,     'E' },
+       { VXLOCK,       'L' },
+       { VXWANT,       'W' },
+       { VBWAIT,       'B' },
+       { VALIASED,     'A' },
+       { VDIROP,       'D' },
+       { VLAYER,       'Y' },
+       { VONWORKLST,   'O' },
+       { 0,            '\0' },
+};
+
 void
 vnode_header()
 {
@@ -378,9 +422,8 @@
        struct vnode *avnode;
        struct vnode *vp;
 {
-       char *type, flags[16];
-       char *fp = flags;
-       int flag, ovflw;
+       char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])];
+       int ovflw;
 
        /*
         * set type
@@ -410,34 +453,7 @@
        /*
         * gather flags
         */
-       flag = vp->v_flag;
-       if (flag & VROOT)
-               *fp++ = 'R';
-       if (flag & VTEXT)
-               *fp++ = 'T';
-       if (flag & VSYSTEM)
-               *fp++ = 'S';
-       if (flag & VISTTY)
-               *fp++ = 'I';
-       if (flag & VEXECMAP)
-               *fp++ = 'E';
-       if (flag & VXLOCK)
-               *fp++ = 'L';
-       if (flag & VXWANT)
-               *fp++ = 'W';
-       if (flag & VBWAIT)
-               *fp++ = 'B';
-       if (flag & VALIASED)
-               *fp++ = 'A';
-       if (flag & VDIROP)
-               *fp++ = 'D';
-       if (flag & VLAYER)
-               *fp++ = 'Y';
-       if (flag & VONWORKLST)
-               *fp++ = 'O';
-       if (flag == 0)
-               *fp++ = '-';
-       *fp = '\0';
+       (void)getflags(vnode_flags, flags, vp->v_flag);
 
        ovflw = 0;
        PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode);
@@ -450,45 +466,20 @@
        return (ovflw);
 }
 
-void
-ufs_getflags(vp, ip, flags)
-       struct vnode *vp;
-       struct inode *ip;
-       char *flags;
-{
-       int flag;
-
-       /*
-        * XXX need to to locking state.
-        */
-
-       flag = ip->i_flag;
-       if (flag & IN_ACCESS)
-               *flags++ = 'A';
-       if (flag & IN_CHANGE)
-               *flags++ = 'C';
-       if (flag & IN_UPDATE)
-               *flags++ = 'U';
-       if (flag & IN_MODIFIED)
-               *flags++ = 'M';
-       if (flag & IN_ACCESSED)
-               *flags++ = 'a';
-       if (flag & IN_RENAME)
-               *flags++ = 'R';
-       if (flag & IN_SHLOCK)
-               *flags++ = 'S';
-       if (flag & IN_EXLOCK)
-               *flags++ = 'E';
-       if (flag & IN_CLEANING)
-               *flags++ = 'c';
-       if (flag & IN_ADIROP)
-               *flags++ = 'D';
-       if (flag & IN_SPACECOUNTED)
-               *flags++ = 's';
-       if (flag == 0)
-               *flags++ = '-';
-       *flags = '\0';
-}
+const struct flagbit_desc ufs_flags[] = {
+       { IN_ACCESS,    'A' },
+       { IN_CHANGE,    'C' },
+       { IN_UPDATE,    'U' },
+       { IN_MODIFIED,  'M' },
+       { IN_ACCESSED,  'a' },
+       { IN_RENAME,    'R' },
+       { IN_SHLOCK,    'S' },
+       { IN_EXLOCK,    'E' },
+       { IN_CLEANING,  'c' },
+       { IN_ADIROP,    'D' },
+       { IN_SPACECOUNTED, 's' },
+       { 0,            '\0' },
+};
 
 void
 ufs_header()
@@ -503,20 +494,27 @@
        int ovflw;
 {
        struct inode inode, *ip = &inode;
-       char buf[16], *name;
+       char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
+       char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
+       char *name;
        mode_t type;
 
        KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
-       ufs_getflags(vp, ip, buf);
+
+       /*
+        * XXX need to to locking state.
+        */
+
+       (void)getflags(ufs_flags, flags, ip->i_flag);
        PRWORD(ovflw, " %*d", 7, 1, ip->i_number);
-       PRWORD(ovflw, " %*s", 6, 1, buf);
+       PRWORD(ovflw, " %*s", 6, 1, flags);
        type = ip->i_ffs_mode & S_IFMT;
        if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode)) {
                if (usenumflag ||
                    (name = devname(ip->i_ffs_rdev, type)) == NULL) {
-                       snprintf(buf, sizeof(buf), "%d,%d",
+                       snprintf(dev, sizeof(dev), "%d,%d",
                            major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
-                       name = buf;
+                       name = dev;
                }
                PRWORD(ovflw, " %*s", 8, 1, name);
        } else
@@ -530,20 +528,27 @@
        int ovflw;
 {
        struct inode inode, *ip = &inode;
-       char buf[16], *name;
+       char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
+       char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
+       char *name;
        mode_t type;
 
        KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
-       ufs_getflags(vp, ip, buf);
+
+       /*
+        * XXX need to to locking state.
+        */
+
+       (void)getflags(ufs_flags, flags, ip->i_flag);
        PRWORD(ovflw, " %*d", 7, 1, ip->i_number);
-       PRWORD(ovflw, " %*s", 6, 1, buf);
+       PRWORD(ovflw, " %*s", 6, 1, flags);
        type = ip->i_e2fs_mode & S_IFMT;
        if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) {
                if (usenumflag ||
                    (name = devname(ip->i_e2fs_rdev, type)) == NULL) {
-                       snprintf(buf, sizeof(buf), "%d,%d",
+                       snprintf(dev, sizeof(dev), "%d,%d",
                            major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev));
-                       name = buf;
+                       name = dev;
                }
                PRWORD(ovflw, " %*s", 8, 1, name);
        } else
@@ -551,6 +556,20 @@
        return (0);
 }
 
+const struct flagbit_desc nfs_flags[] = {
+       { NFLUSHWANT,   'W' },
+       { NFLUSHINPROG, 'P' },
+       { NMODIFIED,    'M' },
+       { NWRITEERR,    'E' },
+       { NQNFSNONCACHE, 'X' },



Home | Main Index | Thread Index | Old Index