1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)nohup.c 5.4 (Berkeley) 06/01/90"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/signal.h> 20 #include <sys/file.h> 21 #include <unistd.h> 22 #include <stdio.h> 23 24 extern int errno; 25 26 main(argc, argv) 27 int argc; 28 char **argv; 29 { 30 char *strerror(); 31 32 if (argc < 2) 33 usage(); 34 35 if (isatty(STDOUT_FILENO)) 36 dofile(); 37 if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) { 38 /* may have just closed stderr */ 39 (void)fprintf(stdin, "nohup: %s\n", strerror(errno)); 40 exit(1); 41 } 42 43 (void)signal(SIGHUP, SIG_IGN); 44 (void)signal(SIGQUIT, SIG_IGN); 45 46 execvp(argv[1], &argv[1]); 47 (void)fprintf(stderr, 48 "nohup: %s: %s\n", argv[1], strerror(errno)); 49 exit(1); 50 } 51 52 dofile() 53 { 54 int fd; 55 char *p, path[MAXPATHLEN]; 56 off_t lseek(); 57 char *getenv(), *strcpy(), *strcat(), *strerror(); 58 59 #define FILENAME "nohup.out" 60 p = FILENAME; 61 if ((fd = open(p, O_RDWR|O_CREAT, 0600)) >= 0) 62 goto dupit; 63 if (p = getenv("HOME")) { 64 (void)strcpy(path, p); 65 (void)strcat(path, "/"); 66 (void)strcat(path, FILENAME); 67 if ((fd = open(p = path, O_RDWR|O_CREAT, 0600)) >= 0) 68 goto dupit; 69 } 70 (void)fprintf(stderr, "nohup: can't open a nohup.out file.\n"); 71 exit(1); 72 73 dupit: (void)lseek(fd, 0L, SEEK_END); 74 if (dup2(fd, STDOUT_FILENO) == -1) { 75 (void)fprintf(stderr, "nohup: %s\n", strerror(errno)); 76 exit(1); 77 } 78 (void)fprintf(stderr, "sending output to %s\n", p); 79 } 80 81 usage() 82 { 83 (void)fprintf(stderr, "usage: nohup command\n"); 84 exit(1); 85 } 86