argument pointer not being incremented through arguments
[unix-history] / usr / src / usr.sbin / sysctl / sysctl.c
index e6824c3..ce0e14b 100644 (file)
@@ -1,22 +1,23 @@
 /*
 /*
- * Copyright (c) 1993 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1993 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)sysctl.c   5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)sysctl.c   8.3 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/gmon.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/gmon.h>
+#include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/socket.h>
@@ -44,10 +45,12 @@ struct ctlname netname[] = CTL_NET_NAMES;
 struct ctlname hwname[] = CTL_HW_NAMES;
 struct ctlname username[] = CTL_USER_NAMES;
 struct ctlname debugname[CTL_DEBUG_MAXID];
 struct ctlname hwname[] = CTL_HW_NAMES;
 struct ctlname username[] = CTL_USER_NAMES;
 struct ctlname debugname[CTL_DEBUG_MAXID];
+struct ctlname *vfsname;
 #ifdef CTL_MACHDEP_NAMES
 struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
 #endif
 char names[BUFSIZ];
 #ifdef CTL_MACHDEP_NAMES
 struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
 #endif
 char names[BUFSIZ];
+int lastused;
 
 struct list {
        struct  ctlname *list;
 
 struct list {
        struct  ctlname *list;
@@ -58,7 +61,7 @@ struct list secondlevel[] = {
        { 0, 0 },                       /* CTL_UNSPEC */
        { kernname, KERN_MAXID },       /* CTL_KERN */
        { vmname, VM_MAXID },           /* CTL_VM */
        { 0, 0 },                       /* CTL_UNSPEC */
        { kernname, KERN_MAXID },       /* CTL_KERN */
        { vmname, VM_MAXID },           /* CTL_VM */
-       { 0, 0 },                       /* CTL_FS */
+       { 0, 0 },                       /* CTL_VFS */
        { netname, NET_MAXID },         /* CTL_NET */
        { 0, CTL_DEBUG_MAXID },         /* CTL_DEBUG */
        { hwname, HW_MAXID },           /* CTL_HW */
        { netname, NET_MAXID },         /* CTL_NET */
        { 0, CTL_DEBUG_MAXID },         /* CTL_DEBUG */
        { hwname, HW_MAXID },           /* CTL_HW */
@@ -116,13 +119,14 @@ main(argc, argv)
 
        if (Aflag || aflag) {
                debuginit();
 
        if (Aflag || aflag) {
                debuginit();
+               vfsinit();
                for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
                        listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
                exit(0);
        }
        if (argc == 0)
                usage();
                for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
                        listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
                exit(0);
        }
        if (argc == 0)
                usage();
-       while (argc-- > 0)
+       for (; *argv != NULL; ++argv)
                parse(*argv, 1);
        exit(0);
 }
                parse(*argv, 1);
        exit(0);
 }
@@ -159,12 +163,14 @@ parse(string, flags)
        char *string;
        int flags;
 {
        char *string;
        int flags;
 {
-       int indx, type, state, size, len;
+       int indx, type, state, len;
+       size_t size;
        int special = 0;
        void *newval = 0;
        int intval, newsize = 0;
        quad_t quadval;
        struct list *lp;
        int special = 0;
        void *newval = 0;
        int intval, newsize = 0;
        quad_t quadval;
        struct list *lp;
+       struct vfsconf vfc;
        int mib[CTL_MAXNAME];
        char *cp, *bufp, buf[BUFSIZ], strval[BUFSIZ];
 
        int mib[CTL_MAXNAME];
        char *cp, *bufp, buf[BUFSIZ], strval[BUFSIZ];
 
@@ -185,6 +191,8 @@ parse(string, flags)
        if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
                return;
        mib[0] = indx;
        if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
                return;
        mib[0] = indx;
+       if (indx == CTL_VFS)
+               vfsinit();
        if (indx == CTL_DEBUG)
                debuginit();
        lp = &secondlevel[indx];
        if (indx == CTL_DEBUG)
                debuginit();
        lp = &secondlevel[indx];
@@ -288,7 +296,26 @@ parse(string, flags)
 #endif
                break;
 
 #endif
                break;
 
-       case CTL_FS:
+       case CTL_VFS:
+               mib[3] = mib[1];
+               mib[1] = VFS_GENERIC;
+               mib[2] = VFS_CONF;
+               len = 4;
+               size = sizeof vfc;
+               if (sysctl(mib, 4, &vfc, &size, (void *)0, (size_t)0) < 0) {
+                       perror("vfs print");
+                       return;
+               }
+               if (flags == 0 && vfc.vfc_refcount == 0)
+                       return;
+               if (!nflag)
+                       fprintf(stdout, "%s has %d mounted instance%s\n",
+                           string, vfc.vfc_refcount,
+                           vfc.vfc_refcount != 1 ? "s" : "");
+               else
+                       fprintf(stdout, "%d\n", vfc.vfc_refcount);
+               return;
+
        case CTL_USER:
                break;
 
        case CTL_USER:
                break;
 
@@ -431,7 +458,7 @@ debuginit()
        secondlevel[CTL_DEBUG].list = debugname;
        mib[0] = CTL_DEBUG;
        mib[2] = CTL_DEBUG_NAME;
        secondlevel[CTL_DEBUG].list = debugname;
        mib[0] = CTL_DEBUG;
        mib[2] = CTL_DEBUG_NAME;
-       for (loc = 0, i = 0; i < CTL_DEBUG_MAXID; i++) {
+       for (loc = lastused, i = 0; i < CTL_DEBUG_MAXID; i++) {
                mib[1] = i;
                size = BUFSIZ - loc;
                if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
                mib[1] = i;
                size = BUFSIZ - loc;
                if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
@@ -440,6 +467,50 @@ debuginit()
                debugname[i].ctl_type = CTLTYPE_INT;
                loc += size;
        }
                debugname[i].ctl_type = CTLTYPE_INT;
                loc += size;
        }
+       lastused = loc;
+}
+
+/*
+ * Initialize the set of filesystem names
+ */
+vfsinit()
+{
+       int mib[4], maxtypenum, cnt, loc, size;
+       struct vfsconf vfc;
+       size_t buflen;
+
+       if (secondlevel[CTL_VFS].list != 0)
+               return;
+       mib[0] = CTL_VFS;
+       mib[1] = VFS_GENERIC;
+       mib[2] = VFS_MAXTYPENUM;
+       buflen = 4;
+       if (sysctl(mib, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0)
+               return;
+       if ((vfsname = malloc(maxtypenum * sizeof(*vfsname))) == 0)
+               return;
+       memset(vfsname, 0, maxtypenum * sizeof(*vfsname));
+       mib[2] = VFS_CONF;
+       buflen = sizeof vfc;
+       for (loc = lastused, cnt = 0; cnt < maxtypenum; cnt++) {
+               mib[3] = cnt;
+               if (sysctl(mib, 4, &vfc, &buflen, (void *)0, (size_t)0) < 0) {
+                       if (errno == EOPNOTSUPP)
+                               continue;
+                       perror("vfsinit");
+                       free(vfsname);
+                       return;
+               }
+               strcat(&names[loc], vfc.vfc_name);
+               vfsname[cnt].ctl_name = &names[loc];
+               vfsname[cnt].ctl_type = CTLTYPE_INT;
+               size = strlen(vfc.vfc_name) + 1;
+               loc += size;
+       }
+       lastused = loc;
+       secondlevel[CTL_VFS].list = vfsname;
+       secondlevel[CTL_VFS].size = maxtypenum;
+       return;
 }
 
 struct ctlname inetname[] = CTL_IPPROTO_NAMES;
 }
 
 struct ctlname inetname[] = CTL_IPPROTO_NAMES;