File: env.c

package info (click to toggle)
freeradius-client 1.1.6-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,048 kB
  • ctags: 504
  • sloc: sh: 8,504; ansic: 4,704; perl: 425; makefile: 86
file content (144 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * $Id: env.c,v 1.6 2007/06/21 18:07:23 cparker Exp $
 *
 * Copyright (C) 1995,1996,1997 Lars Fenneberg
 *
 * See the file COPYRIGHT for the respective terms and conditions.
 * If the file is missing contact me at lf@elemental.net
 * and I'll send you a copy.
 *
 */

#include <config.h>
#include <includes.h>
#include <freeradius-client.h>

/*
 * Function: rc_new_env
 *
 * Purpose: allocate space for a new environment
 *
 */

ENV *rc_new_env(int size)
{
	ENV *p;

	if (size < 1)
		return NULL;

	if ((p = malloc(sizeof(*p))) == NULL)
		return NULL;

	if ((p->env = malloc(size * sizeof(char *))) == NULL)
	{
		rc_log(LOG_CRIT, "rc_new_env: out of memory");
		free(p);
		return NULL;
	}

	p->env[0] = NULL;

	p->size = 0;
	p->maxsize = size;

	return p;
}

/*
 * Function: rc_free_env
 *
 * Purpose: free the space used by an env structure
 *
 */

void rc_free_env(ENV *env)
{
	free(env->env);
	free(env);
}

/*
 * Function: rc_add_env
 *
 * Purpose: add an environment entry
 *
 */

int rc_add_env(ENV *env, char *name, char *value)
{
	int i;
	char *new_env;

	for (i = 0; env->env[i] != NULL; i++)
	{
		if (strncmp(env->env[i], name, MAX(strchr(env->env[i], '=') - env->env[i], (int)strlen(name))) == 0)
			break;
	}

	if (env->env[i])
	{
		if ((new_env = realloc(env->env[i], strlen(name)+strlen(value)+2)) == NULL)
			return -1;

		env->env[i] = new_env;

		sprintf(env->env[i],"%s=%s", name, value);
	} else {
		if (env->size == (env->maxsize-1)) {
			rc_log(LOG_CRIT, "rc_add_env: not enough space for environment (increase ENV_SIZE)");
			return -1;
		}

		if ((env->env[env->size] = malloc(strlen(name)+strlen(value)+2)) == NULL) {
			rc_log(LOG_CRIT, "rc_add_env: out of memory");
			return -1;
		}

		sprintf(env->env[env->size],"%s=%s", name, value);

		env->size++;

		env->env[env->size] = NULL;
	}

	return 0;
}

/*
 * Function: rc_import_env
 *
 * Purpose: imports an array of null-terminated strings
 *
 */

int rc_import_env(ENV *env, char **import)
{
	char *es;

	while (*import)
	{
		es = strchr(*import, '=');

		if (!es)
		{
			import++;
			continue;
		}

		/* ok, i grant thats not very clean... */
		*es = '\0';

		if (rc_add_env(env, *import, es+1) < 0)
		{
			*es = '=';
			return -1;
		}

		*es = '=';

		import++;
	}

	return 0;
}