1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)nohup.c 5.1 (Berkeley) 05/20/89"; 26 #endif /* not lint */ 27 28 #include <sys/param.h> 29 #include <sys/signal.h> 30 #include <sys/file.h> 31 #include <unistd.h> 32 #include <stdio.h> 33 34 main(argc, argv) 35 int argc; 36 char **argv; 37 { 38 extern int errno; 39 char *strerror(); 40 41 if (argc < 2) 42 usage(); 43 44 if (isatty(STDOUT_FILENO)) 45 dofile(); 46 if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) { 47 /* may have just closed stderr */ 48 (void)fprintf(stdin, "nohup: %s\n", strerror(errno)); 49 exit(1); 50 } 51 52 (void)signal(SIGHUP, SIG_IGN); 53 (void)signal(SIGQUIT, SIG_IGN); 54 55 execvp(argv[1], &argv[1]); 56 (void)fprintf(stderr, 57 "nohup: %s: %s\n", argv[1], strerror(errno)); 58 exit(1); 59 } 60 61 dofile() 62 { 63 int fd; 64 char *p, path[MAXPATHLEN]; 65 char *getenv(), *strcpy(), *strcat(), *strerror(); 66 67 #define FILENAME "nohup.out" 68 p = FILENAME; 69 if ((fd = open(p, O_RDWR|O_CREAT|O_TRUNC, 0600)) >= 0) 70 goto dupit; 71 if (p = getenv("HOME")) { 72 (void)strcpy(path, p); 73 (void)strcat(path, "/"); 74 (void)strcat(path, FILENAME); 75 if ((fd = open(p = path, O_RDWR|O_CREAT|O_TRUNC, 0600)) >= 0) 76 goto dupit; 77 } 78 (void)fprintf(stderr, "nohup: can't open a nohup.out file.\n"); 79 exit(1); 80 dupit: if (dup2(fd, STDOUT_FILENO) == -1) { 81 (void)fprintf(stderr, "nohup: %s\n", strerror(errno)); 82 exit(1); 83 } 84 (void)fprintf(stderr, "sending output to %s\n", p); 85 } 86 87 usage() 88 { 89 (void)fprintf(stderr, "usage: nohup command\n"); 90 exit(1); 91 } 92