home *** CD-ROM | disk | FTP | other *** search
- /*_ wildcard.c Fri Jan 29 1988 Modified by: Walter Bright */
- /* Written by Walter Bright */
- /* To compile (with Datalight C): */
- /* DLC -mci wildcard _main.obj */
-
- #include <stdio.h>
- #include <stdlib.h>
-
- #define TRUE 1
- #define FALSE 0
-
- #if DLC
- int _okbigbuf = 0; /* Use as little memory as possible */
- #endif
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char **newargv;
- int i; /* how many times command was executed */
-
- if (argc < 2)
- {
- usage();
- exit(1);
- }
- newargv = (char **) malloc(argc * sizeof(char *));
- if (newargv == NULL)
- {
- printf("out of memory\n");
- exit(1);
- }
- newargv[0] = argv[1];
- for (i = 0; 1; i++)
- {
- int a; /* index into argv[] */
- int n; /* index into newargv[] */
- int gotarg; /* TRUE if we saw a non - arg */
- int status; /* return status from spawn() */
-
- gotarg = FALSE;
- n = 1;
- for (a = 2; a < argc; )
- {
- L1:
- if (argv[a][0] == '-') /* if arg starts with a - */
- /* pass it through after stripping the - */
- newargv[n++] = &argv[a++][1];
- else
- {
- int j;
-
- for (j = a + i; a < j; a++)
- {
- if (a == argc)
- goto doit;
- if (argv[a][0] == '-')
- goto L1;
- }
- if (a < argc && argv[a][0] != '-')
- {
- newargv[n++] = argv[a];
- gotarg = TRUE;
- }
- /* Scan to end of args or next - arg */
- while (a < argc && argv[a][0] != '-')
- a++;
- }
- }
- doit:
- if (!gotarg && i) /* if no file names and not first time */
- break;
- newargv[n] = NULL; /* terminate list of args */
-
- /* Print out the command we are going to attempt */
- for (n = 0; newargv[n]; n++)
- printf("%s ",newargv[n]);
- printf("\n");
- fflush(stdout);
-
- status = spawnvp(0,newargv[0],newargv);
- if (status == -1)
- {
- printf("'%s' failed to execute\n",argv[1]);
- exit(1);
- }
- if (status != 0)
- { printf("--- errorlevel %d\n",status);
- exit((status & ~255) ? 255 : status);
- }
- }
- }
-
- usage()
- {
- printf("\
- Wildcard execution of a command. Written by Walter Bright\n\
- Use:\n\
- WILDCARD progname arguments...\n\
- \n\
- The arguments are wildcard expanded. Progname is executed once for each\n\
- argument. Arguments preceded by a - are passed through to progname (with\n\
- the leading - removed), and do not influence the number of times progname\n\
- is executed.\
- ");
- }
-
- #if DLC
-
- /* Prevent exit() and fclose() from being linked in from library */
- /* (to conserve the size of the output file). */
-
- void exit(exitstatus)
- int exitstatus;
- {
- _exit(exitstatus);
- }
- #endif
-