diff options
Diffstat (limited to 'crypto/heimdal/lib/roken/environment.c')
-rw-r--r-- | crypto/heimdal/lib/roken/environment.c | 129 |
1 files changed, 91 insertions, 38 deletions
diff --git a/crypto/heimdal/lib/roken/environment.c b/crypto/heimdal/lib/roken/environment.c index 62c732c..3822e4c 100644 --- a/crypto/heimdal/lib/roken/environment.c +++ b/crypto/heimdal/lib/roken/environment.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 Kungliga Tekniska Högskolan + * Copyright (c) 2000, 2005 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * @@ -34,70 +34,123 @@ #ifdef HAVE_CONFIG_H #include <config.h> -RCSID("$Id: environment.c,v 1.1 2000/06/21 02:05:03 assar Exp $"); +RCSID("$Id: environment.c 20866 2007-06-03 21:00:29Z lha $"); #endif #include <stdio.h> #include <string.h> +#include <ctype.h> #include "roken.h" +/* find assignment in env list; len is length of variable including + * equal + */ + +static int +find_var(char **env, char *assignment, size_t len) +{ + int i; + for(i = 0; env != NULL && env[i] != NULL; i++) + if(strncmp(env[i], assignment, len) == 0) + return i; + return -1; +} + /* - * return count of environment assignments from `file' and - * list of malloced strings in `env' + * return count of environment assignments from open file F in + * assigned and list of malloced strings in env, return 0 or errno + * number */ -int -read_environment(const char *file, char ***env) +static int +rk_read_env_file(FILE *F, char ***env, int *assigned) { - int i, k; - FILE *F; + int idx = 0; + int i; char **l; char buf[BUFSIZ], *p, *r; + char **tmp; + int ret = 0; - if ((F = fopen(file, "r")) == NULL) { - return 0; - } + *assigned = 0; - i = 0; - if (*env) { - l = *env; - while (*l != NULL) { - i++; - l++; - } - } + for(idx = 0; *env != NULL && (*env)[idx] != NULL; idx++); l = *env; + /* This is somewhat more relaxed on what it accepts then * Wietses sysv_environ from K4 was... */ while (fgets(buf, BUFSIZ, F) != NULL) { - if (buf[0] == '#') - continue; - - p = strchr(buf, '#'); - if (p != NULL) - *p = '\0'; + buf[strcspn(buf, "#\n")] = '\0'; - p = buf; - while (*p == ' ' || *p == '\t' || *p == '\n') p++; + for(p = buf; isspace((unsigned char)*p); p++); if (*p == '\0') continue; - k = strlen(p); - if (p[k-1] == '\n') - p[k-1] = '\0'; - - /* Here one should check that is is a 'valid' env string... */ + /* Here one should check that it's a 'valid' env string... */ r = strchr(p, '='); if (r == NULL) continue; - l = realloc(l, (i+1) * sizeof (char *)); - l[i++] = strdup(p); + if((i = find_var(l, p, r - p + 1)) >= 0) { + char *val = strdup(p); + if(val == NULL) { + ret = ENOMEM; + break; + } + free(l[i]); + l[i] = val; + (*assigned)++; + continue; + } + + tmp = realloc(l, (idx+2) * sizeof (char *)); + if(tmp == NULL) { + ret = ENOMEM; + break; + } + + l = tmp; + l[idx] = strdup(p); + if(l[idx] == NULL) { + ret = ENOMEM; + break; + } + l[++idx] = NULL; + (*assigned)++; } - fclose(F); - l = realloc(l, (i+1) * sizeof (char *)); - l[i] = NULL; + if(ferror(F)) + ret = errno; *env = l; - return i; + return ret; +} + +/* + * return count of environment assignments from file and + * list of malloced strings in `env' + */ + +int ROKEN_LIB_FUNCTION +read_environment(const char *file, char ***env) +{ + int assigned; + FILE *F; + + if ((F = fopen(file, "r")) == NULL) + return 0; + + rk_read_env_file(F, env, &assigned); + fclose(F); + return assigned; +} + +void ROKEN_LIB_FUNCTION +free_environment(char **env) +{ + int i; + if (env == NULL) + return; + for (i = 0; env[i]; i++) + free(env[i]); + free(env); } |