Path: blob/main/crypto/heimdal/lib/roken/environment.c
39536 views
/*1* Copyright (c) 2000, 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/323334#include <config.h>3536#include <stdio.h>37#include <string.h>38#include <ctype.h>39#include "roken.h"4041/* find assignment in env list; len is length of variable including42* equal43*/4445static int46find_var(char **env, char *assignment, size_t len)47{48int i;49for(i = 0; env != NULL && env[i] != NULL; i++)50if(strncmp(env[i], assignment, len) == 0)51return i;52return -1;53}5455/*56* return count of environment assignments from open file F in57* assigned and list of malloced strings in env, return 0 or errno58* number59*/6061static int62read_env_file(FILE *F, char ***env, int *assigned)63{64int idx = 0;65int i;66char **l;67char buf[BUFSIZ], *p, *r;68char **tmp;69int ret = 0;7071*assigned = 0;7273for(idx = 0; *env != NULL && (*env)[idx] != NULL; idx++);74l = *env;7576/* This is somewhat more relaxed on what it accepts then77* Wietses sysv_environ from K4 was...78*/79while (fgets(buf, BUFSIZ, F) != NULL) {80buf[strcspn(buf, "#\n")] = '\0';8182for(p = buf; isspace((unsigned char)*p); p++);83if (*p == '\0')84continue;8586/* Here one should check that it's a 'valid' env string... */87r = strchr(p, '=');88if (r == NULL)89continue;9091if((i = find_var(l, p, r - p + 1)) >= 0) {92char *val = strdup(p);93if(val == NULL) {94ret = ENOMEM;95break;96}97free(l[i]);98l[i] = val;99(*assigned)++;100continue;101}102103tmp = realloc(l, (idx+2) * sizeof (char *));104if(tmp == NULL) {105ret = ENOMEM;106break;107}108109l = tmp;110l[idx] = strdup(p);111if(l[idx] == NULL) {112ret = ENOMEM;113break;114}115l[++idx] = NULL;116(*assigned)++;117}118if(ferror(F))119ret = errno;120*env = l;121return ret;122}123124/*125* return count of environment assignments from file and126* list of malloced strings in `env'127*/128129ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL130read_environment(const char *file, char ***env)131{132int assigned;133FILE *F;134135if ((F = fopen(file, "r")) == NULL)136return 0;137138read_env_file(F, env, &assigned);139fclose(F);140return assigned;141}142143ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL144free_environment(char **env)145{146int i;147if (env == NULL)148return;149for (i = 0; env[i]; i++)150free(env[i]);151free(env);152}153154155