commit d999a15f9aac85a5f89abb02b55735363216997f
Author: FRIGN <[email protected]>
Date:   Wed Mar 4 22:39:12 2015 +0100

    Audit nohup(1)
    
    1) no need to include sys/stat.h
    2) remove the enum which just added a layer too thick on this simple
       program
    3) argc-style, other style
    4) weprintf instead of enprintf, then save the error-message of
       execvp before and return the proper status.
    5) write consistent "not reached" comment.

diff --git a/README b/README
index b1d55a3..962865f 100644
--- a/README
+++ b/README
@@ -50,7 +50,7 @@ The following tools are implemented ('*' == finished, '#' == 
UTF-8 support,
 =*  mv              yes                          none (-i)
 =*| nice            yes                          none
 =   nl              no                           -d, -f, -h, -l, -p
-=*  nohup           yes                          none
+=*| nohup           yes                          none
 #*  paste           yes                          none
 =*| printenv        non-posix                    none
 #*  printf          yes                          none
diff --git a/nohup.c b/nohup.c
index 7543b41..b1e69e9 100644
--- a/nohup.c
+++ b/nohup.c
@@ -1,6 +1,4 @@
 /* See LICENSE file for copyright and license details. */
-#include <sys/stat.h>
-
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -8,8 +6,6 @@
 
 #include "util.h"
 
-enum { Error = 127, Found = 126 };
-
 static void
 usage(void)
 {
@@ -19,35 +15,33 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-       int fd;
+       int fd, savederrno;
 
        ARGBEGIN {
        default:
                usage();
        } ARGEND;
 
-       if (argc == 0)
+       if (!argc)
                usage();
 
        if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
-               enprintf(Error, "signal HUP:");
+               enprintf(127, "signal HUP:");
 
        if (isatty(STDOUT_FILENO)) {
-               if ((fd = open("nohup.out", O_APPEND|O_CREAT,
-                              S_IRUSR|S_IWUSR)) < 0) {
-                       enprintf(Error, "open nohup.out:");
-               }
+               if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | 
S_IWUSR)) < 0)
+                       enprintf(127, "open nohup.out:");
                if (dup2(fd, STDOUT_FILENO) < 0)
-                       enprintf(Error, "dup2:");
+                       enprintf(127, "dup2:");
                close(fd);
        }
-       if (isatty(STDERR_FILENO))
-               if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
-                       enprintf(Error, "dup2:");
-
-       execvp(argv[0], &argv[0]);
-       enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[0]);
-       _exit(Error);
-       /* unreachable */
-       return 0;
+       if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
+               enprintf(127, "dup2:");
+
+       execvp(argv[0], argv);
+       savederrno = errno;
+       weprintf("exec %s:", argv[0]);
+       _exit(126 + (savederrno == ENOENT));
+
+       return 0; /* not reached */
 }

Reply via email to