home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ERRFIX.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  63 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** ERRFIX.C - redirect stderr to some other file under MS-DOS
  5. **
  6. ** by Bob Jarvis
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <process.h>
  13. #include "extkword.h"
  14.  
  15. #ifdef __SC__
  16.  #define SVP_CAST(x) (const char * const *)(x)
  17. #else
  18.  #define SVP_CAST(x) (x)
  19. #endif
  20.  
  21. int usage(void)
  22. {
  23.       puts("ERRFIX [filename] [prog] { {parm1} {parm2} ... {parmN} }");
  24.       puts("   Redirects stderr to another file, then invokes a program");
  25.       puts("   which will inherit the new definition of stderr.\n");
  26.       puts("Parameters:");
  27.       puts("   filename (required) - the name of the file stderr should");
  28.       puts("      be redirected to.  Output written to stderr will");
  29.       puts("      be routed to this file instead of the console.");
  30.       puts("   prog (required) - name of the program to be run.");
  31.       puts("   parm1...parmN (optional) - command-line parameters needed");
  32.       puts("      to run the program specified by the 'prog' argument.");
  33.       return 1;
  34. }
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38.       char **args = argv;
  39.  
  40.       if (3 > argc)
  41.             return usage();
  42.  
  43.       if (NULL != argv[argc]) /* may be a problem under some compilers */
  44.       {
  45.             args = malloc((argc+1) * sizeof(char *));
  46.             if (NULL == args)
  47.             {
  48.                   printf("Unable to allocate storage");
  49.                   return 2;
  50.             }
  51.  
  52.             memcpy(args, argv, argc * sizeof(char *));
  53.  
  54.             args[argc] = NULL;
  55.       }
  56.  
  57.       freopen(args[1], "w", stderr);
  58.  
  59.       spawnvp(0, args[2], SVP_CAST(&args[2]));
  60.  
  61.       return 0;
  62. }
  63.