version 1.10, 2003/02/16 03:09:50 |
version 1.11, 2003/02/18 01:17:44 |
|
|
#include <errno.h> |
#include <errno.h> |
#include <err.h> |
#include <err.h> |
|
|
static void dofile(); |
static void dofile(void); |
static void usage(); |
__dead static void usage(void); |
|
|
/* nohup shall exit with one of the following values: |
/* |
126 - The utility was found but could not be invoked. |
* nohup shall exit with one of the following values: |
127 - An error occurred in the nohup utility, or the utility could |
* 126 - The utility was found but could not be invoked. |
not be found. */ |
* 127 - An error occurred in the nohup utility, or the utility could |
|
* not be found. |
|
*/ |
#define EXIT_NOEXEC 126 |
#define EXIT_NOEXEC 126 |
#define EXIT_NOTFOUND 127 |
#define EXIT_NOTFOUND 127 |
#define EXIT_MISC 127 |
#define EXIT_MISC 127 |
|
|
|
/* |
|
* If the standard output is a terminal, all output written to |
|
* its standard output shall be appended to the end of the file |
|
* nohup.out in the current directory. If nohup.out cannot be |
|
* created or opened for appending, the output shall be appended |
|
* to the end of the file nohup.out in the directory specified |
|
* by the HOME environment variable. |
|
* |
|
* If a file is created, the file's permission bits shall be |
|
* set to S_IRUSR | S_IWUSR. |
|
*/ |
|
#define FILENAME "nohup.out" |
|
|
int |
int |
main(argc, argv) |
main(int argc, char *argv[]) |
int argc; |
|
char **argv; |
|
{ |
{ |
int exit_status; |
int exit_status; |
|
|
|
|
exit(EXIT_MISC); |
exit(EXIT_MISC); |
} |
} |
|
|
/* The nohup utility shall take the standard action for all signals |
/* |
except that SIGHUP shall be ignored. */ |
* The nohup utility shall take the standard action for all signals |
|
* except that SIGHUP shall be ignored. |
|
*/ |
(void)signal(SIGHUP, SIG_IGN); |
(void)signal(SIGHUP, SIG_IGN); |
|
|
execvp(argv[1], &argv[1]); |
execvp(argv[1], &argv[1]); |
|
|
} |
} |
|
|
static void |
static void |
dofile() |
dofile(void) |
{ |
{ |
int fd; |
int fd; |
char *p, path[MAXPATHLEN]; |
const char *p; |
|
char path[MAXPATHLEN]; |
|
|
/* If the standard output is a terminal, all output written to |
|
its standard output shall be appended to the end of the file |
|
nohup.out in the current directory. If nohup.out cannot be |
|
created or opened for appending, the output shall be appended |
|
to the end of the file nohup.out in the directory specified |
|
by the HOME environment variable. |
|
|
|
If a file is created, the file's permission bits shall be |
|
set to S_IRUSR | S_IWUSR. */ |
|
#define FILENAME "nohup.out" |
|
p = FILENAME; |
p = FILENAME; |
if ((fd = open(p, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0) |
if ((fd = open(p, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0) |
goto dupit; |
goto dupit; |
if ((p = getenv("HOME")) != NULL && *p != '\0' && |
if ((p = getenv("HOME")) != NULL && *p != '\0' && |
(strlen(p) + strlen(FILENAME) + 1) < sizeof(path)) { |
(strlen(p) + strlen(FILENAME) + 1) < sizeof(path)) { |
(void)strlcpy(path, p, sizeof path); |
(void)strlcpy(path, p, sizeof(path)); |
(void)strlcat(path, "/", sizeof path); |
(void)strlcat(path, "/", sizeof(path)); |
(void)strlcat(path, FILENAME, sizeof path); |
(void)strlcat(path, FILENAME, sizeof(path)); |
if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0) |
if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0) |
goto dupit; |
goto dupit; |
} |
} |
errx(EXIT_MISC, "can't open a nohup.out file"); |
errx(EXIT_MISC, "can't open a nohup.out file"); |
|
|
dupit: (void)lseek(fd, 0, SEEK_END); |
dupit: |
|
(void)lseek(fd, (off_t)0, SEEK_END); |
if (dup2(fd, STDOUT_FILENO) == -1) |
if (dup2(fd, STDOUT_FILENO) == -1) |
err(EXIT_MISC, NULL); |
err(EXIT_MISC, NULL); |
(void)fprintf(stderr, "sending output to %s\n", p); |
(void)fprintf(stderr, "sending output to %s\n", p); |
} |
} |
|
|
static void |
__dead static void |
usage() |
usage(void) |
{ |
{ |
(void)fprintf(stderr, "usage: nohup command\n"); |
(void)fprintf(stderr, "usage: nohup command\n"); |
exit(EXIT_MISC); |
exit(EXIT_MISC); |