Annotation of coherent/b/bin/nohup.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Make a process immmune to quit and hup signals.
                      3:  * Vlad 2-12-92
                      4:  */
                      5: #include       <stdio.h>
                      6: #include       <signal.h>
                      7: #include       <string.h>
                      8: #include       <ctype.h>
                      9: #include       <access.h>
                     10: 
                     11: char   *flatten();     /* Convert array of strings to a string */
                     12: 
                     13: char   *name;
                     14: 
                     15: main(argc, argv)
                     16: int    argc;
                     17: char   **argv;
                     18: {
                     19:        if (argc < 2) {
                     20:                fprintf(stderr, "usage: nohup command arg...\n");
                     21:                exit(1);
                     22:        }
                     23:        immune();                       /* Make immunity */
                     24:        checkredirect();/* Check and redirect if necessary stdout and stderr */
                     25:        execvp(argv[1], argv + 1);      /* Execute command */
                     26:        /* We should never come here */
                     27:        fatal("%s: No such file or directory", flatten(&argv[1]));
                     28: }
                     29: 
                     30: /* 
                     31:  * Ignore hup and quit signals
                     32:  */
                     33: immune()
                     34: {
                     35:        signal(SIGHUP, SIG_IGN);
                     36:        signal(SIGQUIT, SIG_IGN);
                     37: }
                     38: 
                     39: /* 
                     40:  * If stdout and/or stderr is a tty. If it is so then redirect 
                     41:  * stdout and/or stderr to  "outfile".
                     42:  */ 
                     43: checkredirect()
                     44: {      
                     45:        char    *outname();     /* Find the name of the output file */
                     46:        char    *outfile;       /* Name of the output file */
                     47: 
                     48: 
                     49:        if (isatty(1)) {
                     50:                outfile = outname();    /* Find output file name */
                     51:                fprintf(stderr, "Sending output to %s\n", outfile);
                     52:                redirect(stdout, outfile);
                     53:                if (isatty(2))
                     54:                        redirect(stderr, outfile);
                     55:        } else 
                     56:                if (isatty(2)) {
                     57:                        outfile = outname();    /* Find output file name */
                     58:                        redirect(stderr, outfile);
                     59:                }
                     60: }
                     61: 
                     62: /*
                     63:  * Redirect fp to output file.
                     64:  */
                     65: redirect(fp, name)
                     66: FILE   *fp;    /* Pointer to a file that should be redirected */
                     67: char   *name;  /* File name */
                     68: {
                     69:        if (freopen(name, "a", fp) == NULL) 
                     70:                fatal("cannot open output file");
                     71: }
                     72: 
                     73: /* 
                     74:  * Function flatten() concatinates an array of the strings to a string,
                     75:  * where the input strings are separated by spaces.
                     76:  */
                     77: #define        BUF_SIZE        80              /* Size of the buffer to be alloc */
                     78: 
                     79: char   *flatten(argv)
                     80: char   *argv[];
                     81: {
                     82:        extern char     *malloc(),
                     83:                        *realloc();
                     84:        char            *buf;                   /* Buffer for output string */
                     85:        unsigned        count = BUF_SIZE;       /* Size of the buffer */
                     86:        unsigned        len;                    /* Current length used */
                     87:        unsigned        i;                      /* Length of new addition */
                     88: 
                     89:        buf = malloc(BUF_SIZE);
                     90:        *buf = '\0';
                     91:        len  = 1;       /* We always need a terminator */
                     92: 
                     93:        for (; *argv != NULL; argv++) {
                     94:                while ((len + (i = strlen(*argv))) > count) {
                     95:                        count += BUF_SIZE;
                     96:                        if ((buf = realloc(buf, count)) == NULL)
                     97:                                fatal("cannot allocate memory");
                     98:                }
                     99: 
                    100:                /* If it is not the first string, write string separator */
                    101:                if (len > 1) 
                    102:                        strcpy(buf + len++ - 1, " ");
                    103: 
                    104:                strcpy(buf + len - 1, *argv);
                    105:                len += i;
                    106:        }
                    107:        return (realloc(buf, len));
                    108: }
                    109: 
                    110: /*
                    111:  * Print a message on a terminal and die. The question is we have to open
                    112:  * a terminal.
                    113:  */
                    114: fatal(msg)
                    115: char   *msg;
                    116: {
                    117:        extern char     *ttyname();
                    118:        FILE    *fp;    /* File pointer to a terminal */
                    119: 
                    120:        if ((fp = fopen("/dev/tty", "w", fp)) != NULL)
                    121:                fprintf(fp, "nohup: %r\n", &msg);
                    122:        exit(1);
                    123: }
                    124: 
                    125: /*
                    126:  * Find a name of output file. It is nohup.out or $HOME/nohup.out if
                    127:  * nohup.out is not writeable.
                    128:  */
                    129: char *outname()
                    130: {
                    131:        extern char     *getenv();
                    132:        char    *buf;                           /* Output file name buffer */
                    133:        char    *dir;                           /* User home directory */
                    134:        int     bufsize;                        /* Size of output file name */
                    135:        char    *nohup_out = "nohup.out";       /* Name of output file */
                    136:        char    *home = "HOME";                 /* HOME envir. variable */
                    137: 
                    138:        buf = nohup_out;
                    139:        /* Can we write into the current directory? */
                    140:        if (access(buf, AAPPND) && access(".", ACREAT)) {
                    141:                buf = nohup_out;
                    142:                dir = getenv(home);     /* Get $HOME directory */
                    143:                bufsize = strlen(dir);
                    144:                /* Allocate memory for output file $HOME/nohup.out */
                    145:                if ((buf = malloc(bufsize + strlen(nohup_out) + 1)) == NULL) 
                    146:                        fatal("cannot allocate memory");
                    147:                /* Constract the name of the output file */
                    148:                strcpy(buf, dir);
                    149:                buf[bufsize] = '/';
                    150:                strcpy(buf + bufsize + 1, nohup_out);
                    151:                /* Can we write into the $HOME directory? */                    
                    152:                if (access(buf, AAPPND) && access(dir, ACREAT))
                    153:                        fatal("cannot open output file");
                    154:        }
                    155:        return(buf);
                    156: }
                    157: 
                    158: 
                    159: 
                    160: 
                    161: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.