| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <find.h> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define MAXBUFSIZE 8192 |
| 20 |
|
| 21 |
#ifndef HAVE_UNISTD_H |
| 22 |
#define F_OK 0 |
| 23 |
#define X_OK 1 |
| 24 |
#define W_OK 2 |
| 25 |
#define R_OK 4 |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
#ifdef D_OK |
| 30 |
#undef D_OK |
| 31 |
#endif |
| 32 |
#define D_OK 256 |
| 33 |
|
| 34 |
#ifdef ANSI_FUNC |
| 35 |
static int |
| 36 |
amparse (char *mode) |
| 37 |
#else |
| 38 |
static int amparse(mode) |
| 39 |
char *mode; |
| 40 |
#endif |
| 41 |
{ |
| 42 |
int xmode = 0; |
| 43 |
|
| 44 |
xmode |= ( strpbrk(mode, "r") != NULL ? R_OK : 0 ); |
| 45 |
xmode |= ( strpbrk(mode, "w") != NULL ? W_OK : 0 ); |
| 46 |
xmode |= ( strpbrk(mode, "x") != NULL ? X_OK : 0 ); |
| 47 |
xmode |= ( strpbrk(mode, "f") != NULL ? F_OK : 0 ); |
| 48 |
xmode |= ( strpbrk(mode, "d") != NULL ? D_OK : 0 ); |
| 49 |
|
| 50 |
return xmode; |
| 51 |
} |
| 52 |
|
| 53 |
#ifdef ANSI_FUNC |
| 54 |
static char * |
| 55 |
findpath (char *name, char *mode, char *path) |
| 56 |
#else |
| 57 |
static char *findpath(name, mode, path) |
| 58 |
char *name; |
| 59 |
char *mode; |
| 60 |
char *path; |
| 61 |
#endif |
| 62 |
{ |
| 63 |
char pathbuff[MAXBUFSIZE]; |
| 64 |
char namebuff[MAXBUFSIZE]; |
| 65 |
char tempbuff[MAXBUFSIZE]; |
| 66 |
char backmode[MAXBUFSIZE]; |
| 67 |
char *here, *found; |
| 68 |
int len; |
| 69 |
int mark = 0; |
| 70 |
int skip = strpbrk(mode, ">") != NULL; |
| 71 |
int pick = strpbrk(mode, "<") != NULL; |
| 72 |
|
| 73 |
if ( skip && pick ) return NULL; |
| 74 |
|
| 75 |
if ( (path==NULL) || ( name[0] == '.' && name[1] == '/' ) || name[0] == '/' ) |
| 76 |
return Access(name, mode); |
| 77 |
|
| 78 |
strncpy(pathbuff, path, MAXBUFSIZE-1); |
| 79 |
pathbuff[MAXBUFSIZE-1] = '\0'; |
| 80 |
path = pathbuff; |
| 81 |
|
| 82 |
if ( (here = strpbrk(pathbuff, ":;")) ) { |
| 83 |
mark = *here; |
| 84 |
*here++ = '\0'; |
| 85 |
} |
| 86 |
while ( path ) { |
| 87 |
|
| 88 |
if ( strchr(path, '$') ) { |
| 89 |
|
| 90 |
ExpandEnv(path, tempbuff, MAXBUFSIZE); |
| 91 |
|
| 92 |
if( !strchr(tempbuff, '$') ){ |
| 93 |
if ( (found = findpath(name, mode, tempbuff)) ) |
| 94 |
return found; |
| 95 |
} |
| 96 |
} else { |
| 97 |
if ( !skip ) { |
| 98 |
if ( !strcmp(".", path) ) path[0] = '\0'; |
| 99 |
|
| 100 |
strncpy(namebuff, path, MAXBUFSIZE-1); |
| 101 |
namebuff[MAXBUFSIZE-1] = '\0'; |
| 102 |
len = strlen(namebuff); |
| 103 |
if ( namebuff[0] && namebuff[len-1] != '/' ){ |
| 104 |
if( (len+1) <= (MAXBUFSIZE-1) ){ |
| 105 |
strcat(namebuff, "/"); |
| 106 |
len++; |
| 107 |
} |
| 108 |
|
| 109 |
else |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
if( len+strlen(name) <= MAXBUFSIZE-1 ) |
| 113 |
strcat(namebuff, name); |
| 114 |
|
| 115 |
else |
| 116 |
return NULL; |
| 117 |
|
| 118 |
if ( (found = Access(namebuff, mode)) ) |
| 119 |
return found; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ( mark == ';' ) { |
| 124 |
if ( skip ) { |
| 125 |
skip = 0; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
strncpy(backmode, mode, MAXBUFSIZE-1); |
| 130 |
backmode[MAXBUFSIZE-1] = '\0'; |
| 131 |
mode = backmode; |
| 132 |
} |
| 133 |
if ( pick ) return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
path = here; |
| 137 |
if ( here && (here = strpbrk(here, ":;")) ) { |
| 138 |
mark = *here; |
| 139 |
*here++ = '\0'; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#ifdef ANSI_FUNC |
| 159 |
char * |
| 160 |
ResolvePath (char *ibuf, char *obuf, int maxlen) |
| 161 |
#else |
| 162 |
char *ResolvePath(ibuf, obuf, maxlen) |
| 163 |
char *ibuf; |
| 164 |
char *obuf; |
| 165 |
int maxlen; |
| 166 |
#endif |
| 167 |
{ |
| 168 |
char path[MAXBUFSIZE]; |
| 169 |
char *part[MAXBUFSIZE]; |
| 170 |
char *tbuf; |
| 171 |
int i, j; |
| 172 |
int len; |
| 173 |
int npart=0; |
| 174 |
|
| 175 |
|
| 176 |
if( strchr(ibuf, '/') == NULL ){ |
| 177 |
strncpy(obuf, ibuf, maxlen-1); |
| 178 |
obuf[maxlen-1] = '\0'; |
| 179 |
return(obuf); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
if( !strcmp(ibuf, "/") || !strcmp(ibuf, "/.") ){ |
| 184 |
strncpy(obuf, "/", maxlen-1); |
| 185 |
obuf[maxlen-1] = '\0'; |
| 186 |
return(obuf); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
if( (*ibuf == '.') || ( (strchr(ibuf, '/') != NULL) && (*ibuf != '/') ) ){ |
| 191 |
getcwd(path, MAXBUFSIZE); |
| 192 |
} |
| 193 |
else{ |
| 194 |
*path = '\0'; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
len = strlen(path) + strlen(ibuf) + 1; |
| 199 |
tbuf = (char *)xmalloc(len+1); |
| 200 |
if( *path ){ |
| 201 |
strcpy(tbuf, path); |
| 202 |
strcat(tbuf, "/"); |
| 203 |
strcat(tbuf, ibuf); |
| 204 |
} |
| 205 |
else{ |
| 206 |
strcpy(tbuf, ibuf); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
for(i=0; i<len; i++){ |
| 212 |
if( tbuf[i] == '/' ){ |
| 213 |
tbuf[i] = '\0'; |
| 214 |
|
| 215 |
if( tbuf[i+1] == '/' ) continue; |
| 216 |
part[npart] = &tbuf[i+1]; |
| 217 |
npart++; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
for(i=0; i<npart; i++){ |
| 223 |
|
| 224 |
if( !strcmp(part[i], ".") ){ |
| 225 |
part[i] = NULL; |
| 226 |
} |
| 227 |
|
| 228 |
else if( !strcmp(part[i], "..") ){ |
| 229 |
part[i] = NULL; |
| 230 |
for(j=i-1; j>=0; j--){ |
| 231 |
if( part[j] ){ |
| 232 |
part[j] = NULL; |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
*obuf = '\0'; |
| 241 |
len = 0; |
| 242 |
for(i=0; i<npart; i++){ |
| 243 |
if( part[i] != NULL ){ |
| 244 |
if( len+(int)strlen(part[i])+1 <= maxlen-1 ){ |
| 245 |
strcat(obuf, "/"); |
| 246 |
strcat(obuf, part[i]); |
| 247 |
len += strlen(part[i])+1; |
| 248 |
} |
| 249 |
else{ |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
if( tbuf ) free(tbuf); |
| 257 |
|
| 258 |
|
| 259 |
return(obuf); |
| 260 |
} |
| 261 |
|
| 262 |
#ifdef ANSI_FUNC |
| 263 |
void |
| 264 |
ExpandEnv (char *name, char *envname, int maxlen) |
| 265 |
#else |
| 266 |
void ExpandEnv(name, envname, maxlen) |
| 267 |
char *name; |
| 268 |
char *envname; |
| 269 |
int maxlen; |
| 270 |
#endif |
| 271 |
{ |
| 272 |
char brace[2]; |
| 273 |
char tbuf[MAXBUFSIZE]; |
| 274 |
char *fullname=NULL; |
| 275 |
char *mip; |
| 276 |
char *ip; |
| 277 |
char *s; |
| 278 |
int len; |
| 279 |
int i=0, j=0; |
| 280 |
|
| 281 |
|
| 282 |
if( !(fullname=(char *)xcalloc(maxlen, sizeof(char))) ) return; |
| 283 |
|
| 284 |
|
| 285 |
for(ip=name; *ip; ip++){ |
| 286 |
|
| 287 |
if( *ip != '$' ){ |
| 288 |
fullname[i++] = *ip; |
| 289 |
fullname[i] = '\0'; |
| 290 |
} |
| 291 |
else{ |
| 292 |
mip = ip; |
| 293 |
|
| 294 |
ip++; |
| 295 |
|
| 296 |
if( *ip == '{' ){ |
| 297 |
brace[0] = '{'; |
| 298 |
ip++; |
| 299 |
} |
| 300 |
else if( *ip == '(' ){ |
| 301 |
brace[0] = '('; |
| 302 |
ip++; |
| 303 |
} |
| 304 |
else |
| 305 |
brace[0] = '\0'; |
| 306 |
|
| 307 |
for(*tbuf='\0', j=0; |
| 308 |
(!isspace((int)*ip)) && (*ip != '"') && (*ip != '\'') && (*ip); |
| 309 |
ip++){ |
| 310 |
|
| 311 |
if( *brace && *ip == (*brace == '(' ? ')' : '}') ){ |
| 312 |
ip++; |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
if( *ip == '/' ){ |
| 317 |
break; |
| 318 |
} |
| 319 |
tbuf[j++] = *ip; |
| 320 |
tbuf[j] = '\0'; |
| 321 |
} |
| 322 |
|
| 323 |
ip--; |
| 324 |
if( (s = (char *)getenv(tbuf)) != NULL ){ |
| 325 |
i += strlen(s); |
| 326 |
if( i <= maxlen ) |
| 327 |
strcat(fullname, s); |
| 328 |
} |
| 329 |
|
| 330 |
else{ |
| 331 |
len = ip - mip + 1; |
| 332 |
i += len; |
| 333 |
if( i <= maxlen ) |
| 334 |
strncat(fullname, mip, len); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
strncpy(envname, fullname, maxlen); |
| 341 |
|
| 342 |
|
| 343 |
if( fullname ) xfree(fullname); |
| 344 |
} |
| 345 |
|
| 346 |
#ifdef ANSI_FUNC |
| 347 |
char * |
| 348 |
Access (char *name, char *mode) |
| 349 |
#else |
| 350 |
char *Access (name, mode) |
| 351 |
char *name; |
| 352 |
char *mode; |
| 353 |
#endif |
| 354 |
{ |
| 355 |
struct stat info; |
| 356 |
char fullname[MAXBUFSIZE]; |
| 357 |
char AccessName[MAXBUFSIZE]; |
| 358 |
|
| 359 |
|
| 360 |
ExpandEnv(name, fullname, MAXBUFSIZE); |
| 361 |
if ( stat(fullname, &info) !=0 ) return NULL; |
| 362 |
|
| 363 |
#if HAVE_MINGW32==0 && HAVE_CYGWIN==0 |
| 364 |
if ( mode ) { |
| 365 |
int m = amparse(mode); |
| 366 |
|
| 367 |
|
| 368 |
if ( (m & D_OK) && !(info.st_mode & S_IFDIR) ) return NULL; |
| 369 |
if ( !(m & D_OK) && (info.st_mode & S_IFDIR) ) return NULL; |
| 370 |
|
| 371 |
if ( getuid() == info.st_uid ) { |
| 372 |
if ( m & R_OK && !(info.st_mode & S_IRUSR) ) return NULL; |
| 373 |
if ( m & W_OK && !(info.st_mode & S_IWUSR) ) return NULL; |
| 374 |
if ( m & X_OK && !(info.st_mode & S_IXUSR) ) return NULL; |
| 375 |
} else |
| 376 |
if ( getgid() == info.st_gid ) { |
| 377 |
if ( m & R_OK && !(info.st_mode & S_IRGRP) ) return NULL; |
| 378 |
if ( m & W_OK && !(info.st_mode & S_IWGRP) ) return NULL; |
| 379 |
if ( m & X_OK && !(info.st_mode & S_IXGRP) ) return NULL; |
| 380 |
} else { |
| 381 |
if ( m & R_OK && !(info.st_mode & S_IROTH) ) return NULL; |
| 382 |
if ( m & W_OK && !(info.st_mode & S_IWOTH) ) return NULL; |
| 383 |
if ( m & X_OK && !(info.st_mode & S_IXOTH) ) return NULL; |
| 384 |
} |
| 385 |
} |
| 386 |
#endif |
| 387 |
|
| 388 |
ResolvePath(fullname, AccessName, MAXBUFSIZE); |
| 389 |
return(xstrdup(AccessName)); |
| 390 |
} |
| 391 |
|
| 392 |
#ifdef ANSI_FUNC |
| 393 |
char * |
| 394 |
Find (char *name, char *mode, char *exten, char *path) |
| 395 |
#else |
| 396 |
char *Find (name, mode, exten, path) |
| 397 |
char *name; |
| 398 |
char *mode; |
| 399 |
char *exten; |
| 400 |
char *path; |
| 401 |
#endif |
| 402 |
{ |
| 403 |
char extenbuff[MAXBUFSIZE]; |
| 404 |
char namebuff[MAXBUFSIZE]; |
| 405 |
char *here, *found; |
| 406 |
int len; |
| 407 |
|
| 408 |
|
| 409 |
if( !name || !*name ) |
| 410 |
return NULL; |
| 411 |
|
| 412 |
|
| 413 |
if( !strncmp(name, "ftp://", 6) || |
| 414 |
!strncmp(name, "http://", 7) ){ |
| 415 |
return(xstrdup(name)); |
| 416 |
} |
| 417 |
|
| 418 |
if ( exten == NULL ) |
| 419 |
return findpath(name, mode, path); |
| 420 |
|
| 421 |
strncpy(extenbuff, exten, MAXBUFSIZE-1); |
| 422 |
extenbuff[MAXBUFSIZE-1] = '\0'; |
| 423 |
exten = extenbuff; |
| 424 |
|
| 425 |
if ( (here = strpbrk(extenbuff, ":;")) ) *here++ = '\0'; |
| 426 |
|
| 427 |
while ( exten ) { |
| 428 |
if ( exten[0] == '$' ) { |
| 429 |
if ( (exten = (char *)getenv(&exten[1])) ) |
| 430 |
if ( (found = Find(name, mode, exten, path)) ) |
| 431 |
return found; |
| 432 |
} else { |
| 433 |
char *e = strstr(name, exten); |
| 434 |
|
| 435 |
strncpy(namebuff, name, MAXBUFSIZE-1); |
| 436 |
namebuff[MAXBUFSIZE-1] = '\0'; |
| 437 |
len = strlen(namebuff); |
| 438 |
if ( (e==NULL) || ( e && *(e + len)) ){ |
| 439 |
if( len+strlen(exten) <= MAXBUFSIZE-1 ) |
| 440 |
strcat(namebuff, exten); |
| 441 |
|
| 442 |
else |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
|
| 446 |
if ( (found = findpath(namebuff, mode, path)) ) |
| 447 |
return found; |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
exten = here; |
| 452 |
if ( here && (here = strpbrk(here, ":;")) ) *here++ = '\0'; |
| 453 |
} |
| 454 |
|
| 455 |
return NULL; |
| 456 |
} |