home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n01.zip / EXP.C < prev    next >
C/C++ Source or Header  |  1988-07-22  |  3KB  |  109 lines

  1. /*  exp.c
  2.  *  For MSC 4.0/5.0/QuickC: cl exp.c
  3.  */
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. #include<ctype.h>
  8.  
  9. #ifndef TRUE
  10. #define TRUE 1
  11. #endif
  12.  
  13. #ifndef FALSE
  14. #define FALSE 0
  15. #endif
  16.  
  17. #define DOS_COMMAND_LINE 128
  18. #define LPAREN '('
  19. #define RPAREN ')'
  20. #define PERIOD '.'
  21. #define BLANK ' '
  22.  
  23. main(argc, argv)
  24. int argc;
  25. char **argv;
  26. {
  27.   int i, echo_only = FALSE;
  28.   char oldcommandline[DOS_COMMAND_LINE], newcommandline[DOS_COMMAND_LINE];
  29.   char *lparen, *rparen, *period, *first_arg, *last_arg, *nextdelim;
  30.   
  31.   if(argc < 2)                    /* check usage */
  32.     print_usage();
  33.  
  34.     /* check for echo only argument, and if so, set flag, increment
  35.       arg pointer, and decrement the arg count
  36.  */
  37.   if((argv[1][0] == '-' || argv[1][0] == '/') && toupper(argv[1][1]) == 'E')
  38.   {
  39.     echo_only = TRUE;
  40.     argv++;
  41.     argc--;
  42.   }
  43.     /* put all command-line arguments into one buffer */
  44.   for( i = 1, oldcommandline[0] = NULL; i < argc; i++)
  45.   {
  46.     strcat(oldcommandline,argv[i]);
  47.     strcat(oldcommandline," ");
  48.   }
  49.   lparen = strchr(oldcommandline, LPAREN);
  50.   rparen = strchr(oldcommandline, RPAREN);
  51.   period = strchr(oldcommandline, PERIOD);
  52.  
  53.     /* check syntax: if no parens or if parens reversed */
  54.   if(!lparen || !rparen || (rparen < lparen))
  55.     print_usage();
  56.  
  57.     /* check syntax: if period supplied and in middle */
  58.   if(period && ((period < rparen) && (period > lparen)) )
  59.     print_usage();
  60.  
  61.     /* set last_arg to the remainder of the command line after our
  62.       expanded filenames/extensions
  63.  */
  64.   last_arg = (rparen+1);
  65.  
  66.     /* copy up to the first paren into the new command buffer */
  67.   strncpy(newcommandline, oldcommandline, lparen-oldcommandline);
  68.   newcommandline[lparen-oldcommandline] = NULL;
  69.  
  70.     /* set first_arg to where the first argument will start each time */
  71.   first_arg = &newcommandline[strlen(newcommandline)];
  72.   lparen++;                /* point to first expansion */
  73.  
  74.     /* main loop:
  75.       pick off each filename/extension,
  76.       copy it into the buffer,
  77.       tack on additional arguments
  78.       run the command.
  79.  */
  80.   while(TRUE)
  81.   {
  82.     *first_arg = NULL;
  83.     nextdelim = strpbrk(lparen, ",)");      /* find the next , or ) */
  84.     *nextdelim = NULL;              /* NULL terminate */
  85.     strcat(first_arg, lparen);          /* add to the command */
  86.     strcat(first_arg, last_arg);          /* add to the command */
  87.  
  88.     if(echo_only)
  89.       printf("\nexp: %s",newcommandline);     /* echo the command */
  90.     else if(system(newcommandline))         /* execute the command */
  91.         printf("\nexp: error executing \"%s\"",newcommandline);
  92.  
  93.     lparen = nextdelim+1;             /* move to next arg */
  94.     while(*lparen == BLANK)
  95.       lparen++;
  96.     if(lparen > rparen)
  97.       break;
  98.   }
  99.   exit(0);
  100. }
  101.  
  102. print_usage()
  103. {
  104.   printf("\nexp [-e] DOScommand (filename1,...,filenameN).ext");
  105.   printf("\nexp [-e] DOScommand filename.(ext1,...,extN)");
  106.   exit(1);
  107. }
  108.  
  109.