xref: /freebsd/crypto/heimdal/lib/roken/environment.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*
2*ae771770SStanislav Sedov  * Copyright (c) 2000, 2005 Kungliga Tekniska Högskolan
35e9cd1aeSAssar Westerlund  * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund  * All rights reserved.
55e9cd1aeSAssar Westerlund  *
65e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund  * are met:
95e9cd1aeSAssar Westerlund  *
105e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund  *
135e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund  *
175e9cd1aeSAssar Westerlund  * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
345e9cd1aeSAssar Westerlund 
355e9cd1aeSAssar Westerlund #include <config.h>
365e9cd1aeSAssar Westerlund 
375e9cd1aeSAssar Westerlund #include <stdio.h>
385e9cd1aeSAssar Westerlund #include <string.h>
39c19800e8SDoug Rabson #include <ctype.h>
405e9cd1aeSAssar Westerlund #include "roken.h"
415e9cd1aeSAssar Westerlund 
42c19800e8SDoug Rabson /* find assignment in env list; len is length of variable including
43c19800e8SDoug Rabson  * equal
445e9cd1aeSAssar Westerlund  */
455e9cd1aeSAssar Westerlund 
46c19800e8SDoug Rabson static int
find_var(char ** env,char * assignment,size_t len)47c19800e8SDoug Rabson find_var(char **env, char *assignment, size_t len)
485e9cd1aeSAssar Westerlund {
49c19800e8SDoug Rabson     int i;
50c19800e8SDoug Rabson     for(i = 0; env != NULL && env[i] != NULL; i++)
51c19800e8SDoug Rabson 	if(strncmp(env[i], assignment, len) == 0)
52c19800e8SDoug Rabson 	    return i;
53c19800e8SDoug Rabson     return -1;
54c19800e8SDoug Rabson }
55c19800e8SDoug Rabson 
56c19800e8SDoug Rabson /*
57c19800e8SDoug Rabson  * return count of environment assignments from open file F in
58c19800e8SDoug Rabson  * assigned and list of malloced strings in env, return 0 or errno
59c19800e8SDoug Rabson  * number
60c19800e8SDoug Rabson  */
61c19800e8SDoug Rabson 
62c19800e8SDoug Rabson static int
read_env_file(FILE * F,char *** env,int * assigned)63*ae771770SStanislav Sedov read_env_file(FILE *F, char ***env, int *assigned)
64c19800e8SDoug Rabson {
65c19800e8SDoug Rabson     int idx = 0;
66c19800e8SDoug Rabson     int i;
675e9cd1aeSAssar Westerlund     char **l;
685e9cd1aeSAssar Westerlund     char buf[BUFSIZ], *p, *r;
69c19800e8SDoug Rabson     char **tmp;
70c19800e8SDoug Rabson     int ret = 0;
715e9cd1aeSAssar Westerlund 
72c19800e8SDoug Rabson     *assigned = 0;
735e9cd1aeSAssar Westerlund 
74c19800e8SDoug Rabson     for(idx = 0; *env != NULL && (*env)[idx] != NULL; idx++);
755e9cd1aeSAssar Westerlund     l = *env;
76c19800e8SDoug Rabson 
775e9cd1aeSAssar Westerlund     /* This is somewhat more relaxed on what it accepts then
785e9cd1aeSAssar Westerlund      * Wietses sysv_environ from K4 was...
795e9cd1aeSAssar Westerlund      */
805e9cd1aeSAssar Westerlund     while (fgets(buf, BUFSIZ, F) != NULL) {
81c19800e8SDoug Rabson 	buf[strcspn(buf, "#\n")] = '\0';
825e9cd1aeSAssar Westerlund 
83c19800e8SDoug Rabson 	for(p = buf; isspace((unsigned char)*p); p++);
845e9cd1aeSAssar Westerlund 	if (*p == '\0')
855e9cd1aeSAssar Westerlund 	    continue;
865e9cd1aeSAssar Westerlund 
87c19800e8SDoug Rabson 	/* Here one should check that it's a 'valid' env string... */
885e9cd1aeSAssar Westerlund 	r = strchr(p, '=');
895e9cd1aeSAssar Westerlund 	if (r == NULL)
905e9cd1aeSAssar Westerlund 	    continue;
915e9cd1aeSAssar Westerlund 
92c19800e8SDoug Rabson 	if((i = find_var(l, p, r - p + 1)) >= 0) {
93c19800e8SDoug Rabson 	    char *val = strdup(p);
94c19800e8SDoug Rabson 	    if(val == NULL) {
95c19800e8SDoug Rabson 		ret = ENOMEM;
96c19800e8SDoug Rabson 		break;
975e9cd1aeSAssar Westerlund 	    }
98c19800e8SDoug Rabson 	    free(l[i]);
99c19800e8SDoug Rabson 	    l[i] = val;
100c19800e8SDoug Rabson 	    (*assigned)++;
101c19800e8SDoug Rabson 	    continue;
102c19800e8SDoug Rabson 	}
103c19800e8SDoug Rabson 
104c19800e8SDoug Rabson 	tmp = realloc(l, (idx+2) * sizeof (char *));
105c19800e8SDoug Rabson 	if(tmp == NULL) {
106c19800e8SDoug Rabson 	    ret = ENOMEM;
107c19800e8SDoug Rabson 	    break;
108c19800e8SDoug Rabson 	}
109c19800e8SDoug Rabson 
110c19800e8SDoug Rabson 	l = tmp;
111c19800e8SDoug Rabson 	l[idx] = strdup(p);
112c19800e8SDoug Rabson 	if(l[idx] == NULL) {
113c19800e8SDoug Rabson 	    ret = ENOMEM;
114c19800e8SDoug Rabson 	    break;
115c19800e8SDoug Rabson 	}
116c19800e8SDoug Rabson 	l[++idx] = NULL;
117c19800e8SDoug Rabson 	(*assigned)++;
118c19800e8SDoug Rabson     }
119c19800e8SDoug Rabson     if(ferror(F))
120c19800e8SDoug Rabson 	ret = errno;
1215e9cd1aeSAssar Westerlund     *env = l;
122c19800e8SDoug Rabson     return ret;
123c19800e8SDoug Rabson }
124c19800e8SDoug Rabson 
125c19800e8SDoug Rabson /*
126c19800e8SDoug Rabson  * return count of environment assignments from file and
127c19800e8SDoug Rabson  * list of malloced strings in `env'
128c19800e8SDoug Rabson  */
129c19800e8SDoug Rabson 
130*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
read_environment(const char * file,char *** env)131c19800e8SDoug Rabson read_environment(const char *file, char ***env)
132c19800e8SDoug Rabson {
133c19800e8SDoug Rabson     int assigned;
134c19800e8SDoug Rabson     FILE *F;
135c19800e8SDoug Rabson 
136c19800e8SDoug Rabson     if ((F = fopen(file, "r")) == NULL)
137c19800e8SDoug Rabson 	return 0;
138c19800e8SDoug Rabson 
139*ae771770SStanislav Sedov     read_env_file(F, env, &assigned);
140c19800e8SDoug Rabson     fclose(F);
141c19800e8SDoug Rabson     return assigned;
142c19800e8SDoug Rabson }
143c19800e8SDoug Rabson 
144*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
free_environment(char ** env)145c19800e8SDoug Rabson free_environment(char **env)
146c19800e8SDoug Rabson {
147c19800e8SDoug Rabson     int i;
148c19800e8SDoug Rabson     if (env == NULL)
149c19800e8SDoug Rabson 	return;
150c19800e8SDoug Rabson     for (i = 0; env[i]; i++)
151c19800e8SDoug Rabson 	free(env[i]);
152c19800e8SDoug Rabson     free(env);
1535e9cd1aeSAssar Westerlund }
154