This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / usr.bin / nohup / nohup.c
index 7401dc0..b38b9f6 100644 (file)
@@ -44,16 +44,31 @@ static char sccsid[] = "@(#)nohup.c 5.4 (Berkeley) 6/1/90";
 #include <sys/param.h>
 #include <sys/signal.h>
 #include <sys/file.h>
 #include <sys/param.h>
 #include <sys/signal.h>
 #include <sys/file.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 
 
-extern int errno;
+static void dofile();
+static void usage();
 
 
+/* nohup shall exit with one of the following values:
+   126 - The utility was found but could not be invoked.
+   127 - An error occured in the nohup utility, or the utility could
+         not be found. */
+#define EXIT_NOEXEC    126
+#define EXIT_NOTFOUND  127
+#define EXIT_MISC      127
+
+int
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       char *strerror();
+       int exit_status;
 
        if (argc < 2)
                usage();
 
        if (argc < 2)
                usage();
@@ -63,49 +78,59 @@ main(argc, argv)
        if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
                /* may have just closed stderr */
                (void)fprintf(stdin, "nohup: %s\n", strerror(errno));
        if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
                /* may have just closed stderr */
                (void)fprintf(stdin, "nohup: %s\n", strerror(errno));
-               exit(1);
+               exit(EXIT_MISC);
        }
 
        }
 
+       /* The nohup utility shall take the standard action for all signals
+          except that SIGHUP shall be ignored. */
        (void)signal(SIGHUP, SIG_IGN);
        (void)signal(SIGHUP, SIG_IGN);
-       (void)signal(SIGQUIT, SIG_IGN);
 
        execvp(argv[1], &argv[1]);
 
        execvp(argv[1], &argv[1]);
-       (void)fprintf(stderr,
-           "nohup: %s: %s\n", argv[1], strerror(errno));
-       exit(1);
+       exit_status = (errno = ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
+       (void)fprintf(stderr, "nohup: %s: %s\n", argv[1], strerror(errno));
+       exit(exit_status);
 }
 
 }
 
+static void
 dofile()
 {
        int fd;
        char *p, path[MAXPATHLEN];
 dofile()
 {
        int fd;
        char *p, path[MAXPATHLEN];
-       off_t lseek();
-       char *getenv(), *strcpy(), *strcat(), *strerror();
 
 
+       /* If the standard output is a terminal, all output written to 
+          its standard output shall be appended to the end of the file
+          nohup.out in the current directory.  If nohup.out cannot be
+          created or opened for appending, the output shall be appended
+          to the end of the file nohup.out in the directory specified 
+          by the HOME environment variable.
+
+          If a file is created, the file's permission bits shall be
+          set to S_IRUSR | S_IWUSR. */
 #define        FILENAME        "nohup.out"
        p = FILENAME;
 #define        FILENAME        "nohup.out"
        p = FILENAME;
-       if ((fd = open(p, O_RDWR|O_CREAT, 0600)) >= 0)
+       if ((fd = open(p, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
                goto dupit;
                goto dupit;
-       if (p = getenv("HOME")) {
+       if ((p = getenv("HOME")) != NULL) {
                (void)strcpy(path, p);
                (void)strcat(path, "/");
                (void)strcat(path, FILENAME);
                (void)strcpy(path, p);
                (void)strcat(path, "/");
                (void)strcat(path, FILENAME);
-               if ((fd = open(p = path, O_RDWR|O_CREAT, 0600)) >= 0)
+               if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
                        goto dupit;
        }
        (void)fprintf(stderr, "nohup: can't open a nohup.out file.\n");
                        goto dupit;
        }
        (void)fprintf(stderr, "nohup: can't open a nohup.out file.\n");
-       exit(1);
+       exit(EXIT_MISC);
 
 dupit: (void)lseek(fd, 0L, SEEK_END);
        if (dup2(fd, STDOUT_FILENO) == -1) {
                (void)fprintf(stderr, "nohup: %s\n", strerror(errno));
 
 dupit: (void)lseek(fd, 0L, SEEK_END);
        if (dup2(fd, STDOUT_FILENO) == -1) {
                (void)fprintf(stderr, "nohup: %s\n", strerror(errno));
-               exit(1);
+               exit(EXIT_MISC);
        }
        (void)fprintf(stderr, "sending output to %s\n", p);
 }
 
        }
        (void)fprintf(stderr, "sending output to %s\n", p);
 }
 
+static void
 usage()
 {
        (void)fprintf(stderr, "usage: nohup command\n");
 usage()
 {
        (void)fprintf(stderr, "usage: nohup command\n");
-       exit(1);
+       exit(EXIT_MISC);
 }
 }