the format string gets modified by parsefmt -- don't use it twice
[unix-history] / usr / src / bin / ps / ps.c
index 4fa903b..79fe0e1 100644 (file)
@@ -12,7 +12,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ps.c       5.40 (Berkeley) %G%";
+static char sccsid[] = "@(#)ps.c       5.42 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -27,7 +27,6 @@ static char sccsid[] = "@(#)ps.c      5.40 (Berkeley) %G%";
 #include <kvm.h>
 #include <errno.h>
 #include <unistd.h>
 #include <kvm.h>
 #include <errno.h>
 #include <unistd.h>
-#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -113,6 +112,7 @@ main(argc, argv)
                case 'j':
                        parsefmt(jfmt);
                        fmt = 1;
                case 'j':
                        parsefmt(jfmt);
                        fmt = 1;
+                       jfmt[0] = '\0';
                        break;
                case 'L': 
                        showkey();
                        break;
                case 'L': 
                        showkey();
@@ -120,6 +120,7 @@ main(argc, argv)
                case 'l':
                        parsefmt(lfmt);
                        fmt = 1;
                case 'l':
                        parsefmt(lfmt);
                        fmt = 1;
+                       lfmt[0] = '\0';
                        break;
                case 'M':
                        memf = optarg;
                        break;
                case 'M':
                        memf = optarg;
@@ -134,6 +135,7 @@ main(argc, argv)
                        parsefmt(o1);
                        parsefmt(optarg);
                        parsefmt(o2);
                        parsefmt(o1);
                        parsefmt(optarg);
                        parsefmt(o2);
+                       o1[0] = o2[0] = '\0';
                        fmt = 1;
                        break;
                case 'o':
                        fmt = 1;
                        break;
                case 'o':
@@ -152,7 +154,7 @@ main(argc, argv)
                        break;
                case 'T':
                        if ((optarg = ttyname(STDIN_FILENO)) == NULL)
                        break;
                case 'T':
                        if ((optarg = ttyname(STDIN_FILENO)) == NULL)
-                               error("stdin: not a terminal");
+                               err("stdin: not a terminal");
                        /* FALLTHROUGH */
                case 't': {
                        char *ttypath;
                        /* FALLTHROUGH */
                case 't': {
                        char *ttypath;
@@ -166,13 +168,10 @@ main(argc, argv)
                                    _PATH_TTY, optarg);
                        else
                                ttypath = optarg;
                                    _PATH_TTY, optarg);
                        else
                                ttypath = optarg;
-                       if (stat(ttypath, &stbuf) == -1) {
-                               (void)fprintf(stderr,
-                                   "ps: %s: %s\n", ttypath, strerror(errno));
-                               exit(1);
-                       }
+                       if (stat(ttypath, &stbuf) == -1)
+                               err("%s: %s", ttypath, strerror(errno));
                        if (!S_ISCHR(stbuf.st_mode))
                        if (!S_ISCHR(stbuf.st_mode))
-                               error("%s: not a terminal", ttypath);
+                               err("%s: not a terminal", ttypath);
                        ttydev = stbuf.st_rdev;
                        break;
                }
                        ttydev = stbuf.st_rdev;
                        break;
                }
@@ -180,11 +179,13 @@ main(argc, argv)
                        parsefmt(ufmt);
                        sortby = SORTCPU;
                        fmt = 1;
                        parsefmt(ufmt);
                        sortby = SORTCPU;
                        fmt = 1;
+                       ufmt[0] = '\0';
                        break;
                case 'v':
                        parsefmt(vfmt);
                        sortby = SORTMEM;
                        fmt = 1;
                        break;
                case 'v':
                        parsefmt(vfmt);
                        sortby = SORTMEM;
                        fmt = 1;
+                       vfmt[0] = '\0';
                        break;
                case 'W':
                        swapf = optarg;
                        break;
                case 'W':
                        swapf = optarg;
@@ -218,7 +219,7 @@ main(argc, argv)
        }
 #endif
        if (kvm_openfiles(nlistf, memf, swapf) == -1)
        }
 #endif
        if (kvm_openfiles(nlistf, memf, swapf) == -1)
-               error("kvm_openfiles: %s", kvm_geterr());
+               err("kvm_openfiles: %s", kvm_geterr());
 
        if (!fmt)
                parsefmt(dfmt);
 
        if (!fmt)
                parsefmt(dfmt);
@@ -246,15 +247,11 @@ main(argc, argv)
        /*
         * select procs
         */
        /*
         * select procs
         */
-       if ((nentries = kvm_getprocs(what, flag)) == -1) {
-               (void) fprintf(stderr, "ps: %s\n", kvm_geterr());
-               exit(1);
-       }
-       kinfo = (KINFO *)malloc(nentries * sizeof(KINFO));
-       if (kinfo == NULL) {
-               (void)fprintf(stderr, "ps: %s\n", strerror(ENOMEM));
-               exit(1);
-       }
+       if ((nentries = kvm_getprocs(what, flag)) == -1)
+               err("%s", kvm_geterr());
+       kinfo = malloc(nentries * sizeof(KINFO));
+       if (kinfo == NULL)
+               err("%s", strerror(errno));
        for (nentries = 0; p = kvm_nextproc(); ++nentries) {
                kinfo[nentries].ki_p = p;
                kinfo[nentries].ki_e = kvm_geteproc(p);
        for (nentries = 0; p = kvm_nextproc(); ++nentries) {
                kinfo[nentries].ki_p = p;
                kinfo[nentries].ki_e = kvm_geteproc(p);
@@ -321,10 +318,8 @@ saveuser(ki)
        register struct usave *usp;
        register struct user *up;
 
        register struct usave *usp;
        register struct user *up;
 
-       if ((usp = (struct usave *)calloc(1, sizeof(struct usave))) == NULL) {
-               (void)fprintf(stderr, "ps: %s\n", strerror(errno));
-               exit(1);
-       }
+       if ((usp = calloc(1, sizeof(struct usave))) == NULL)
+               err("%s", strerror(errno));
        ki->ki_u = usp;
        up = kvm_getu(ki->ki_p);
        /*
        ki->ki_u = usp;
        up = kvm_getu(ki->ki_p);
        /*
@@ -389,10 +384,8 @@ kludge_oldps_options(s)
        char *newopts, *ns, *cp;
 
        len = strlen(s);
        char *newopts, *ns, *cp;
 
        len = strlen(s);
-       if ((newopts = ns = malloc(len + 2)) == NULL) {
-               (void)fprintf(stderr, "ps: %s\n", strerror(errno));
-               exit(1);
-       }
+       if ((newopts = ns = malloc(len + 2)) == NULL)
+               err("%s", strerror(errno));
        /*
         * options begin with '-'
         */
        /*
         * options begin with '-'
         */
@@ -432,27 +425,38 @@ kludge_oldps_options(s)
        return (newopts);
 }
 
        return (newopts);
 }
 
-#ifdef lint
-/* VARARGS1 */
-error(fmt) char *fmt; { (void) fputs(fmt, stderr); exit(1); /* NOTREACHED */ }
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+void
+#if __STDC__
+err(const char *fmt, ...)
 #else
 #else
-error(fmt)
+err(fmt, va_alist)
        char *fmt;
        char *fmt;
+        va_dcl
+#endif
 {
        va_list ap;
 {
        va_list ap;
-
+#if __STDC__
        va_start(ap, fmt);
        va_start(ap, fmt);
-       (void) fprintf(stderr, "ps: ");
-       (void) vfprintf(stderr, fmt, ap);
-       (void) fprintf(stderr, "\n");
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "ps: ");
+       (void)vfprintf(stderr, fmt, ap);
        va_end(ap);
        va_end(ap);
+       (void)fprintf(stderr, "\n");
        exit(1);
        exit(1);
+       /* NOTREACHED */
 }
 }
-#endif
 
 usage()
 {
        (void) fprintf(stderr,
 
 usage()
 {
        (void) fprintf(stderr,
-           "usage:\tps [ -aChjlmrSsTuvwx ] [ -O|o fmt ] [ -p pid ] [ -t tty ] [ system ] [ core ] [ swap ]\n\t ps [ -L ]\n");
+"usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]\n\t  [-M core] [-N system] [-W swap]\n       ps [-L]\n");
        exit(1);
 }
        exit(1);
 }