Source-Changes-HG archive

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

[src/netbsd-3]: src/usr.bin/pkill Pull up following revision(s) (requested by...



details:   https://anonhg.NetBSD.org/src/rev/49739a6c7991
branches:  netbsd-3
changeset: 577356:49739a6c7991
user:      riz <riz%NetBSD.org@localhost>
date:      Sat Oct 15 16:32:20 2005 +0000

description:
Pull up following revision(s) (requested by kleink in ticket #890):
        usr.bin/pkill/pkill.c: revision 1.11
- KNF
- better handling of errors and messages
- pass lint

diffstat:

 usr.bin/pkill/pkill.c |  140 ++++++++++++++++++++++++++-----------------------
 1 files changed, 74 insertions(+), 66 deletions(-)

diffs (truncated from 310 to 300 lines):

diff -r f972e1b71881 -r 49739a6c7991 usr.bin/pkill/pkill.c
--- a/usr.bin/pkill/pkill.c     Sat Oct 15 16:31:50 2005 +0000
+++ b/usr.bin/pkill/pkill.c     Sat Oct 15 16:32:20 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pkill.c,v 1.9.2.1 2005/10/15 16:31:50 riz Exp $        */
+/*     $NetBSD: pkill.c,v 1.9.2.2 2005/10/15 16:32:20 riz Exp $        */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: pkill.c,v 1.9.2.1 2005/10/15 16:31:50 riz Exp $");
+__RCSID("$NetBSD: pkill.c,v 1.9.2.2 2005/10/15 16:32:20 riz Exp $");
 #endif /* !lint */
 
 #include <sys/types.h>
@@ -61,6 +61,7 @@
 #include <pwd.h>
 #include <grp.h>
 #include <errno.h>
+#include <paths.h>
 
 #define        STATUS_MATCH    0
 #define        STATUS_NOMATCH  1
@@ -83,40 +84,38 @@
 
 SLIST_HEAD(listhead, list);
 
-struct kinfo_proc2     *plist;
-char   *selected;
-char   *delim = "\n";
-int    nproc;
-int    pgrep;
-int    signum = SIGTERM;
-int    newest;
-int    inverse;
-int    longfmt;
-int    matchargs;
-int    fullmatch;
-int    cflags = REG_EXTENDED;
-kvm_t  *kd;
-pid_t  mypid;
+static struct kinfo_proc2      *plist;
+static char    *selected;
+static const char *delim = "\n";
+static int     nproc;
+static int     pgrep;
+static int     signum = SIGTERM;
+static int     newest;
+static int     inverse;
+static int     longfmt;
+static int     matchargs;
+static int     fullmatch;
+static int     cflags = REG_EXTENDED;
+static kvm_t   *kd;
+static pid_t   mypid;
 
-struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
-struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
-struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
-struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
-struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
-struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
-struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
+static struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
 
 int    main(int, char **);
-void   usage(void);
-int    killact(struct kinfo_proc2 *);
-int    grepact(struct kinfo_proc2 *);
-void   makelist(struct listhead *, enum listtype, char *);
+static void    usage(void) __attribute__((__noreturn__));
+static int     killact(struct kinfo_proc2 *);
+static int     grepact(struct kinfo_proc2 *);
+static void    makelist(struct listhead *, enum listtype, char *);
 
 int
 main(int argc, char **argv)
 {
-       extern char *optarg;
-       extern int optind;
        char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
        int i, j, ch, bestidx, rv, criteria;
        int (*action)(struct kinfo_proc2 *);
@@ -126,6 +125,8 @@
        regex_t reg;
        regmatch_t regmatch;
 
+       setprogname(argv[0]);
+
        if (strcmp(getprogname(), "pgrep") == 0) {
                action = grepact;
                pgrep = 1;
@@ -232,19 +233,20 @@
         */
        kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
        if (kd == NULL)
-               errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
+               errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf);
 
        plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
        if (plist == NULL)
-               errx(STATUS_ERROR, "kvm_getproc2() failed");
+               errx(STATUS_ERROR, "Cannot get process list (%s)",
+                   kvm_geterr(kd));
 
        /*
         * Allocate memory which will be used to keep track of the
         * selection.
         */
-       if ((selected = malloc(nproc)) == NULL)
-               errx(STATUS_ERROR, "memory allocation failure");
-       memset(selected, 0, nproc);
+       if ((selected = calloc(1, nproc)) == NULL)
+               err(STATUS_ERROR, "Cannot allocate memory for %d processes",
+                   nproc);
 
        /*
         * Refine the selection.
@@ -252,7 +254,9 @@
        for (; *argv != NULL; argv++) {
                if ((rv = regcomp(&reg, *argv, cflags)) != 0) {
                        regerror(rv, &reg, buf, sizeof(buf));
-                       errx(STATUS_BADUSAGE, "bad expression: %s", buf);
+                       errx(STATUS_BADUSAGE,
+                           "Cannot compile regular expression `%s' (%s)",
+                           *argv, buf);
                }
 
                for (i = 0, kp = plist; i < nproc; i++, kp++) {
@@ -285,7 +289,9 @@
                                        selected[i] = 1;
                        } else if (rv != REG_NOMATCH) {
                                regerror(rv, &reg, buf, sizeof(buf));
-                               errx(STATUS_ERROR, "regexec(): %s", buf);
+                               errx(STATUS_ERROR,
+                                   "Regular expression evaluation error (%s)",
+                                   buf);
                        }
                }
 
@@ -378,7 +384,7 @@
                        }
                }
 
-               memset(selected, 0, nproc);
+               (void)memset(selected, 0, nproc);
                if (bestidx != -1)
                        selected[bestidx] = 1;
        }
@@ -404,7 +410,7 @@
        exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
 }
 
-void
+static void
 usage(void)
 {
        const char *ustr;
@@ -414,15 +420,15 @@
        else
                ustr = "[-signal] [-finvx]";
 
-       fprintf(stderr,
-               "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n"
+       (void)fprintf(stderr,
+               "Usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n"
                "             [-t tty] [-u euid] pattern ...\n", getprogname(),
                ustr);
 
        exit(STATUS_ERROR);
 }
 
-int
+static int
 killact(struct kinfo_proc2 *kp)
 {
        if (kill(kp->p_pid, signum) == -1) {
@@ -441,7 +447,7 @@
        return 1;
 }
 
-int
+static int
 grepact(struct kinfo_proc2 *kp)
 {
        char **argv;
@@ -455,31 +461,33 @@
                         */
                        return 0;
 
-               printf("%d ", (int)kp->p_pid);
+               (void)printf("%d ", (int)kp->p_pid);
                for (; *argv != NULL; argv++) {
-                       printf("%s", *argv);
+                       (void)printf("%s", *argv);
                        if (argv[1] != NULL)
-                               putchar(' ');
+                               (void)putchar(' ');
                }
        } else if (longfmt)
-               printf("%d %s", (int)kp->p_pid, kp->p_comm);
+               (void)printf("%d %s", (int)kp->p_pid, kp->p_comm);
        else
-               printf("%d", (int)kp->p_pid);
+               (void)printf("%d", (int)kp->p_pid);
 
-       printf("%s", delim);
+       (void)printf("%s", delim);
 
        return 1;
 }
 
-void
+static void
 makelist(struct listhead *head, enum listtype type, char *src)
 {
        struct list *li;
        struct passwd *pw;
        struct group *gr;
        struct stat st;
-       char *sp, *p, buf[MAXPATHLEN];
+       char *sp, *ep, buf[MAXPATHLEN];
+       const char *p;
        int empty;
+       const char *prefix = _PATH_DEV;
 
        empty = 1;
 
@@ -488,12 +496,13 @@
                        usage();
 
                if ((li = malloc(sizeof(*li))) == NULL)
-                       errx(STATUS_ERROR, "memory allocation failure");
+                       err(STATUS_ERROR, "Cannot allocate %zd bytes",
+                           sizeof(*li));
                SLIST_INSERT_HEAD(head, li, li_chain);
                empty = 0;
 
-               li->li_number = (uid_t)strtol(sp, &p, 0);
-               if (*p == '\0') {
+               li->li_number = (uid_t)strtol(sp, &ep, 0);
+               if (*ep == '\0') {
                        switch (type) {
                        case LT_PGRP:
                                if (li->li_number == 0)
@@ -514,13 +523,13 @@
                switch (type) {
                case LT_USER:
                        if ((pw = getpwnam(sp)) == NULL)
-                               errx(STATUS_BADUSAGE, "unknown user `%s'",
+                               errx(STATUS_BADUSAGE, "Unknown user `%s'",
                                    sp);
                        li->li_number = pw->pw_uid;
                        break;
                case LT_GROUP:
                        if ((gr = getgrnam(sp)) == NULL)
-                               errx(STATUS_BADUSAGE, "unknown group `%s'",
+                               errx(STATUS_BADUSAGE, "Unknown group `%s'",
                                    sp);
                        li->li_number = gr->gr_gid;
                        break;
@@ -532,29 +541,28 @@
                                p = "console";
                        else if (strncmp(sp, "tty", 3) == 0)
                                p = sp;
-                       else
-                               p = NULL;
+                       else {
+                               p = sp;
+                               prefix = _PATH_TTY;
+                       }
 
-                       if (p == NULL)
-                               snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
-                       else
-                               snprintf(buf, sizeof(buf), "/dev/%s", p);
+                       (void)snprintf(buf, sizeof(buf), "%s%s", prefix, p);
 
-                       if (stat(buf, &st) < 0) {
+                       if (stat(buf, &st) == -1) {
                                if (errno == ENOENT)
                                        errx(STATUS_BADUSAGE,
-                                           "no such tty: `%s'", sp);
-                               err(STATUS_ERROR, "stat(%s)", sp);
+                                           "No such tty: `%s'", sp);
+                               err(STATUS_ERROR, "Cannot access `%s'", sp);
                        }
 
                        if ((st.st_mode & S_IFCHR) == 0)
-                               errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
+                               errx(STATUS_BADUSAGE, "Not a tty: `%s'", sp);



Home | Main Index | Thread Index | Old Index