home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / sysutl / wildcard.arc / WILDCARD.C next >
C/C++ Source or Header  |  1988-08-01  |  3KB  |  120 lines

  1. /*_ wildcard.c   Fri Jan 29 1988   Modified by: Walter Bright */
  2. /* Written by Walter Bright         */
  3. /* To compile (with Datalight C):    */
  4. /*    DLC -mci wildcard _main.obj    */
  5.  
  6. #include    <stdio.h>
  7. #include    <stdlib.h>
  8.  
  9. #define TRUE    1
  10. #define FALSE    0
  11.  
  12. #if DLC
  13. int _okbigbuf = 0;    /* Use as little memory as possible    */
  14. #endif
  15.  
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     char **newargv;
  21.     int i;            /* how many times command was executed    */
  22.  
  23.     if (argc < 2)
  24.     {
  25.     usage();
  26.     exit(1);
  27.     }
  28.     newargv = (char **) malloc(argc * sizeof(char *));
  29.     if (newargv == NULL)
  30.     {
  31.     printf("out of memory\n");
  32.     exit(1);
  33.     }
  34.     newargv[0] = argv[1];
  35.     for (i = 0; 1; i++)
  36.     {
  37.     int a;                /* index into argv[]        */
  38.     int n;                /* index into newargv[]        */
  39.     int gotarg;            /* TRUE if we saw a non - arg    */
  40.     int status;            /* return status from spawn()    */
  41.  
  42.     gotarg = FALSE;
  43.     n = 1;
  44.     for (a = 2; a < argc; )
  45.     {
  46.     L1:
  47.         if (argv[a][0] == '-')    /* if arg starts with a -    */
  48.         /* pass it through after stripping the -  */
  49.         newargv[n++] = &argv[a++][1];
  50.         else
  51.         {
  52.         int j;
  53.  
  54.         for (j = a + i; a < j; a++)
  55.         {
  56.             if (a == argc)
  57.             goto doit;
  58.             if (argv[a][0] == '-')
  59.             goto L1;
  60.         }
  61.         if (a < argc && argv[a][0] != '-')
  62.         {
  63.             newargv[n++] = argv[a];
  64.             gotarg = TRUE;
  65.         }
  66.         /* Scan to end of args or next - arg    */
  67.         while (a < argc && argv[a][0] != '-')
  68.             a++;
  69.         }
  70.     }
  71.     doit:
  72.     if (!gotarg && i)    /* if no file names and not first time    */
  73.         break;
  74.     newargv[n] = NULL;        /* terminate list of args    */
  75.  
  76.     /* Print out the command we are going to attempt        */
  77.     for (n = 0; newargv[n]; n++)
  78.         printf("%s ",newargv[n]);
  79.     printf("\n");
  80.     fflush(stdout);
  81.  
  82.     status = spawnvp(0,newargv[0],newargv);
  83.     if (status == -1)
  84.     {
  85.         printf("'%s' failed to execute\n",argv[1]);
  86.         exit(1);
  87.     }
  88.     if (status != 0)
  89.     {   printf("--- errorlevel %d\n",status);
  90.         exit((status & ~255) ? 255 : status);
  91.     }
  92.     }
  93. }
  94.  
  95. usage()
  96. {
  97.     printf("\
  98. Wildcard execution of a command. Written by Walter Bright\n\
  99. Use:\n\
  100.     WILDCARD progname arguments...\n\
  101. \n\
  102. The arguments are wildcard expanded. Progname is executed once for each\n\
  103. argument. Arguments preceded by a - are passed through to progname (with\n\
  104. the leading - removed), and do not influence the number of times progname\n\
  105. is executed.\
  106. ");
  107. }
  108.  
  109. #if DLC
  110.  
  111. /* Prevent exit() and fclose() from being linked in from library    */
  112. /* (to conserve the size of the output file).                */
  113.  
  114. void exit(exitstatus)
  115. int exitstatus;
  116. {
  117.     _exit(exitstatus);
  118. }
  119. #endif
  120.