home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / utils / redir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  5.1 KB  |  183 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /*
  3.  
  4.    Redir 1.0 Copyright (C) 1995 DJ Delorie (dj@delorie.com)
  5.  
  6.    Redir is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Redir is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    General Public License for more details.
  15.  
  16. */
  17.    
  18.  
  19. #include <stdio.h>
  20. #include <process.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <fcntl.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <crt0.h>
  29.  
  30. int _crt0_startup_flags = _CRT0_FLAG_DISALLOW_RESPONSE_FILES;
  31.  
  32. char **
  33. __crt0_glob_function(char *a)
  34. {
  35.   return 0;
  36. }
  37.  
  38. int time_it=0, display_exit_code=0;
  39. time_t startt, endt;
  40. int std_err_fid;
  41. FILE *std_err;
  42. int rv;
  43.  
  44. static void
  45. usage(void)
  46. {
  47.   /*               ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 */
  48.   fprintf(stderr, "Redir 1.0 Copyright (C) 1995 DJ Delorie (dj@delorie.com) - distribute freely\n");
  49.   fprintf(stderr, "NO WARRANTEE.  This program is protected by the GNU General Public License.\n");
  50.   fprintf(stderr, "Usage: redir [-i file] [-o file] [-oa file] [-e file] [-ea file]\n");
  51.   fprintf(stderr, "                [-eo] [-oe] [-x] [-t] command [args . . .]\n\n");
  52.   fprintf(stderr, "  -i file   redirect stdandard input from file\n");
  53.   fprintf(stderr, "  -o file   redirect standard output to file\n");
  54.   fprintf(stderr, "  -oa file  append standard output to file\n");
  55.   fprintf(stderr, "  -e file   redirect standard error to file\n");
  56.   fprintf(stderr, "  -ea file  append standard error to file\n");
  57.   fprintf(stderr, "  -eo       redirect standard error to standard output\n");
  58.   fprintf(stderr, "  -oe       redirect standard output to standard error\n");
  59.   fprintf(stderr, "  -x        print exit code\n");
  60.   fprintf(stderr, "  -t        print elapsed time\n");
  61.   fprintf(stderr, "  command   the program you want to run, with arguments\n\n");
  62.   fprintf(stderr, "Options are processed in the order they are encountered.\n\n");
  63.   exit(1);
  64. }
  65.  
  66. static void
  67. fatal(const char *msg, const char *fn)
  68. {
  69.   fprintf(std_err, msg, fn);
  70.   fprintf(std_err, "The error was: %s\n", strerror(errno));
  71.   exit(1);
  72. }
  73.  
  74. int
  75. main(int argc, char **argv)
  76. {
  77.  
  78.   if (argc < 2)
  79.     usage();
  80.  
  81.   std_err_fid = dup(1);
  82.   std_err = fdopen(std_err_fid, "w");
  83.  
  84.   time(&startt);
  85.  
  86.   while (argc > 1 && argv[1][0] == '-')
  87.   {
  88.     if (strcmp(argv[1], "-i")==0 && argc > 2)
  89.     {
  90.       close(0);
  91.       if (open(argv[2], O_RDONLY, 0666) != 0)
  92.     fatal("redir: attempt to redirect stdin from %s failed", argv[2]);
  93.       argc--;
  94.       argv++;
  95.     }
  96.     else if (strcmp(argv[1], "-o")==0 && argc > 2)
  97.     {
  98.       close(1);
  99.       if (open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666) != 1)
  100.     fatal("redir: attempt to redirect stdout to %s failed", argv[2]);
  101.       argc--;
  102.       argv++;
  103.     }
  104.     else if (strcmp(argv[1], "-oa")==0 && argc > 2)
  105.     {
  106.       close(1);
  107.       if (open(argv[2], O_WRONLY|O_APPEND|O_CREAT, 0666) != 1)
  108.     fatal("redir: attempt to append stdout to %s failed", argv[2]);
  109.       argc--;
  110.       argv++;
  111.     }
  112.     else if (strcmp(argv[1], "-e")==0 && argc > 2)
  113.     {
  114.       close(2);
  115.       if (open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666) != 2)
  116.     fatal("redir: attempt to redirect stderr to %s failed", argv[2]);
  117.       argc--;
  118.       argv++;
  119.     }
  120.     else if (strcmp(argv[1], "-ea")==0 && argc > 2)
  121.     {
  122.       close(2);
  123.       if (open(argv[2], O_WRONLY|O_APPEND|O_CREAT, 0666) != 2)
  124.     fatal("redir: attempt to append stderr to %s failed", argv[2]);
  125.       argc--;
  126.       argv++;
  127.     }
  128.     else if (strcmp(argv[1], "-eo")==0)
  129.     {
  130.       close(2);
  131.       if (dup2(1,2) == -1)
  132.     fatal("redir: attempt to redirect stderr to stdout failed", 0);
  133.     }
  134.     else if (strcmp(argv[1], "-oe")==0)
  135.     {
  136.       close(1);
  137.       if (dup2(2,1) == -1)
  138.     fatal("redir: attempt to redirect stdout to stderr failed", 0);
  139.     }
  140.     else if (strcmp(argv[1], "-x")==0)
  141.     {
  142.       display_exit_code = 1;
  143.     }
  144.     else if (strcmp(argv[1], "-t")==0)
  145.     {
  146.       time_it = 1;
  147.     }
  148.     else
  149.       usage();
  150.     argc--;
  151.     argv++;
  152.   }
  153.  
  154.   rv = spawnvp(P_WAIT, argv[1], argv+1);
  155.   if (rv < 0)
  156.     fatal("Error attempting to run program %s\n", argv[1]);
  157.  
  158.   if (display_exit_code)
  159.   {
  160.     if ((rv & 255) == rv)
  161.       fprintf(std_err, "Exit code: %d\n", rv & 255);
  162.     else
  163.       fprintf(std_err, "Exit code: %d (0x%04x)\n", rv & 255, rv);
  164.   }
  165.  
  166.   time(&endt);
  167.   if (time_it)
  168.   {
  169.     time_t min, sec, hour, elapsedt;
  170.     elapsedt = endt - startt;
  171.  
  172.     sec = elapsedt % 60;
  173.     min = (elapsedt / 60) % 60;
  174.     hour = elapsedt / 3600;
  175.     if (elapsedt > 59)
  176.       fprintf(std_err, "Elapsed time: %d seconds (%d:%02d:%02d)\n", elapsedt, hour, min, sec);
  177.     else
  178.       fprintf(std_err, "Elapsed time: %d seconds\n", elapsedt);
  179.   }
  180.  
  181.   return rv;
  182. }
  183.