Do not use .Xo/.Xc to work around ancient roff limits.
[netbsd-mini2440.git] / libexec / cron / env.c
blob63a34d033315526edc24e8f9556e8b8fadfdc94d
1 /* Copyright 1988,1990,1993 by Paul Vixie
2 * All rights reserved
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static char rcsid[] = "$Id: env.c,v 1.1 1994/01/05 20:40:14 jtc Exp $";
20 #endif
23 #include "cron.h"
24 #include "externs.h"
27 char **
28 env_init()
30 register char **p = (char **) malloc(sizeof(char **));
32 p[0] = NULL;
33 return p;
37 char **
38 env_set(envp, envstr)
39 char **envp;
40 char *envstr;
42 register int count, found;
43 register char **p;
46 * count the number of elements, including the null pointer;
47 * also set 'found' to -1 or index of entry if already in here.
49 found = -1;
50 for (count = 0; envp[count] != NULL; count++) {
51 if (!strcmp_until(envp[count], envstr, '='))
52 found = count;
54 count++; /* for the null pointer
57 if (found != -1) {
59 * it exists already, so just free the existing setting,
60 * save our new one there, and return the existing array.
62 free(envp[found]);
63 envp[found] = strdup(envstr);
64 return envp;
68 * it doesn't exist yet, so resize the array, move null pointer over
69 * one, save our string over the old null pointer, and return resized
70 * array.
72 p = (char **) realloc((void *) envp,
73 (unsigned) ((count+1) * sizeof(char **)));
74 p[count] = p[count-1];
75 p[count-1] = strdup(envstr);
76 return p;
80 /* return ERR = end of file
81 * FALSE = not an env setting (file was repositioned)
82 * TRUE = was an env setting
84 int
85 load_env(envstr, f)
86 char *envstr;
87 FILE *f;
89 long filepos;
90 int fileline;
91 char name[MAX_TEMPSTR], val[MAX_ENVSTR];
92 int fields;
94 filepos = ftell(f);
95 fileline = LineNumber;
96 skip_comments(f);
97 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
98 return ERR;
100 Debug(DPARS, ("load_env, read <%s>\n", envstr))
102 name[0] = val[0] = '\0';
103 fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
104 if (fields != 2) {
105 Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields))
106 fseek(f, filepos, 0);
107 Set_LineNum(fileline);
108 return FALSE;
111 /* 2 fields from scanf; looks like an env setting
115 * process value string
117 /*local*/{
118 int len = strdtb(val);
120 if (len >= 2) {
121 if (val[0] == '\'' || val[0] == '"') {
122 if (val[len-1] == val[0]) {
123 val[len-1] = '\0';
124 (void) strcpy(val, val+1);
130 (void) sprintf(envstr, "%s=%s", name, val);
131 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
132 return TRUE;
136 char *
137 env_get(name, envp)
138 char *name;
139 char **envp;
141 for (; *envp; envp++)
142 if (!strcmp_until(*envp, name, '='))
143 return strchr(*envp, '=') + 1;
144 return NULL;