]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radiusclient/lib/env.c
Add these files, used with TDB.
[ppp.git] / pppd / plugins / radius / radiusclient / lib / env.c
1 /*
2  * $Id: env.c,v 1.2 2002/02/27 15:51:20 dfs Exp $
3  *
4  * Copyright (C) 1995,1996,1997 Lars Fenneberg
5  *
6  * See the file COPYRIGHT for the respective terms and conditions.
7  * If the file is missing contact me at lf@elemental.net
8  * and I'll send you a copy.
9  *
10  */
11
12 #include <config.h>
13 #include <includes.h>
14 #include <radiusclient.h>
15
16 /*
17  * Function: rc_new_env
18  *
19  * Purpose: allocate space for a new environment
20  *
21  */
22
23 ENV *rc_new_env(int size)
24 {
25         ENV *p;
26
27         if (size < 1)
28                 return NULL;
29
30         if ((p = malloc(sizeof(*p))) == NULL)
31                 return NULL;
32
33         if ((p->env = malloc(size * sizeof(char *))) == NULL)
34         {
35                 rc_log(LOG_CRIT, "rc_new_env: out of memory");
36                 free(p);
37                 return NULL;
38         }
39
40         p->env[0] = NULL;
41
42         p->size = 0;
43         p->maxsize = size;
44
45         return p;
46 }
47
48 /*
49  * Function: rc_free_env
50  *
51  * Purpose: free the space used by an env structure
52  *
53  */
54
55 void rc_free_env(ENV *env)
56 {
57         free(env->env);
58         free(env);
59 }
60
61 /*
62  * Function: rc_add_env
63  *
64  * Purpose: add an environment entry
65  *
66  */
67
68 int rc_add_env(ENV *env, char *name, char *value)
69 {
70         int i;
71         char *new_env;
72
73         for (i = 0; env->env[i] != NULL; i++)
74         {
75                 if (strncmp(env->env[i], name, MAX(strchr(env->env[i], '=') - env->env[i],strlen(name))) == 0)
76                         break;
77         }
78
79         if (env->env[i])
80         {
81                 if ((new_env = realloc(env->env[i], strlen(name)+strlen(value)+2)) == NULL)
82                         return (-1);
83
84                 env->env[i] = new_env;
85
86                 sprintf(env->env[i],"%s=%s", name, value);
87         } else {
88                 if (env->size == (env->maxsize-1)) {
89                         rc_log(LOG_CRIT, "rc_add_env: not enough space for environment (increase ENV_SIZE)");
90                         return (-1);
91                 }
92
93                 if ((env->env[env->size] = malloc(strlen(name)+strlen(value)+2)) == NULL) {
94                         rc_log(LOG_CRIT, "rc_add_env: out of memory");
95                         return (-1);
96                 }
97
98                 sprintf(env->env[env->size],"%s=%s", name, value);
99
100                 env->size++;
101
102                 env->env[env->size] = NULL;
103         }
104
105         return 0;
106 }
107
108 /*
109  * Function: rc_import_env
110  *
111  * Purpose: imports an array of null-terminated strings
112  *
113  */
114
115 int rc_import_env(ENV *env, char **import)
116 {
117         char *es;
118
119         while (*import)
120         {
121                 es = strchr(*import, '=');
122
123                 if (!es)
124                 {
125                         import++;
126                         continue;
127                 }
128
129                 /* ok, i grant thats not very clean... */
130                 *es = '\0';
131
132                 if (rc_add_env(env, *import, es+1) < 0)
133                 {
134                         *es = '=';
135                         return (-1);
136                 }
137
138                 *es = '=';
139
140                 import++;
141         }
142
143         return 0;
144 }