1
0
mirror of git://git.suckless.org/sbase synced 2024-12-25 16:32:37 +00:00
sbase/nohup.c
FRIGN 7b6d918b78 Add mandoc-manpage for nohup(1)
and mark it as finished in README.
2015-01-28 23:55:57 +01:00

54 lines
983 B
C

/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "util.h"
enum { Error = 127, Found = 126 };
static void
usage(void)
{
eprintf("usage: %s cmd [arg ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
int fd;
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc == 0)
usage();
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
enprintf(Error, "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 (dup2(fd, STDOUT_FILENO) < 0)
enprintf(Error, "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;
}