Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/cron - eliminate a goto.



details:   https://anonhg.NetBSD.org/src/rev/2f8bd721b056
branches:  trunk
changeset: 583349:2f8bd721b056
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jul 31 17:52:01 2005 +0000

description:
- eliminate a goto.
- cast more returns to void.
- use waitpid instead of wait() to wait for our process, not accidentally
  wait for other children.
- return exitstatus only if exited, and return the termination signal otherwise.

diffstat:

 usr.sbin/cron/popen.c |  29 +++++++++++++----------------
 1 files changed, 13 insertions(+), 16 deletions(-)

diffs (94 lines):

diff -r db27789be1f3 -r 2f8bd721b056 usr.sbin/cron/popen.c
--- a/usr.sbin/cron/popen.c     Sun Jul 31 13:31:07 2005 +0000
+++ b/usr.sbin/cron/popen.c     Sun Jul 31 17:52:01 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: popen.c,v 1.9 2005/03/16 02:53:55 xtraeme Exp $        */
+/*     $NetBSD: popen.c,v 1.10 2005/07/31 17:52:01 christos Exp $      */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -36,7 +36,7 @@
 static char rcsid[] = "Id: popen.c,v 1.5 1994/01/15 20:43:43 vixie Exp";
 static char sccsid[] = "@(#)popen.c    5.7 (Berkeley) 2/14/89";
 #else
-__RCSID("$NetBSD: popen.c,v 1.9 2005/03/16 02:53:55 xtraeme Exp $");
+__RCSID("$NetBSD: popen.c,v 1.10 2005/07/31 17:52:01 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -84,29 +84,27 @@
                if (!(argv[argc++] = strtok(cp, " \t\n")))
                        break;
 
-       iop = NULL;
        switch(pid = vfork()) {
        case -1:                        /* error */
                (void)close(pdes[0]);
                (void)close(pdes[1]);
-               goto pfree;
-               /* NOTREACHED */
+               return NULL;
        case 0:                         /* child */
                if (*type == 'r') {
                        if (pdes[1] != 1) {
-                               dup2(pdes[1], 1);
-                               dup2(pdes[1], 2);       /* stderr, too! */
+                               (void)dup2(pdes[1], 1);
+                               (void)dup2(pdes[1], 2); /* stderr, too! */
                                (void)close(pdes[1]);
                        }
                        (void)close(pdes[0]);
                } else {
                        if (pdes[0] != 0) {
-                               dup2(pdes[0], 0);
+                               (void)dup2(pdes[0], 0);
                                (void)close(pdes[0]);
                        }
                        (void)close(pdes[1]);
                }
-               execvp(argv[0], argv);
+               (void)execvp(argv[0], argv);
                _exit(1);
        }
        /* parent; assume fdopen can't fail...  */
@@ -118,8 +116,6 @@
                (void)close(pdes[0]);
        }
        pids[fileno(iop)] = pid;
-
-pfree:
        return(iop);
 }
 
@@ -128,7 +124,7 @@
 {
        int fdes;
        sigset_t oset, nset;
-       WAIT_T stat_loc;
+       WAIT_T status;
        PID_T pid;
 
        /*
@@ -136,7 +132,7 @@
         * `popened' command, or, if already `pclosed'.
         */
        if (pids == 0 || pids[fdes = fileno(iop)] == 0)
-               return(-1);
+               return -1;
        (void)fclose(iop);
        
        sigemptyset(&nset);
@@ -144,9 +140,10 @@
        sigaddset(&nset, SIGQUIT);
        sigaddset(&nset, SIGHUP);
        (void)sigprocmask(SIG_BLOCK, &nset, &oset);
-       while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1)
-               ;
+       pid = waitpid(pids[fdes], &status, 0);
        (void)sigprocmask(SIG_SETMASK, &oset, NULL);
        pids[fdes] = 0;
-       return (pid == -1 ? -1 : WEXITSTATUS(stat_loc));
+       if (pid == -1)
+               return -1;
+       return WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status);
 }



Home | Main Index | Thread Index | Old Index