date and time created 88/12/14 16:21:47 by marc
[unix-history] / usr / src / usr.bin / fstat / fstat.c
index 807f807..23529fd 100644 (file)
@@ -1,18 +1,29 @@
 /*
  * Copyright (c) 1987 Regents of the University of California.
 /*
  * Copyright (c) 1987 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif /* !lint */
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)fstat.c    5.9 (Berkeley) %G%";
-#endif /* !lint */
+static char sccsid[] = "@(#)fstat.c    5.13 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  *  fstat 
 
 /*
  *  fstat 
@@ -61,15 +72,13 @@ static char sccsid[] = "@(#)fstat.c 5.9 (Berkeley) %G%";
 #define        TEXT    -2
 #define        WD      -1
 
 #define        TEXT    -2
 #define        WD      -1
 
-#define        vprintf if (vflg) printf
-
 typedef struct devs {
 typedef struct devs {
-       struct devs     *next;
-       dev_t   dev;
-       int     inum;
-       char    *name;
+       struct devs *next;
+       dev_t dev;
+       int inum;
+       char *name;
 } DEVS;
 } DEVS;
-DEVS   *devs;
+DEVS *devs;
 
 static struct nlist nl[] = {
        { "_proc" },
 
 static struct nlist nl[] = {
        { "_proc" },
@@ -83,25 +92,24 @@ static struct nlist nl[] = {
        { "" },
 };
 
        { "" },
 };
 
-struct proc    *mproc;
-struct pte     *Usrptma, *usrpt;
+struct proc *mproc;
+struct pte *Usrptma, *usrpt;
 
 union {
 
 union {
-       struct  user user;
-       char    upages[UPAGES][NBPG];
+       struct user user;
+       char upages[UPAGES][NBPG];
 } user;
 
 } user;
 
-extern int     errno;
-static off_t   procp;
-static int     fflg, hadfflg, vflg;
-static int     kmem, mem, nproc, swap;
-static char    *uname;
+extern int errno;
+static int fflg, vflg;
+static int kmem, mem, nproc, swap;
+static char *uname;
 
 
-off_t  lseek();
+off_t lseek();
 
 main(argc, argv)
 
 main(argc, argv)
-       int     argc;
-       char    **argv;
+       int argc;
+       char **argv;
 {
        extern char *optarg;
        extern int optind;
 {
        extern char *optarg;
        extern int optind;
@@ -134,7 +142,7 @@ main(argc, argv)
                        uid = passwd->pw_uid;
                        uname = passwd->pw_name;
                        break;
                        uid = passwd->pw_uid;
                        uname = passwd->pw_name;
                        break;
-               case 'v':
+               case 'v':       /* undocumented: print read error messages */
                        vflg++;
                        break;
                case '?':
                        vflg++;
                        break;
                case '?':
@@ -142,19 +150,14 @@ main(argc, argv)
                        usage();
                }
 
                        usage();
                }
 
-       for (argv += optind; *argv; ++argv) {
-               hadfflg = 1;
-               if (getfname(*argv))
-                       fflg = 1;
+       if (*(argv += optind)) {
+               for (; *argv; ++argv) {
+                       if (getfname(*argv))
+                               fflg = 1;
+               }
+               if (!fflg)      /* file(s) specified, but none accessable */
+                       exit(1);
        }
        }
-       if (hadfflg && !fflg)   /* file(s) specified, but none accessable */
-               exit(1);
-
-       printf("USER\t CMD\t      PID    FD\tDEVICE\tINODE\t  SIZE TYPE");
-       if (fflg)
-               printf(" NAME\n");
-       else
-               printf("\n");
 
        openfiles();
 
 
        openfiles();
 
@@ -164,18 +167,19 @@ main(argc, argv)
        }
        Usrptma = (struct pte *)nl[X_USRPTMA].n_value;
        usrpt = (struct pte *) nl[X_USRPT].n_value;
        }
        Usrptma = (struct pte *)nl[X_USRPTMA].n_value;
        usrpt = (struct pte *) nl[X_USRPT].n_value;
-       procp = lgetw((off_t)nl[X_PROC].n_value);
        nproc = (int)lgetw((off_t)nl[X_NPROC].n_value);
 
        nproc = (int)lgetw((off_t)nl[X_NPROC].n_value);
 
+       (void)lseek(kmem, lgetw((off_t)nl[X_PROC].n_value), L_SET);
        size = nproc * sizeof(struct proc);
        if ((mproc = (struct proc *)malloc((u_int)size)) == NULL) {
                fprintf(stderr, "fstat: out of space.\n");
                exit(1);
        }
        size = nproc * sizeof(struct proc);
        if ((mproc = (struct proc *)malloc((u_int)size)) == NULL) {
                fprintf(stderr, "fstat: out of space.\n");
                exit(1);
        }
-
-       (void)lseek(kmem, (off_t)procp, L_SET);
        if (read(kmem, (char *)mproc, size) != size)
        if (read(kmem, (char *)mproc, size) != size)
-               cantread("proc table", N_KMEM);
+               rerr1("proc table", N_KMEM);
+
+       printf("USER\t CMD\t      PID    FD\tDEVICE\tINODE\t  SIZE TYPE%s\n",
+           fflg ? " NAME" : "");
        for (; nproc--; ++mproc) {
                if (mproc->p_stat == 0)
                        continue;
        for (; nproc--; ++mproc) {
                if (mproc->p_stat == 0)
                        continue;
@@ -242,11 +246,11 @@ getu()
 static
 dotext()
 {
 static
 dotext()
 {
-       struct text     text;
+       struct text text;
 
        (void)lseek(kmem, (off_t)mproc->p_textp, L_SET);
        if (read(kmem, (char *) &text, sizeof(text)) != sizeof(text)) {
 
        (void)lseek(kmem, (off_t)mproc->p_textp, L_SET);
        if (read(kmem, (char *) &text, sizeof(text)) != sizeof(text)) {
-               cantread("text table", N_KMEM);
+               rerr1("text table", N_KMEM);
                return;
        }
        if (text.x_flag)
                return;
        }
        if (text.x_flag)
@@ -256,7 +260,7 @@ dotext()
 static
 itrans(ftype, g, fno)
        int ftype, fno;
 static
 itrans(ftype, g, fno)
        int ftype, fno;
-       struct inode    *g;             /* if ftype is inode */
+       struct inode *g;                /* if ftype is inode */
 {
        struct inode inode;
        dev_t idev;
 {
        struct inode inode;
        dev_t idev;
@@ -266,7 +270,7 @@ itrans(ftype, g, fno)
        if (g || fflg) {
                (void)lseek(kmem, (off_t)g, L_SET);
                if (read(kmem, (char *)&inode, sizeof(inode)) != sizeof(inode)) {
        if (g || fflg) {
                (void)lseek(kmem, (off_t)g, L_SET);
                if (read(kmem, (char *)&inode, sizeof(inode)) != sizeof(inode)) {
-                       vprintf("error %d reading inode at %x from kmem\n", errno, (int)g);
+                       rerr2(errno, (int)g, "inode");
                        return;
                }
                idev = inode.i_dev;
                        return;
                }
                idev = inode.i_dev;
@@ -363,7 +367,7 @@ socktrans(sock)
        (void)lseek(kmem, (off_t)sock, L_SET);
        if (read(kmem, (char *)&so, sizeof(struct socket))
            != sizeof(struct socket)) {
        (void)lseek(kmem, (off_t)sock, L_SET);
        if (read(kmem, (char *)&so, sizeof(struct socket))
            != sizeof(struct socket)) {
-               vprintf("error %d reading socket at %x from kmem\n", errno, (int)sock);
+               rerr2(errno, (int)sock, "socket");
                return;
        }
 
                return;
        }
 
@@ -371,7 +375,7 @@ socktrans(sock)
        (void)lseek(kmem, (off_t)so.so_proto, L_SET);
        if (read(kmem, (char *)&proto, sizeof(struct protosw))
            != sizeof(struct protosw)) {
        (void)lseek(kmem, (off_t)so.so_proto, L_SET);
        if (read(kmem, (char *)&proto, sizeof(struct protosw))
            != sizeof(struct protosw)) {
-               vprintf("error %d reading protosw at %x from kmem\n", errno, (int)so.so_proto);
+               rerr2(errno, (int)so.so_proto, "protosw");
                return;
        }
 
                return;
        }
 
@@ -379,7 +383,7 @@ socktrans(sock)
        (void)lseek(kmem, (off_t)proto.pr_domain, L_SET);
        if (read(kmem, (char *)&dom, sizeof(struct domain))
            != sizeof(struct domain)) {
        (void)lseek(kmem, (off_t)proto.pr_domain, L_SET);
        if (read(kmem, (char *)&dom, sizeof(struct domain))
            != sizeof(struct domain)) {
-               vprintf("error %d reading domain at %x from kmem\n", errno, (int)proto.pr_domain);
+               rerr2(errno, (int)proto.pr_domain, "domain");
                return;
        }
 
                return;
        }
 
@@ -392,7 +396,7 @@ socktrans(sock)
        else {
                (void)lseek(kmem, (off_t)dom.dom_name, L_SET);
                if ((len = read(kmem, dname, sizeof(dname) - 1)) < 0) {
        else {
                (void)lseek(kmem, (off_t)dom.dom_name, L_SET);
                if ((len = read(kmem, dname, sizeof(dname) - 1)) < 0) {
-                       vprintf("error %d reading char at %x from kmem\n", errno, (int)dom.dom_name);
+                       rerr2(errno, (int)dom.dom_name, "char");
                        dname[0] = '\0';
                }
                else
                        dname[0] = '\0';
                }
                else
@@ -406,7 +410,7 @@ socktrans(sock)
                    so.so_state);
 
        /* 
                    so.so_state);
 
        /* 
-        * protocol specific formating 
+        * protocol specific formatting
         *
         * Try to find interesting things to print.  For tcp, the interesting
         * thing is the address of the tcpcb, for udp and others, just the
         *
         * Try to find interesting things to print.  For tcp, the interesting
         * thing is the address of the tcpcb, for udp and others, just the
@@ -424,7 +428,7 @@ socktrans(sock)
                                (void)lseek(kmem, (off_t)so.so_pcb, L_SET);
                                if (read(kmem, (char *)&inpcb, sizeof(struct inpcb))
                                    != sizeof(struct inpcb)){
                                (void)lseek(kmem, (off_t)so.so_pcb, L_SET);
                                if (read(kmem, (char *)&inpcb, sizeof(struct inpcb))
                                    != sizeof(struct inpcb)){
-                                       vprintf("error %d reading inpcb at %x from kmem\n", errno, (int)so.so_pcb);
+                                       rerr2(errno, (int)so.so_pcb, "inpcb");
                                        return;
                                }
                                printf(" %x", (int)inpcb.inp_ppcb);
                                        return;
                                }
                                printf(" %x", (int)inpcb.inp_ppcb);
@@ -440,7 +444,7 @@ socktrans(sock)
                        (void)lseek(kmem, (off_t)so.so_pcb, L_SET);
                        if (read(kmem, (char *)&unpcb, sizeof(struct unpcb))
                            != sizeof(struct unpcb)){
                        (void)lseek(kmem, (off_t)so.so_pcb, L_SET);
                        if (read(kmem, (char *)&unpcb, sizeof(struct unpcb))
                            != sizeof(struct unpcb)){
-                               vprintf("error %d reading unpcb at %x from kmem\n", errno, (int)so.so_pcb);
+                               rerr2(errno, (int)so.so_pcb, "unpcb");
                                return;
                        }
                        if (unpcb.unp_conn) {
                                return;
                        }
                        if (unpcb.unp_conn) {
@@ -513,7 +517,7 @@ readf()
                (void)lseek(kmem, (off_t)user.user.u_ofile[i], L_SET);
                if (read(kmem, (char *)&lfile, sizeof(lfile))
                    != sizeof(lfile)) {
                (void)lseek(kmem, (off_t)user.user.u_ofile[i], L_SET);
                if (read(kmem, (char *)&lfile, sizeof(lfile))
                    != sizeof(lfile)) {
-                       cantread("file", N_KMEM);
+                       rerr1("file", N_KMEM);
                        continue;
                }
                itrans(lfile.f_type, (struct inode *)lfile.f_data, i);
                        continue;
                }
                itrans(lfile.f_type, (struct inode *)lfile.f_data, i);
@@ -586,10 +590,20 @@ openfiles()
 }
 
 static
 }
 
 static
-cantread(what, fromwhat)
+rerr1(what, fromwhat)
        char *what, *fromwhat;
 {
        char *what, *fromwhat;
 {
-       vprintf("fstat: error reading %s from %s", what, fromwhat);
+       if (vflg)
+               printf("fstat: error reading %s from %s", what, fromwhat);
+}
+
+static
+rerr2(err, address, what)
+       int err, address;
+       char *what;
+{
+       if (vflg)
+               printf("error %d reading %s at %x from kmem\n", errno, what, address);
 }
 
 static long
 }
 
 static long
@@ -599,8 +613,8 @@ lgetw(loc)
        long word;
 
        (void)lseek(kmem, (off_t)loc, L_SET);
        long word;
 
        (void)lseek(kmem, (off_t)loc, L_SET);
-       if (read(kmem, (char *) &word, sizeof(word)) != sizeof(word))
-               vprintf("error reading kmem at %lx\n", loc);
+       if (read(kmem, (char *)&word, sizeof(word)) != sizeof(word))
+               rerr2(errno, (int)loc, "word");
        return(word);
 }
 
        return(word);
 }