/* * expand: routines to expand argc & argv into myargc & myargv * * contains: * expand_arglist()- expands argc & argv. * * addarg() - is a local used by expand_arglist. * * no_more_core()- crashes and dies when there's no more core. * * The function _find() is a Microsoft C function that does the appropriate * finding for OS/2 and DOS. It's not even slightly portable. */ #include #include #include /* for strdup() definition */ #include #include "glob.h" struct args_t myargs; /* * no_more_core() complains and dies when core runs out */ static no_more_core() { fprintf(stderr, "Out of memory\n"); exit(1); } /* no_more_core */ /* * addarg() is a local to add an argument to our arglist */ static addarg(name, args) char *name; struct args_t *args; { /* expand the arglist and add the arg, or die */ if ((args->argv = realloc(args->argv, (1+args->argc)*sizeof(char**))) && (args->argv[args->argc] = strdup(name))) return ++args->argc; no_more_core(); } /* addarg */ /* * funkycmp() does a strcmp on char**'s */ static int funkycmp(a, b) char **a, **b; { return strcmp(*a,*b); } /* funkycmp */ /* * expand_arglist() blows up wildcards in the argument list */ void expand_arglist(argc, argv, args) char **argv; struct args_t *args; { register i; register char *fn; register first; /* if no args_t element was supplied, use our local buffer */ if (args == (struct args_t*)0) args = &myargs; /* initialize our arglist - preload myargv with a 1-byte malloced * field, because some realloc()s don't follow the ANSIism that * lets you realloc against a null pointer. */ args->flags = args->argc = 0; if ((args->argv = malloc(1)) == (char**)0) no_more_core(); /* walk everything except argv[0] */ for (i=1; iargc; do { addarg(fn, args); args->flags |= ARGS_EXPANDED; } while (fn=glob((char*)0, (struct glob_t*)0)); qsort(args->argv+first, args->argc-first, sizeof args->argv[0], funkycmp); } else /* didn't expand the file - just copy whatever the arg is over */ addarg(argv[i], args); } if (args->argv = realloc(args->argv, (1+args->argc)*sizeof(char**))) args->argv[args->argc] = 0; else no_more_core(); } /* expandargs */