From 708929bc329af952077b86fc6c4d5a774406847a Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Sat, 15 Aug 2009 11:34:41 +0000 Subject: [PATCH] awk: support for ENVIRON --- commands/awk/m.c | 4 ++-- commands/awk/v.c | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/commands/awk/m.c b/commands/awk/m.c index 14a779ce9..1f2ba270d 100755 --- a/commands/awk/m.c +++ b/commands/awk/m.c @@ -36,7 +36,7 @@ char *cmd; int iflg; /* interactive mode */ #endif -main(argc, argv) char **argv; +main(argc, argv, envp) char **argv, *envp; { char *s, *strpbrk(), *strchr(); void onint(); @@ -98,7 +98,7 @@ main(argc, argv) char **argv; xargc--; } - initarg(cmd, xargc, xargv); + initarg(cmd, xargc, xargv, envp); if (xargc == 0) { ifp = stdin; *FILENAME = "-"; } diff --git a/commands/awk/v.c b/commands/awk/v.c index 7d59e3e31..072d5a61a 100755 --- a/commands/awk/v.c +++ b/commands/awk/v.c @@ -36,6 +36,7 @@ extern prmflg; SYMBOL *hashtab[HASHSIZE]; SYMBOL *funtab[HASHSIZE]; SYMBOL *argtab[HASHSIZE]; +SYMBOL *envtab[HASHSIZE]; char *strsave(), *emalloc(), *strchr(); CELL *lookup(), *install(), *_install(), *mkcell(), *mktmp(), *getvar(); @@ -86,11 +87,11 @@ setvar(s) char *s; } } -initarg(arg0, argc, argv) char *arg0, **argv; +initarg(arg0, argc, argv, envp) char *arg0, **argv, **envp; { CELL *u; register int i; - register char str[4]; + register char str[4], *p; ARGC = &install("ARGC", VAR|NUM, (char *)NULL, (double)argc+1, hashtab)->c_fval; u = install("ARGV", ARR, (char *)NULL, 0.0, hashtab); @@ -103,6 +104,19 @@ initarg(arg0, argc, argv) char *arg0, **argv; else install(str, VAR|STR, argv[i], 0.0, argtab); } + + u = install("ENVIRON", ARR, (char *)NULL, 0.0, hashtab); + u->c_sval = (char *) envtab; + for (i = 0; envp[i] && *envp[i]; i++) { + if ((p = strchr(envp[i], '=')) != NULL) { + *p = 0; + if (isnum(p+1)) + install(envp[i], VAR|STR|NUM, p+1, atof(p+1), envtab); + else + install(envp[i], VAR|STR, p+1, 0.0, envtab); + *p = '='; + } + } } static