No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / cron / env.c
blob6fde62944a9b5e4e88cf1ab32a409b63da7df01e
1 /* $NetBSD: env.c,v 1.14 2005/06/05 19:08:28 chs Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * All rights reserved
6 * Distribute freely, except: don't remove my name from the source or
7 * documentation (don't take credit for my work), mark your changes (don't
8 * get me blamed for your possible bugs), don't alter or remove this
9 * notice. May be sold if buildable source is provided to buyer. No
10 * warrantee of any kind, express or implied, is included with this
11 * software; use at your own risk, responsibility for damages (if any) to
12 * anyone resulting from the use of this software rests entirely with the
13 * user.
15 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
16 * I'll try to keep a version up to date. I can be reached as follows:
17 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
20 #include <sys/cdefs.h>
21 #if !defined(lint) && !defined(LINT)
22 #if 0
23 static char rcsid[] = "Id: env.c,v 2.7 1994/01/26 02:25:50 vixie Exp";
24 #else
25 __RCSID("$NetBSD: env.c,v 1.14 2005/06/05 19:08:28 chs Exp $");
26 #endif
27 #endif
30 #include "cron.h"
31 #include <string.h>
33 char **
34 env_init(void)
36 char **p = (char **) malloc(sizeof(char **));
38 p[0] = NULL;
39 return (p);
43 void
44 env_free(char **envp)
46 char **p;
48 for (p = envp; *p; p++)
49 free(*p);
50 free(envp);
54 char **
55 env_copy(char **envp)
57 int count, i;
58 char **p;
60 for (count = 0; envp[count] != NULL; count++)
62 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
63 for (i = 0; i < count; i++)
64 p[i] = strdup(envp[i]);
65 p[count] = NULL;
66 return (p);
70 char **
71 env_set(char **envp, char *envstr)
73 int count, found;
74 char **p;
77 * count the number of elements, including the null pointer;
78 * also set 'found' to -1 or index of entry if already in here.
80 found = -1;
81 for (count = 0; envp[count] != NULL; count++) {
82 if (!strcmp_until(envp[count], envstr, '='))
83 found = count;
85 count++; /* for the NULL */
87 if (found != -1) {
89 * it exists already, so just free the existing setting,
90 * save our new one there, and return the existing array.
92 free(envp[found]);
93 envp[found] = strdup(envstr);
94 return (envp);
98 * it doesn't exist yet, so resize the array, move null pointer over
99 * one, save our string over the old null pointer, and return resized
100 * array.
102 p = (char **) realloc((void *) envp,
103 (unsigned) ((count+1) * sizeof(char **)));
104 p[count] = p[count-1];
105 p[count-1] = strdup(envstr);
106 return (p);
110 /* return ERR = end of file
111 * FALSE = not an env setting (file was repositioned)
112 * TRUE = was an env setting
115 load_env(char *envstr, FILE *f)
117 long filepos;
118 int fileline, len;
119 char *name, *name_end, *val, *equal;
120 char *s;
122 s = name = name_end = NULL; /* XXXGCC -Wuninitialized [sparc64] */
124 filepos = ftell(f);
125 fileline = LineNumber;
126 skip_comments(f);
127 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
128 return (ERR);
130 Debug(DPARS, ("load_env, read <%s>\n", envstr))
132 name = NULL;
133 name_end = NULL;
134 s = NULL;
135 equal = strchr(envstr, '=');
136 if (equal) {
138 * decide if this is an environment variable or not by
139 * checking for spaces in the middle of the variable name.
140 * (it could also be a crontab line of the form
141 * <min> <hour> <day> <month> <weekday> command flag=value)
143 /* space before var name */
144 for (name = envstr; name < equal && isspace((unsigned char)*name); name++)
147 /* var name */
148 if (*name == '"' || *name == '\'') {
149 s = strchr(name + 1, *name);
150 name++;
151 if (!s || s > equal) {
152 Debug(DPARS, ("load_env, didn't get valid string"));
153 fseek(f, filepos, 0);
154 Set_LineNum(fileline);
155 return (FALSE);
157 name_end = s++;
158 } else {
159 for (s = name ; s < equal && !isspace((unsigned char)*s); s++)
161 name_end = s;
162 if (s < equal)
163 s++;
166 /* space after var name */
167 for ( ; s < equal && isspace((unsigned char)*s); s++)
170 * "s" should equal "equal".. otherwise, this is not an
171 * environment set command.
174 if (equal == NULL || name == name_end || s != equal) {
175 Debug(DPARS, ("load_env, didn't get valid string"));
176 fseek(f, filepos, 0);
177 Set_LineNum(fileline);
178 return (FALSE);
182 * process value string
184 val = equal + 1;
185 while (*val && isspace((unsigned char)*val))
186 val++;
187 if (*val) {
188 len = strdtb(val);
189 if (len >= 2 && (val[0] == '\'' || val[0] == '"') &&
190 val[len-1] == val[0]) {
191 val[len-1] = '\0';
192 val++;
196 *name_end = '\0';
197 (void) snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val);
198 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
199 return (TRUE);
203 char *
204 env_get(const char *name, char **envp)
206 int len = strlen(name);
207 char *p, *q;
209 while ((p = *envp++) != NULL) {
210 if (!(q = strchr(p, '=')))
211 continue;
212 if ((q - p) == len && !strncmp(p, name, len))
213 return (q+1);
215 return (NULL);