opendir change: refinement
[minix.git] / commands / nohup / nohup.c
blob94f95842f776bc61a8b570e11c55f0a9f5a4d84f
1 /*
2 * Copyright (c) 2009, Erik van der Kouwe
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Functionality implemented according to this specification:
30 * http://www.opengroup.org/onlinepubs/000095399/utilities/nohup.html
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
42 #define NOHUP_OUT_FILENAME "nohup.out"
44 static void print_usage(const char *argv0)
46 printf("Usage: %s command [arg...]\n", argv0);
49 static int redirect_tty(void)
51 int fd;
52 char buffer[PATH_MAX + 1], *home;
54 /* redirect stdout to a file if needed */
55 if (isatty(STDOUT_FILENO))
57 /* first try: current directory */
58 fd = open(NOHUP_OUT_FILENAME, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
59 if (fd < 0)
61 /* alternative: home directory */
62 home = getenv("HOME");
63 if (home)
65 snprintf(buffer, sizeof(buffer), "%s/%s", home, NOHUP_OUT_FILENAME);
66 buffer[sizeof(buffer) - 1] = 0;
67 fd = open(buffer, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
71 if (fd < 0)
73 perror("cannot create " NOHUP_OUT_FILENAME " and $HOME/" NOHUP_OUT_FILENAME);
74 return -1;
77 /* move the fd to stdout */
78 if (dup2(fd, STDOUT_FILENO) < 0 || close(fd) < 0)
80 perror("cannot redirect stdout");
81 return -1;
85 /* redirect stderr to stdout if needed */
86 if (isatty(STDERR_FILENO))
88 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
90 perror("cannot redirect stderr");
91 return -1;
95 return 0;
98 int main(int argc, char **argv)
100 struct sigaction sa;
102 /* check parameters */
103 if (argc < 2)
105 print_usage(argv[0]);
106 return 127;
109 /* ignore SIGHUP */
110 sa.sa_handler = SIG_IGN;
111 sigemptyset(&sa.sa_mask);
112 sa.sa_flags = 0;
113 if (sigaction(SIGHUP, &sa, NULL) < 0)
115 perror("cannot ignore SIGHUP");
116 return 127;
119 /* redirect TTY input and output */
120 if (redirect_tty() < 0)
121 return 127;
123 /* run the command */
124 execvp(argv[1], argv + 1);
125 perror("cannot execute");
127 /* exit code depends on whether the utility was found */
128 switch (errno)
130 case ELOOP:
131 case ENAMETOOLONG:
132 case ENOENT:
133 case ENOTDIR:
134 /* utility not found */
135 return 127;
137 default:
138 /* exec failed for other reason */
139 return 126;