|
1 | 1 | /* var.c -- es variables ($Revision: 1.1.1.1 $) */ |
| 2 | +/* stdgetenv is based on the FreeBSD getenv */ |
2 | 3 |
|
3 | 4 | #include "es.h" |
4 | 5 | #include "gc.h" |
@@ -392,9 +393,6 @@ extern void initvars(void) { |
392 | 393 | vars = mkdict(); |
393 | 394 | noexport = NULL; |
394 | 395 | env = mkvector(ENVSIZE); |
395 | | -#if LOCAL_GETENV |
396 | | - initgetenv(); |
397 | | -#endif |
398 | 396 | } |
399 | 397 |
|
400 | 398 | /* importvar -- import a single environment variable */ |
@@ -451,6 +449,64 @@ static void importvar(char *name0, char *value) { |
451 | 449 | } |
452 | 450 |
|
453 | 451 | #if LOCAL_GETENV |
| 452 | +static char *stdgetenv(const char *); |
| 453 | +static char *esgetenv(const char *); |
| 454 | +static char *(*realgetenv)(const char *) = stdgetenv; |
| 455 | + |
| 456 | +/* esgetenv -- fake version of getenv for readline (or other libraries) */ |
| 457 | +static char *esgetenv(const char *name) { |
| 458 | + List *value = varlookup(name, NULL); |
| 459 | + if (value == NULL) |
| 460 | + return NULL; |
| 461 | + else { |
| 462 | + char *export; |
| 463 | + static Dict *envdict; |
| 464 | + static Boolean initialized = FALSE; |
| 465 | + Ref(char *, string, NULL); |
| 466 | + |
| 467 | + gcdisable(); |
| 468 | + if (!initialized) { |
| 469 | + initialized = TRUE; |
| 470 | + envdict = mkdict(); |
| 471 | + globalroot(&envdict); |
| 472 | + } |
| 473 | + |
| 474 | + string = dictget(envdict, name); |
| 475 | + if (string != NULL) |
| 476 | + efree(string); |
| 477 | + |
| 478 | + export = str("%W", value); |
| 479 | + string = ealloc(strlen(export) + 1); |
| 480 | + strcpy(string, export); |
| 481 | + envdict = dictput(envdict, (char *) name, string); |
| 482 | + |
| 483 | + gcenable(); |
| 484 | + RefReturn(string); |
| 485 | + } |
| 486 | +} |
| 487 | + |
| 488 | +static char *stdgetenv(const char *name) { |
| 489 | + extern char **environ; |
| 490 | + register int len; |
| 491 | + register const char *np; |
| 492 | + register char **p, *c; |
| 493 | + |
| 494 | + if (name == NULL || environ == NULL) |
| 495 | + return (NULL); |
| 496 | + for (np = name; *np && *np != '='; ++np) |
| 497 | + continue; |
| 498 | + len = np - name; |
| 499 | + for (p = environ; (c = *p) != NULL; ++p) |
| 500 | + if (strncmp(c, name, len) == 0 && c[len] == '=') { |
| 501 | + return (c + len + 1); |
| 502 | + } |
| 503 | + return (NULL); |
| 504 | +} |
| 505 | + |
| 506 | +char *getenv(const char *name) { |
| 507 | + return realgetenv(name); |
| 508 | +} |
| 509 | + |
454 | 510 | extern int setenv(const char *name, const char *value, int overwrite) { |
455 | 511 | assert(vars != NULL); |
456 | 512 | if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL) { |
@@ -536,4 +592,8 @@ extern void initenv(char **envp, Boolean protected) { |
536 | 592 | RefEnd2(var, imported); |
537 | 593 | envmin = env->count; |
538 | 594 | efree(buf); |
| 595 | + |
| 596 | +#if LOCAL_GETENV |
| 597 | + realgetenv = esgetenv; |
| 598 | +#endif |
539 | 599 | } |
0 commit comments