commit a6ee96af7c3219a6835adbb851887f1e6e30ba00
parent 4c4e5a3eb2a18a2a80e8571e8560ef7bf68abddf
Author: FRIGN <dev@frign.de>
Date: Wed, 4 Mar 2015 22:39:12 +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.
Diffstat:
2 files changed, 16 insertions(+), 22 deletions(-)
diff --git 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
@@ -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 */
}