| 1 |
/* |
| 2 |
Copyright (c) 1990-1999 Info-ZIP. All rights reserved. |
| 3 |
|
| 4 |
See the accompanying file LICENSE, version 1999-Oct-05 or later |
| 5 |
(the contents of which are also included in zip.h) for terms of use. |
| 6 |
If, for some reason, both of these files are missing, the Info-ZIP license |
| 7 |
also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html |
| 8 |
*/ |
| 9 |
/* |
| 10 |
* __setargv.c - command argument expander |
| 11 |
* |
| 12 |
* Author : Jean-Michel Dubois |
| 13 |
* Date : 09/26/92 |
| 14 |
* |
| 15 |
* Function: Expands the command line arguments by replacing any filename |
| 16 |
* including wilcards by the sorted list of matching files name. |
| 17 |
* Strings beginning by a dash are considered as options and left |
| 18 |
* unchanged. |
| 19 |
* |
| 20 |
* Syntax : void _setargv(int *argc, char ***argv); |
| 21 |
* |
| 22 |
* Returns : new argc. Caller's argc and argv are updated. |
| 23 |
* If a insufficient memory condition occurs, return 0 and errno |
| 24 |
* is set to ENOMEM. |
| 25 |
* |
| 26 |
* Example : |
| 27 |
* main(int argc, char **argv) |
| 28 |
* { |
| 29 |
* if (_setargv(&argc, &argv)) { |
| 30 |
* ... |
| 31 |
*/ |
| 32 |
#pragma library |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <malloc.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <scr.h> |
| 40 |
#include <peek.h> |
| 41 |
|
| 42 |
/* Allocate argv array in 16 entries chunks */ |
| 43 |
|
| 44 |
static int allocarg(int n, int l, char ***nargv, char *s) |
| 45 |
{ |
| 46 |
if ((n+1) > l) { /* If array full */ |
| 47 |
l += 16; /* increase size and reallocate */ |
| 48 |
if (!(*nargv = (char **) realloc(*nargv,l * sizeof (void *)))) { |
| 49 |
errno = _errnum = ENOMEM; /* Not enough memory */ |
| 50 |
return 0; |
| 51 |
} |
| 52 |
} |
| 53 |
(*nargv)[n] = strdup(s); /* Save argument */ |
| 54 |
return l; /* Return new maxsize */ |
| 55 |
} |
| 56 |
|
| 57 |
/* Comparison function for qsort */ |
| 58 |
|
| 59 |
static int sortcmp(char **p, char **q) |
| 60 |
{ |
| 61 |
return stricmp(*p,*q); |
| 62 |
} |
| 63 |
|
| 64 |
/* Main body of the function */ |
| 65 |
|
| 66 |
int _setargv(int *argc, char ***argv) |
| 67 |
{ |
| 68 |
register int nargc; /* New arguments counter */ |
| 69 |
char **nargv; /* New arguments pointers */ |
| 70 |
register int i, l, base; |
| 71 |
char *p, *q, *r; |
| 72 |
char path[FILENAME_MAX]; |
| 73 |
|
| 74 |
_errnum = 0; |
| 75 |
nargc = 0; /* Initialise counter, size counter */ |
| 76 |
l = *argc; /* and new argument vector to the */ |
| 77 |
/* current argv array size */ |
| 78 |
|
| 79 |
if ((nargv = (char **) calloc((size_t) *argc, sizeof (void *))) != NULL) { |
| 80 |
/* For each initial argument */ |
| 81 |
for (i = 0; i < *argc; i++) { |
| 82 |
q = (*argv)[i]; |
| 83 |
if (q[0] == '-' || ! testwild(q)) { |
| 84 |
/* if it begins with a dash or doesnt include |
| 85 |
* wildcard simply add it to the new array |
| 86 |
*/ |
| 87 |
if (! (l = allocarg(nargc, l, &nargv, q))) |
| 88 |
return 0; /* Not enough memory */ |
| 89 |
nargc++; |
| 90 |
} else { |
| 91 |
/* else keep current counter for qsort */ |
| 92 |
base = nargc; |
| 93 |
/* open directory with argument */ |
| 94 |
diropen(q); |
| 95 |
while ((r = dirread()) != NULL) { |
| 96 |
/* reduce path to given one */ |
| 97 |
if ((p = strrchr(q, '/')) != NULL) { |
| 98 |
strncpy(path, q, p-q+1); |
| 99 |
path[p-q+1] = '\0'; |
| 100 |
} else |
| 101 |
path[0] = '\0'; |
| 102 |
|
| 103 |
if ((p = strrchr(r, '/')) != NULL) |
| 104 |
strcat(path, p+1); |
| 105 |
else |
| 106 |
strcat(path, r); |
| 107 |
|
| 108 |
if (peekscr(&SCR->searchseq[1]) == 255 |
| 109 |
&& strchr(q, ':') == NULL) { |
| 110 |
*strchr(path, ':') = '\0'; |
| 111 |
} |
| 112 |
/* and add each matching filename. */ |
| 113 |
if (! (l = allocarg(nargc,l,&nargv,path))) |
| 114 |
return 0;/* Not enough memory */ |
| 115 |
nargc++; |
| 116 |
} |
| 117 |
if (nargc == base) { |
| 118 |
/* if no match found include wild card name */ |
| 119 |
if (! (l = allocarg(nargc, l, &nargv, q))) |
| 120 |
return 0; /* Not enough memory */ |
| 121 |
nargc++; |
| 122 |
} else if ((nargc - base) > 1) |
| 123 |
/* If more than one file name matchs */ |
| 124 |
/* sort arguments. */ |
| 125 |
qsort(&(nargv[base]),(size_t)nargc-base, |
| 126 |
sizeof(void *),sortcmp); |
| 127 |
dirclose(); |
| 128 |
} |
| 129 |
} |
| 130 |
/* Update caller's parameters */ |
| 131 |
*argc = nargc; |
| 132 |
*argv = nargv; |
| 133 |
/* and sign on success */ |
| 134 |
return nargc; |
| 135 |
} |
| 136 |
|
| 137 |
/* If it is not possible to allocate initial array, sign on error */ |
| 138 |
_errnum = ENOMEM; |
| 139 |
return 0; |
| 140 |
} |