home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / redirect.c < prev    next >
Internet Message Format  |  1994-03-04  |  3KB

  1. From: microsoft!randyn@uunet.uu.net (Randy Nevin)
  2. Newsgroups: comp.sys.ibm.pc
  3. Subject: redirecting std[in,out,err] vs. the shell
  4. Date: 26 Apr 89 15:12:15 GMT
  5. Keywords: redirection, command.com
  6.  
  7. stdin, stdout, and stderr are simply file pointers, and can be redirected on
  8. dos or unix. there is nothing magic about them that prevents redirection. the
  9. fact that the shell (sh or csh on unix, command.com on dos) does not do this
  10. for you does not prohibit you from doing it yourself. below is the code to a
  11. simple shell which redirects any of these file pointers before executing an
  12. arbitrary command. "redirect -i myin -o myout -e myerr foo arg1 arg2" will
  13. execute the dos command "foo arg1 arg2" with stdin redirected from file myin,
  14. and stdout and stderr redirected to files myout and myerr, respectively. so
  15. information is not lost, myout and myerr are opened for appending, rather
  16. than overwriting. this works on dos or unix.
  17.  
  18. /*
  19. ** redirect -- feed line to shell with stdin/stdout/stderr redirected
  20. **
  21. ** usage -- redirect [-i newin] [-o newout] [-e newerr] command
  22. **
  23. ** executes command via the shell, but redirects stdin/stdout/stderr first.
  24. ** stdout/stderr are appended, not overwritten.
  25. */
  26.  
  27. #include <stdio.h>
  28.  
  29. extern char *strcat( char *, char * );
  30. extern char *strcpy( char *, char * );
  31. extern int system( char * );
  32. extern void exit( int );
  33. extern int errno;
  34. char umsg[] = "usage: redirect [-i newin] [-o newout] [-e newerr] command\n";
  35. char emsg[] = "can't redirect %s to %s\n";
  36.  
  37. void main ( int, char ** );
  38. void main ( argc, argv )
  39.     int argc;
  40.     char **argv;
  41.     {
  42.     int result;
  43.     char buf[5120];
  44.  
  45.     argc--;  argv++;
  46.     if (!argc) {
  47.         fprintf( stderr, umsg );
  48.         exit( 0 );
  49.         }
  50.     while (**argv == '-') {
  51.         if (*(*argv+1) == 'i') {
  52.             argc--;  argv++;
  53.             if (!freopen( *argv, "r", stdin )) {
  54.                 fprintf( stderr, emsg, "stdin", *argv );
  55.                 exit( 1 );
  56.                 }
  57.             }
  58.         else if (*(*argv+1) == 'o') {
  59.             argc--;  argv++;
  60.             if (!freopen( *argv, "a", stdout )) {
  61.                 fprintf( stderr, emsg, "stdout", *argv );
  62.                 exit( 1 );
  63.                 }
  64.             }
  65.         else if (*(*argv+1) == 'e') {
  66.             argc--;  argv++;
  67.             if (!freopen( *argv, "a", stderr )) {
  68.                 fprintf( stderr, emsg, "stderr", *argv );
  69.                 exit( 1 );
  70.                 }
  71.             }
  72.         else
  73.             fprintf( stderr, "unknown option %c\n", *(*argv+1) );
  74.         argc--;  argv++;
  75.         }
  76.     if (!argc) {
  77.         fprintf( stderr, umsg );
  78.         exit( 0 );
  79.         }
  80.     strcpy( buf, *argv++ );
  81.     while (*argv)
  82.         strcat( strcat( buf, " " ), *argv++ );
  83.     if (result = system( buf ))
  84.         fprintf( stderr, "exit code = %d, errno = %d\n", result,
  85.             errno );
  86.     exit( 0 );
  87.     }
  88.  
  89. arpanet:  microsoft!randyn@beaver.cs.washington.edu
  90.           or  microsoft!randyn@uunet.uu.net
  91. uucp:     ..uw-beaver!microsoft!randyn  or  ..uunet!microsoft!randyn
  92.