]> git.saurik.com Git - apple/system_cmds.git/blobdiff - sysctl.tproj/sysctl.c
system_cmds-431.tar.gz
[apple/system_cmds.git] / sysctl.tproj / sysctl.c
index 631ad4094b8a6dfc1afa5a383d88946d0f33c6de..b861263c2a3d25b84caec8b78a8a6a92dbe00264 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  setting values where the format is specified as unsigned integer.
  */
  
+#include <sys/cdefs.h>
 #ifndef lint
-static char copyright[] =
+__unused static char copyright[] =
 "@(#) Copyright (c) 1993\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)sysctl.c   8.5 (Berkeley) 5/9/95";
+__unused static char sccsid[] = "@(#)sysctl.c  8.5 (Berkeley) 5/9/95";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -158,6 +159,7 @@ struct list secondlevel[] = {
 
 static int     Aflag, aflag, bflag, nflag, wflag, Xflag;
 static int     foundSome = 0;
+static int     invalid_name_used = 0;
 
 void listall(char *prefix, struct list *lp);
 void old_parse(char *string, int flags);
@@ -213,7 +215,7 @@ main(argc, argv)
                usage();
        for (; *argv != NULL; ++argv) 
                parse(*argv, 1);
-       exit(0);
+       exit(invalid_name_used ? 1 : 0);
 }
 
 /*
@@ -348,7 +350,8 @@ old_parse(string, flags)
                useUnsignedInt = 1;
                break;
 
-       case CTL_VM:
+       case CTL_VM: break;
+#if 0 /* XXX Handled by the new sysctl mechanism */
                switch (mib[1]) {
                case VM_LOADAVG: {      /* XXX this is bogus */
                        double loads[3];
@@ -392,6 +395,7 @@ old_parse(string, flags)
                fprintf(stderr,
                    "Use vmstat or systat to view %s information\n", string);
                return;
+#endif
         
        case CTL_DEBUG:
                mib[2] = CTL_DEBUG_VALUE;
@@ -441,18 +445,33 @@ old_parse(string, flags)
                switch (type) {
                case CTLTYPE_INT:
                        if (useUnsignedInt) {   
-                               uintval = strtoul(newval, 0, 0);
+                               uintval = strtoul(newval, NULL, 0);
+                               if ((uintval == 0) && (errno == EINVAL)) {
+                                       fprintf(stderr, "invalid argument: %s\n",
+                                               (char *)newval);
+                                       return;
+                               }
                                newval = &uintval;
                                newsize = sizeof uintval;
                        } else {
-                       intval = atoi(newval);
-                       newval = &intval;
-                       newsize = sizeof intval;
+                               intval = strtol(newval, NULL,  0);
+                               if ((intval == 0) && (errno == EINVAL)) {
+                                       fprintf(stderr, "invalid argument: %s\n",
+                                               (char *)newval);
+                                       return;
+                               }
+                               newval = &intval;
+                               newsize = sizeof intval;
                        }
                        break;
 
                case CTLTYPE_QUAD:
-                       sscanf(newval, "%qd", &quadval);
+                       quadval = strtoq(newval, NULL, 0);
+                       if ((quadval == 0) && (errno == EINVAL)) {
+                               fprintf(stderr, "invalid argument: %s\n",
+                                       (char *)newval);
+                               return;
+                       }
                        newval = &quadval;
                        newsize = sizeof quadval;
                        break;
@@ -463,7 +482,7 @@ old_parse(string, flags)
                if (flags == 0)
                        return;
                switch (errno) {
-               case EOPNOTSUPP:
+               case ENOTSUP:
                        fprintf(stderr, "%s: value is not available\n", string);
                        return;
                case ENOTDIR:
@@ -496,7 +515,7 @@ old_parse(string, flags)
                        fprintf(stdout, "%s = %s\n", string,
                            ctime((time_t *) &btp->tv_sec));
                else
-                       fprintf(stdout, "%d\n", btp->tv_sec);
+                       fprintf(stdout, "%ld\n", btp->tv_sec);
                return;
        }
        if (special & CONSDEV) {
@@ -609,7 +628,7 @@ void vfsinit()
        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)
+                       if (errno == ENOTSUP)
                                continue;
                        perror("vfsinit");
                        free(vfsname);
@@ -645,6 +664,7 @@ findname(string, level, bufp, namelist)
                bufp[0][strlen(*bufp)-1]='\0';
        if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
                fprintf(stderr, "%s: incomplete specification\n", string);
+               invalid_name_used = 1;
                return (-1);
        }
        for (i = 0; i < namelist->size; i++)
@@ -654,6 +674,7 @@ findname(string, level, bufp, namelist)
        if (i == namelist->size) {
                fprintf(stderr, "%s level name %s in %s is invalid\n",
                    level, name, string);
+               invalid_name_used = 1;
                return (-1);
        }
        return (i);
@@ -738,10 +759,22 @@ parse(char *string, int flags)
                        case CTLTYPE_INT:
                                if ((*fmt == 'I') && (*(fmt + 1) == 'U')) {
                                        uintval = (unsigned int) strtoul (newval, NULL, 0);
+                                       if ((uintval == 0) &&
+                                           (errno == EINVAL)) {
+                                               errx(1, "invalid argument: %s",
+                                                       newval);
+                                               return;
+                                       }
                                        newval = &uintval;
                                        newsize = sizeof uintval;
                                } else {
                                        intval = (int) strtol(newval, NULL, 0);
+                                       if ((intval == 0) &&
+                                           (errno == EINVAL)) {
+                                               errx(1, "invalid argument: %s",
+                                                       newval);
+                                               return;
+                                       }
                                        newval = &intval;
                                        newsize = sizeof intval;
                                }
@@ -750,6 +783,10 @@ parse(char *string, int flags)
                                break;
                        case CTLTYPE_QUAD:
                                quadval = strtoq(newval, NULL, 0);
+                               if ((quadval == 0) && (errno == EINVAL)) {
+                                       errx(1, "invalid argument %s", newval);
+                                       return;
+                               }
                                newval = &quadval;
                                newsize = sizeof quadval;
                                break;
@@ -764,7 +801,7 @@ parse(char *string, int flags)
                        if (!i && !bflag)
                                putchar('\n');
                        switch (errno) {
-                       case EOPNOTSUPP:
+                       case ENOTSUP:
                                errx(1, "%s: value is not available", 
                                        string);
                        case ENOTDIR:
@@ -837,6 +874,23 @@ S_timeval(int l2, void *p)
        return (0);
 }
 
+static int
+S_xswusage(int l2, void *p)
+{
+        struct xsw_usage       *xsu = (struct xsw_usage *)p;
+  
+       if(l2 != sizeof (*xsu))
+           err(1, "S_xswusage %d != %d", l2, sizeof *xsu);
+
+       fprintf(stdout,
+               "total = %.2fM  used = %.2fM  free = %.2fM  %s",
+               ((double) xsu->xsu_total) / (1024.0 * 1024.0),
+               ((double) xsu->xsu_used) / (1024.0 * 1024.0),
+               ((double) xsu->xsu_avail) / (1024.0 * 1024.0),
+               xsu->xsu_encrypted ? "(encrypted)" : "");
+       return 0;
+}
+
 static int
 T_dev_t(int l2, void *p)
 {
@@ -987,13 +1041,13 @@ show_var(int *oid, int nlen, int show_masked)
                if (!nflag)
                        printf("%s: ", name);
                fmt++;
-               val = "";
+               val = (unsigned char *)"";
                while (len >= sizeof(int)) {
                        if(*fmt == 'U')
                                printf("%s%u", val, *(unsigned int *)p);
                        else
                                printf("%s%d", val, *(int *)p);
-                       val = " ";
+                       val = (unsigned char *)" ";
                        len -= sizeof (int);
                        p += sizeof (int);
                }
@@ -1004,13 +1058,13 @@ show_var(int *oid, int nlen, int show_masked)
                if (!nflag)
                        printf("%s: ", name);
                fmt++;
-               val = "";
+               val = (unsigned char *)"";
                while (len >= sizeof(long)) {
                        if(*fmt == 'U')
                                printf("%s%lu", val, *(unsigned long *)p);
                        else
                                printf("%s%ld", val, *(long *)p);
-                       val = " ";
+                       val = (unsigned char *)" ";
                        len -= sizeof (long);
                        p += sizeof (long);
                }
@@ -1028,13 +1082,13 @@ show_var(int *oid, int nlen, int show_masked)
                if (!nflag)
                        printf("%s: ", name);
                fmt++;
-               val = "";
+               val = (unsigned char *)"";
                while (len >= sizeof(long long)) {
                        if(*fmt == 'U')
                                printf("%s%llu", val, *(unsigned long long *)p);
                        else
                                printf("%s%lld", val, *(long long *)p);
-                       val = " ";
+                       val = (unsigned char *)" ";
                        len -= sizeof (long long);
                        p += sizeof (long long);
                }
@@ -1048,6 +1102,7 @@ show_var(int *oid, int nlen, int show_masked)
                if (!strcmp(fmt, "S,clockinfo"))        func = S_clockinfo;
                else if (!strcmp(fmt, "S,timeval"))     func = S_timeval;
                else if (!strcmp(fmt, "S,loadavg"))     func = S_loadavg;
+               else if (!strcmp(fmt, "S,xsw_usage"))   func = S_xswusage;
                else if (!strcmp(fmt, "T,dev_t"))       func = T_dev_t;
                if (func) {
                        if (!nflag)