#if 0
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
static char sccsid[] = "@(#)nohup.c 8.1 (Berkeley) 6/6/93";
#endif
#endif
#include <sys/cdefs.h>
#ifndef __APPLE__
__FBSDID("$FreeBSD: src/usr.bin/nohup/nohup.c,v 1.10 2003/05/03 19:44:46 obrien Exp $");
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#include <vproc.h>
#include <vproc_priv.h>
#endif
static void dofile(void);
static void usage(void);
#define FILENAME "nohup.out"
#define EXIT_NOEXEC 126
#define EXIT_NOTFOUND 127
#define EXIT_MISC 127
int
main(int argc, char *argv[])
{
int exit_status;
while (getopt(argc, argv, "") != -1)
usage();
argc -= optind;
argv += optind;
if (argc < 1)
usage();
if (isatty(STDOUT_FILENO))
dofile();
if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
err(EXIT_MISC, "%s", argv[0]);
(void)signal(SIGHUP, SIG_IGN);
#if defined(__APPLE__) && !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
if (_vprocmgr_detach_from_console(0) != NULL)
err(EXIT_MISC, "can't detach from console");
#endif
execvp(*argv, argv);
exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
err(exit_status, "%s", argv[0]);
}
static void
dofile(void)
{
int fd;
char path[MAXPATHLEN];
const char *p;
p = FILENAME;
fd = open(p, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
if (fd != -1)
goto dupit;
if ((p = getenv("HOME")) != NULL && *p != '\0' &&
(size_t)snprintf(path, sizeof(path), "%s/%s", p, FILENAME) <
sizeof(path)) {
fd = open(p = path, O_RDWR | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR);
if (fd != -1)
goto dupit;
}
errx(EXIT_MISC, "can't open a nohup.out file");
dupit:
#ifdef __APPLE__
(void)lseek(fd, 0L, SEEK_END);
#endif
if (dup2(fd, STDOUT_FILENO) == -1)
err(EXIT_MISC, NULL);
(void)fprintf(stderr, "appending output to %s\n", p);
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: nohup [--] utility [arguments]\n");
exit(EXIT_MISC);
}