-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetach.c
77 lines (70 loc) · 1.09 KB
/
detach.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <fcntl.h>
main(ac, av)
int ac;
char **av;
{
int pid;
char *file;
int vflag;
int mode;
close(0); open("/dev/null", 0);
close(1);
file = "detach.out";
mode = O_TRUNC;
vflag = 0;
while(**++av == '-') {
while(*++*av) {
switch(**av) {
case 'f':
if(*++*av)
file = *av;
else
file = *++av;
goto next_arg;
case 'v':
vflag++;
break;
case 'a':
mode = O_APPEND;
break;
}
}
next_arg: ;
}
if(open(file, O_WRONLY|mode|O_CREAT, 0666) < 0) {
perror(file);
exit(1);
}
switch(pid = fork()) {
case -1:
perror(av[0]);
exit(1);
break;
case 0:
if(vflag) {
char **p = av;
printf("# %d", getpid());
while(*p)
printf(" %s", *p++);
putchar('\n');
}
fflush(stdout);
close(2); dup(1);
setsid();
execv(av[0], av);
execvp(av[0], av);
perror(av[0]);
exit(1);
break;
default:
if(vflag) {
fprintf(stderr, "# %d", pid);
while(*av)
fprintf(stderr, " %s", *av++);
fputc('\n', stderr);
} else
fprintf(stderr, "%d\n", pid);
break;
}
}