summaryrefslogtreecommitdiff
path: root/bin/nohup/nohup.c
blob: 8a6171ebfcfad6107ff63efac0c780058cf70600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	int fildes;

	if (argc < 2) {
		write(STDOUT_FILENO, "nohup utility [argument...]\n", 29);
		return 1;
	}

	if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
		perror("nohup: signal");
		return 1;
	}

	if (isatty(STDOUT_FILENO)) {
		fildes = open("nohup.out", O_WRONLY | O_APPEND | O_CREAT);
		if (fildes < 0) {
			perror("nohup: open");
			return 1;
		}

		if (dup2(fildes, STDOUT_FILENO) < 0) {
			perror("nohup: dup2");
			return 1;
		}

		close(fildes);
	}

	if (isatty(STDERR_FILENO)) {
		if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
			perror("nohup: dup2");
			return 1;
		}
	}

	execvp(argv[0], argv);
	perror("nohup: execvp");
	return 127 + (errno == ENOENT);
}