xref: /freebsd/crypto/heimdal/lib/roken/environment.c (revision c19800e8cd5640693f36f2040db4ab5e8d738146)
15e9cd1aeSAssar Westerlund /*
2c19800e8SDoug Rabson  * 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 #ifdef HAVE_CONFIG_H
365e9cd1aeSAssar Westerlund #include <config.h>
37c19800e8SDoug Rabson RCSID("$Id: environment.c 20866 2007-06-03 21:00:29Z lha $");
385e9cd1aeSAssar Westerlund #endif
395e9cd1aeSAssar Westerlund 
405e9cd1aeSAssar Westerlund #include <stdio.h>
415e9cd1aeSAssar Westerlund #include <string.h>
42c19800e8SDoug Rabson #include <ctype.h>
435e9cd1aeSAssar Westerlund #include "roken.h"
445e9cd1aeSAssar Westerlund 
45c19800e8SDoug Rabson /* find assignment in env list; len is length of variable including
46c19800e8SDoug Rabson  * equal
475e9cd1aeSAssar Westerlund  */
485e9cd1aeSAssar Westerlund 
49c19800e8SDoug Rabson static int
50c19800e8SDoug Rabson find_var(char **env, char *assignment, size_t len)
515e9cd1aeSAssar Westerlund {
52c19800e8SDoug Rabson     int i;
53c19800e8SDoug Rabson     for(i = 0; env != NULL && env[i] != NULL; i++)
54c19800e8SDoug Rabson 	if(strncmp(env[i], assignment, len) == 0)
55c19800e8SDoug Rabson 	    return i;
56c19800e8SDoug Rabson     return -1;
57c19800e8SDoug Rabson }
58c19800e8SDoug Rabson 
59c19800e8SDoug Rabson /*
60c19800e8SDoug Rabson  * return count of environment assignments from open file F in
61c19800e8SDoug Rabson  * assigned and list of malloced strings in env, return 0 or errno
62c19800e8SDoug Rabson  * number
63c19800e8SDoug Rabson  */
64c19800e8SDoug Rabson 
65c19800e8SDoug Rabson static int
66c19800e8SDoug Rabson rk_read_env_file(FILE *F, char ***env, int *assigned)
67c19800e8SDoug Rabson {
68c19800e8SDoug Rabson     int idx = 0;
69c19800e8SDoug Rabson     int i;
705e9cd1aeSAssar Westerlund     char **l;
715e9cd1aeSAssar Westerlund     char buf[BUFSIZ], *p, *r;
72c19800e8SDoug Rabson     char **tmp;
73c19800e8SDoug Rabson     int ret = 0;
745e9cd1aeSAssar Westerlund 
75c19800e8SDoug Rabson     *assigned = 0;
765e9cd1aeSAssar Westerlund 
77c19800e8SDoug Rabson     for(idx = 0; *env != NULL && (*env)[idx] != NULL; idx++);
785e9cd1aeSAssar Westerlund     l = *env;
79c19800e8SDoug Rabson 
805e9cd1aeSAssar Westerlund     /* This is somewhat more relaxed on what it accepts then
815e9cd1aeSAssar Westerlund      * Wietses sysv_environ from K4 was...
825e9cd1aeSAssar Westerlund      */
835e9cd1aeSAssar Westerlund     while (fgets(buf, BUFSIZ, F) != NULL) {
84c19800e8SDoug Rabson 	buf[strcspn(buf, "#\n")] = '\0';
855e9cd1aeSAssar Westerlund 
86c19800e8SDoug Rabson 	for(p = buf; isspace((unsigned char)*p); p++);
875e9cd1aeSAssar Westerlund 	if (*p == '\0')
885e9cd1aeSAssar Westerlund 	    continue;
895e9cd1aeSAssar Westerlund 
90c19800e8SDoug Rabson 	/* Here one should check that it's a 'valid' env string... */
915e9cd1aeSAssar Westerlund 	r = strchr(p, '=');
925e9cd1aeSAssar Westerlund 	if (r == NULL)
935e9cd1aeSAssar Westerlund 	    continue;
945e9cd1aeSAssar Westerlund 
95c19800e8SDoug Rabson 	if((i = find_var(l, p, r - p + 1)) >= 0) {
96c19800e8SDoug Rabson 	    char *val = strdup(p);
97c19800e8SDoug Rabson 	    if(val == NULL) {
98c19800e8SDoug Rabson 		ret = ENOMEM;
99c19800e8SDoug Rabson 		break;
1005e9cd1aeSAssar Westerlund 	    }
101c19800e8SDoug Rabson 	    free(l[i]);
102c19800e8SDoug Rabson 	    l[i] = val;
103c19800e8SDoug Rabson 	    (*assigned)++;
104c19800e8SDoug Rabson 	    continue;
105c19800e8SDoug Rabson 	}
106c19800e8SDoug Rabson 
107c19800e8SDoug Rabson 	tmp = realloc(l, (idx+2) * sizeof (char *));
108c19800e8SDoug Rabson 	if(tmp == NULL) {
109c19800e8SDoug Rabson 	    ret = ENOMEM;
110c19800e8SDoug Rabson 	    break;
111c19800e8SDoug Rabson 	}
112c19800e8SDoug Rabson 
113c19800e8SDoug Rabson 	l = tmp;
114c19800e8SDoug Rabson 	l[idx] = strdup(p);
115c19800e8SDoug Rabson 	if(l[idx] == NULL) {
116c19800e8SDoug Rabson 	    ret = ENOMEM;
117c19800e8SDoug Rabson 	    break;
118c19800e8SDoug Rabson 	}
119c19800e8SDoug Rabson 	l[++idx] = NULL;
120c19800e8SDoug Rabson 	(*assigned)++;
121c19800e8SDoug Rabson     }
122c19800e8SDoug Rabson     if(ferror(F))
123c19800e8SDoug Rabson 	ret = errno;
1245e9cd1aeSAssar Westerlund     *env = l;
125c19800e8SDoug Rabson     return ret;
126c19800e8SDoug Rabson }
127c19800e8SDoug Rabson 
128c19800e8SDoug Rabson /*
129c19800e8SDoug Rabson  * return count of environment assignments from file and
130c19800e8SDoug Rabson  * list of malloced strings in `env'
131c19800e8SDoug Rabson  */
132c19800e8SDoug Rabson 
133c19800e8SDoug Rabson int ROKEN_LIB_FUNCTION
134c19800e8SDoug Rabson read_environment(const char *file, char ***env)
135c19800e8SDoug Rabson {
136c19800e8SDoug Rabson     int assigned;
137c19800e8SDoug Rabson     FILE *F;
138c19800e8SDoug Rabson 
139c19800e8SDoug Rabson     if ((F = fopen(file, "r")) == NULL)
140c19800e8SDoug Rabson 	return 0;
141c19800e8SDoug Rabson 
142c19800e8SDoug Rabson     rk_read_env_file(F, env, &assigned);
143c19800e8SDoug Rabson     fclose(F);
144c19800e8SDoug Rabson     return assigned;
145c19800e8SDoug Rabson }
146c19800e8SDoug Rabson 
147c19800e8SDoug Rabson void ROKEN_LIB_FUNCTION
148c19800e8SDoug Rabson free_environment(char **env)
149c19800e8SDoug Rabson {
150c19800e8SDoug Rabson     int i;
151c19800e8SDoug Rabson     if (env == NULL)
152c19800e8SDoug Rabson 	return;
153c19800e8SDoug Rabson     for (i = 0; env[i]; i++)
154c19800e8SDoug Rabson 	free(env[i]);
155c19800e8SDoug Rabson     free(env);
1565e9cd1aeSAssar Westerlund }
157