sbase

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

nohup.c (901B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 
      4 #include <errno.h>
      5 #include <fcntl.h>
      6 #include <signal.h>
      7 #include <unistd.h>
      8 
      9 #include "util.h"
     10 
     11 static void
     12 usage(void)
     13 {
     14 	eprintf("usage: %s cmd [arg ...]\n", argv0);
     15 }
     16 
     17 int
     18 main(int argc, char *argv[])
     19 {
     20 	int fd, savederrno;
     21 
     22 	argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
     23 
     24 	if (!argc)
     25 		usage();
     26 
     27 	if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
     28 		enprintf(127, "signal HUP:");
     29 
     30 	if (isatty(STDOUT_FILENO)) {
     31 		if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
     32 			enprintf(127, "open nohup.out:");
     33 		if (dup2(fd, STDOUT_FILENO) < 0)
     34 			enprintf(127, "dup2:");
     35 		close(fd);
     36 	}
     37 	if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
     38 		enprintf(127, "dup2:");
     39 
     40 	execvp(argv[0], argv);
     41 	savederrno = errno;
     42 	weprintf("execvp %s:", argv[0]);
     43 
     44 	_exit(126 + (savederrno == ENOENT));
     45 }