]> granicus.if.org Git - fcron/blob - env_list.c
don't fully disable @-line on apparent nextexe integer overflows: instead set nextexe...
[fcron] / env_list.c
1 /*
2  * FCRON - periodic command scheduler
3  *
4  *  Copyright 2000-2014 Thibault Godouet <fcron@free.fr>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  The GNU General Public License can also be found in the file
21  *  `LICENSE' that comes with the fcron source distribution.
22  */
23
24
25 /* List of jobs currently being executed.
26  * This is a wrapper for an u_list (unordered list) (see u_list.h and u_list.c),
27  * to make the rest of the code clearer and as a way to ensure the compiler can checks
28  * the type in the rest of the code (u_list extensively uses the void type) */
29
30 #include "global.h"
31 #include "fcron.h"
32 #include "env_list.h"
33
34 env_list_t *
35 env_list_init(void)
36 {
37     return (env_list_t *) u_list_init(sizeof(env_t),
38                                       ENVVAR_INITIAL_SIZE, ENVVAR_GROW_SIZE);
39 }
40
41 env_list_t *
42 env_list_copy(env_list_t * list)
43 {
44     env_list_t *new_list = NULL;
45     env_t *e = NULL;
46
47     /* copy the list structure */
48     new_list = (env_list_t *) u_list_copy((u_list_t *) list);
49
50     /* for now the new list points to the same data strings - duplicate them */
51     for (e = env_list_first(new_list); e != NULL; e = env_list_next(new_list)) {
52         e->e_envvar = strdup2(e->e_envvar);
53     }
54
55     return new_list;
56
57 }
58
59 env_t *
60 env_list_setenv(env_list_t * list, char *name, char *value, int overwrite)
61 {
62     env_t e = { NULL };
63     env_t *c = NULL;
64     size_t len = strlen(name) + 1 + strlen(value) + 1;  /* 1 for '=', 1 for '\0' */
65
66     /* sanity check */
67     if (name == NULL || name[0] == '\0')
68         return NULL;
69
70     /* check if a var 'name' already exists */
71     for (c = env_list_first(list); c != NULL; c = env_list_next(list)) {
72         if (strcmp_until(name, c->e_envvar, '=') == 0) {
73             /* variable already set: overwrite the value if asked
74              * and return that entry */
75             if (overwrite == 1) {
76                 c->e_envvar =
77                     realloc_safe(c->e_envvar, len, "new env var value");
78                 snprintf(c->e_envvar, len, "%s=%s", name, value);
79             }
80             env_list_end_iteration(list);
81             return c;
82         }
83     }
84
85     /* if we're here we didn't find a var called 'name': add it */
86     e.e_envvar = alloc_safe(len, "new env var");
87     snprintf(e.e_envvar, len, "%s=%s", name, value);
88     return (env_t *) u_list_add((u_list_t *) list, (u_list_entry_t *) & e);
89 }
90
91 env_t *
92 env_list_putenv(env_list_t * list, char *envvar, int overwrite)
93 {
94     env_t e = { NULL };
95     env_t *c = NULL;
96     size_t len = strlen(envvar) + 1;    /* +1 for the terminating '\0' */
97
98     /* sanity check */
99     if (envvar == NULL || envvar[0] == '\0')
100         return NULL;
101
102     /* check if a var 'name' already exists */
103     for (c = env_list_first(list); c != NULL; c = env_list_next(list)) {
104         if (strcmp_until(envvar, c->e_envvar, '=') == 0) {
105             /* variable already set: overwrite the value if asked
106              * and return that entry */
107             if (overwrite == 1) {
108                 c->e_envvar =
109                     realloc_safe(c->e_envvar, len, "new env var value");
110                 memcpy(c->e_envvar, envvar, len);       /* includes the final '\0' */
111             }
112             env_list_end_iteration(list);
113             return c;
114         }
115     }
116
117     /* if we're here we didn't find a var called 'name': add it */
118     e.e_envvar = strdup2(envvar);
119     return (env_t *) u_list_add((u_list_t *) list, (u_list_entry_t *) & e);
120 }
121
122 char *
123 env_list_getenv(env_list_t * list, char *name)
124 {
125     env_t *c = NULL;
126
127     /* sanity check */
128     if (name == NULL || name[0] == '\0')
129         return NULL;
130
131     for (c = env_list_first(list); c != NULL; c = env_list_next(list)) {
132         if (strcmp_until(name, c->e_envvar, '=') == 0) {
133             /* found the var: return the pointer to the value */
134             env_list_end_iteration(list);
135             return (c->e_envvar + strlen(name) + 1);    /* +1 for '=' */
136         }
137     }
138
139     /* var 'name' not found */
140     return NULL;
141 }
142
143 env_t *
144 env_list_first(env_list_t * list)
145 {
146     return (env_t *) u_list_first((u_list_t *) list);
147 }
148
149 env_t *
150 env_list_next(env_list_t * list)
151 {
152     return (env_t *) u_list_next((u_list_t *) list);
153 }
154
155 void
156 env_list_end_iteration(env_list_t * list)
157 {
158     u_list_end_iteration((u_list_t *) list);
159 }
160
161 env_list_t *
162 env_list_destroy(env_list_t * list)
163     /* free() the memory allocated for list and returns NULL */
164 {
165     env_t *c = NULL;
166
167     /* make sure the iteration below won't fail in case one was already iterating */
168     env_list_end_iteration(list);
169     /* free the data in the env_t entries */
170     for (c = env_list_first(list); c != NULL; c = env_list_next(list)) {
171         Free_safe(c->e_envvar);
172     }
173     /* free the actual list structure */
174     return (env_list_t *) u_list_destroy((u_list_t *) list);
175 }
176
177 char **
178 env_list_export_envp(env_list_t * list)
179 /* export list data as a char **envp structure to use as an argument of execle() */
180 {
181     env_t *c = NULL;
182     int i = 0;
183     char **envp = NULL;
184
185     /* +1 for the end-of-list NULL */
186     envp =
187         alloc_safe((list->num_entries + 1) * sizeof(char *),
188                    "environment export");
189
190     for (c = env_list_first(list), i = 0; c != NULL && i < list->num_entries;
191          c = env_list_next(list), i++) {
192         envp[i] = strdup2(c->e_envvar);
193     }
194     /* add a NULL as a end-of-list marker */
195     envp[(list->num_entries + 1) - 1] = NULL;
196
197     return envp;
198 }
199
200 void
201 env_list_free_envp(char **envp)
202 {
203     char *p = NULL;
204
205     for (p = envp[0]; p != NULL; p++) {
206         Free_safe(p);
207     }
208     Free_safe(envp);
209
210 }