12 int main(int argc
, char **argv
) {
13 int outfd
,tty_1
,tty_2
;
16 fprintf(stderr
,"usage: %s utility [args...]\n",argv
[0]);
22 outfd
=open("nohup.out",O_APPEND
|O_CREAT
|O_WRONLY
,S_IRUSR
|S_IWUSR
);
25 char *homedir
=getenv("HOME");
27 fprintf(stderr
,"%s: cannot get value of $HOME\n",argv
[0]);
30 if(!(filepath
=malloc(strlen(homedir
)+11))) {
31 fprintf(stderr
,"%s: out of memory\n",argv
[0]);
34 strcpy(filepath
,homedir
);
35 strcat(filepath
,"/nohup.out");
36 outfd
=open(filepath
,O_APPEND
|O_CREAT
|O_WRONLY
,S_IRUSR
|S_IWUSR
);
39 fprintf(stderr
,"%s: cannot create %s/nohup.out\n",argv
[0],homedir
);
43 if(tty_2
&& dup2(outfd
,2)==-1) {
44 fprintf(stderr
,"%s: cannot redirect stderr: %s\n",argv
[0],strerror(errno
));
47 if(tty_1
&& dup2(outfd
,1)==-1) {
48 fprintf(stderr
,"%s: cannot redirect stdout: %s\n",argv
[0],strerror(errno
));
52 signal(SIGHUP
, SIG_IGN
);
53 execvp(argv
[1], argv
+ 1);
54 fprintf(stderr
, "%s: cannot exec `%s': %s\n",argv
[0],argv
[1],strerror(errno
));