home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / redir / redir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-04  |  4.9 KB  |  155 lines

  1. /* redir
  2.  * a program to execute a command, redirecting stdout and stderr separately,
  3.  * a feature available in sh but not csh.
  4.  *
  5.  * THIS SOFTWARE EXISTS IN THE PUBLIC DOMAIN.  ABSOLUTELY NO
  6.  * RESTRICTIONS ON ITS USAGE, MODIFICATION OR DISTRIBUTION
  7.  * EXIST.  NO GUARANTEES ARE MADE CONCERNING THE PROPER
  8.  * FUNCTIONING OF THIS SOFTWARE.
  9.  *
  10.  * Usage:
  11.  *
  12.  * redir [options] command-words...
  13.  *
  14.  * where options are
  15.  *  -o file
  16.  *   to redirect stdout to file "file," without clobbering "file" if it exists
  17.  *  -e file
  18.  *   to redirect stderr to file "file," without clobbering "file" if it exists
  19.  *  -O file
  20.  *   to redirect stdout to file "file," clobbering "file" if it exists
  21.  *  -E file
  22.  *   to redirect stderr to file "file," clobbering "file" if it exists
  23.  *  -a
  24.  *   the following redirection option is to append to the named file, as in:
  25.  *    redir -O foo.log -ae foo.err command command-arg1 command-arg2 ...
  26.  *   (stdout (destructively) to foo.log, stderr appended to foo.err.
  27.  *   The -a option doesn't care if the named file exists or not.  This option
  28.  *   must be used twice if you want stdout and stderr both to be appended
  29.  *   to their respective files;
  30.  *    redir -ao foo.log -ae foo.err command ...
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <sys/file.h>
  35.  
  36. #define USAGESTRING ("Usage: %s [-[a][oeOE] file] ... command-words\n")
  37.  
  38. main(argc, argv)
  39. int             argc;
  40. char          **argv;
  41. {
  42.     extern int      optind;
  43.     extern char    *optarg;
  44.     char           *stderrfile = NULL, *stdoutfile = NULL;
  45.     int             rstderr = 0, rstdout = 0, cstderr = 0, cstdout = 0, astderr
  46. = 0, astdout = 0, appendopt = 0, c, fd;
  47.  
  48.     while ((c = getopt(argc, argv, "ao:e:O:E:")) != EOF) {
  49.        switch (c) {
  50.            case 'a':
  51.                if (appendopt) {
  52.                    fprintf(stderr, USAGESTRING, argv[0]);
  53.                    exit(1);
  54.                }
  55.                ++appendopt;
  56.                break;
  57.            case 'e':
  58.                if (rstderr) {
  59.                    fprintf(stderr, USAGESTRING, argv[0]);
  60.                    exit(1);
  61.                }
  62.                ++rstderr;
  63.                stderrfile = optarg;
  64.                if (appendopt) {
  65.                    appendopt = 0;
  66.                    ++astderr;
  67.                }
  68.                break;
  69.            case 'o':
  70.                if (rstdout) {
  71.                    fprintf(stderr, USAGESTRING, argv[0]);
  72.                    exit(1);
  73.                }
  74.                ++rstdout;
  75.                stdoutfile = optarg;
  76.                if (appendopt) {
  77.                    appendopt = 0;
  78.                    ++astdout;
  79.                }
  80.                break;
  81.            case 'E':
  82.                if (rstderr) {
  83.                    fprintf(stderr, USAGESTRING, argv[0]);
  84.                    exit(1);
  85.                }
  86.                ++rstderr;
  87.                ++cstderr;
  88.                stderrfile = optarg;
  89.                if (appendopt) {
  90.                    appendopt = 0;
  91.                    ++astderr;
  92.                }
  93.                break;
  94.            case 'O':
  95.                if (rstdout) {
  96.                    fprintf(stderr, USAGESTRING, argv[0]);
  97.                    exit(1);
  98.                }
  99.                ++rstdout;
  100.                ++cstdout;
  101.                stdoutfile = optarg;
  102.                if (appendopt) {
  103.                    appendopt = 0;
  104.                    ++astdout;
  105.                }
  106.                break;
  107.            default:
  108.                fprintf(stderr, USAGESTRING, argv[0]);
  109.                exit(1);
  110.                break;
  111.        }
  112.     }
  113.     if (rstdout) {
  114.        if ((fd = open(stdoutfile, (astdout ?
  115.                                    (O_WRONLY | O_APPEND | O_CREAT) :
  116.                                    (cstdout ?
  117.                                     (O_WRONLY | O_CREAT | O_TRUNC) :
  118.                                     (O_WRONLY | O_CREAT | O_EXCL))),
  119.                       0644)) < 0) {
  120.            fprintf(stderr,
  121.                    "%s: couldn't open destination %s for standard output\n",
  122.                    argv[0], stdoutfile);
  123.            exit(2);
  124.        }
  125.        if (dup2(fd, 1) < 0) {
  126.            fprintf(stderr,
  127.                    "%s: couldn't open destination %s for standard output\n",
  128.                    argv[0], stdoutfile);
  129.            exit(2);
  130.        }
  131.        close(fd);
  132.     }
  133.     if (rstderr) {
  134.        if ((fd = open(stderrfile, (astderr ?
  135.                                    (O_WRONLY | O_APPEND | O_CREAT) :
  136.                                    (cstderr ?
  137.                                     (O_WRONLY | O_CREAT | O_TRUNC) :
  138.                                     (O_WRONLY | O_CREAT | O_EXCL))),
  139.                       0644)) < 0) {
  140.            fprintf(stderr,
  141.                    "%s: couldn't open destination %s for standard error\n",
  142.                    argv[0], stderrfile);
  143.            exit(2);
  144.        }
  145.        if (dup2(fd, 2) < 0) {
  146.            fprintf(stderr,
  147.                    "%s: couldn't open destination %s for standard error\n",
  148.                    argv[0], stderrfile);
  149.            exit(2);
  150.        }
  151.        close(fd);
  152.     }
  153.     execvp(argv[optind], argv + optind);
  154. }
  155.