/* * environ: massage the environment * * loadenviron() loads the environment * childenviron() builds an environment block for passing to child processes * eget() returns some specified class of environment variable * eput() adds an environment variable (of whatever class) */ #include #include #include #include "args.h" #include "execute.h" /* for shell error codes */ /* * build an environment block from the environment passed in to us */ struct environ_t * loadenviron(envp) char **envp; { register struct environ_t *ep; register char *p; register i; char **walk; register int epsize=0; if (ep = malloc(sizeof *ep)) { for (walk=envp; *walk; ++walk) ++epsize; if (ep->var = malloc(epsize * sizeof(struct var_t))) { ep->size = epsize; ep->touched = 1; ep->blok = malloc(1); memset(ep->var, 0, epsize * sizeof(struct var_t)); i=0; while (*envp) { if (p = strdup(*envp)) { ep->var[i].name = p; if (ep->var[i].text = strchr(p, '=')) *(ep->var[i].text++) = 0; else ep->var[i].text = ""; } else break; } return ep; } } return (struct environ_t *)0; } /* loadenviron() */ /* * childenviron() generates an environment block * For DOS, OS2 and Unix, this block is a string of null terminated strings * terminated by a null string */ char * childenviron(p) struct environ_t *p; { register int size, i; register char *env; printf("in childenviron\n"); fflush(stdout); if (p->touched) { puts("p->touched..."); fflush(stdout); free(p->blok); p->blok = "\0\0"; for (size=1, i=0; i < p->size; i++) if (p->var[i].flag & EXPORT) size += 2+strlen(p->var[i].name) +strlen(p->var[i].text); if (size <= 1) return p->blok; if (env = malloc(size)) { p->blok = env; for (i=0; i< p->size; i++) if (p->var[i].flag & EXPORT) { printf("%s=%s\n", p->var[i].name, p->var[i].text); sprintf(env,"%s=%s",p->var[i].name, p->var[i].text); env += 1+strlen(env); } *env = 0; p->touched = 0; } else no_more_core(); } return p->blok; } /* childenviron */ /* * eget() gets a string out of the environment according to the flags * you want. */ char * eget(env, name, flags) struct environ_t *env; char *name; { register i; for (i=0; i < env->size; i++) if ((env->var[i].flag & flags & ANY) && strcmp(env->var[i].name, name) == 0) return env->var[i].text; return (char*)0; } /* eget() */ /* * eput() writes a variable into the environment * * the token passed to it is the raw token from the shell, in name=val * format. */ eput(env, item, flags) struct environ_t *env; char *item; { register char *tn; register char *tv; register i; if (tn = strdup(item)) { if (tv = strchr(tn, '=')) *tv++ = 0; else tv = "\0\0"; for (i=0; i < env->size; i++) if ((env->var[i].flag & flags & ANY) && strcmp(env->var[i].name, tn) == 0) { free(env->var[i].name); add_env:env->var[i].name = tn; env->var[i].text= tv; env->var[i].flag = flags; env->touched = 1; return 0; } if (env->var=realloc(env->var, (1+env->size)*sizeof env->var[0])) { i = env->size++; goto add_env; } /* drat - lost all of our environment variables */ env->touched = 1; env->size = 0; } no_more_core(); return ST_SERR; } /* eput */