Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | #if !defined(lint) && !defined(LINT) |
2 | static char rcsid[] = "$Header: env.c,v 2.2 90/07/18 00:23:48 vixie Exp $"; | |
3 | #endif | |
4 | ||
5 | /* Copyright 1988,1990 by Paul Vixie | |
6 | * All rights reserved | |
7 | * | |
8 | * Distribute freely, except: don't remove my name from the source or | |
9 | * documentation (don't take credit for my work), mark your changes (don't | |
10 | * get me blamed for your possible bugs), don't alter or remove this | |
11 | * notice. May be sold if buildable source is provided to buyer. No | |
12 | * warrantee of any kind, express or implied, is included with this | |
13 | * software; use at your own risk, responsibility for damages (if any) to | |
14 | * anyone resulting from the use of this software rests entirely with the | |
15 | * user. | |
16 | * | |
17 | * Send bug reports, bug fixes, enhancements, requests, flames, etc., and | |
18 | * I'll try to keep a version up to date. I can be reached as follows: | |
19 | * Paul Vixie, 329 Noe Street, San Francisco, CA, 94114, (415) 864-7013, | |
20 | * paul@vixie.sf.ca.us || {hoptoad,pacbell,decwrl,crash}!vixie!paul | |
21 | * | |
22 | * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE | |
23 | * -------------------- ----- ---------------------- | |
24 | * CURRENT PATCH LEVEL: 1 00131 | |
25 | * -------------------- ----- ---------------------- | |
26 | * | |
27 | * 06 Apr 93 Adam Glass Fixes so it compiles quitely | |
28 | * | |
29 | */ | |
30 | ||
31 | ||
32 | #include "cron.h" | |
33 | ||
34 | ||
35 | char ** | |
36 | env_init() | |
37 | { | |
38 | extern char *malloc(); | |
39 | register char **p = (char **) malloc(sizeof(char **)); | |
40 | ||
41 | p[0] = NULL; | |
42 | return p; | |
43 | } | |
44 | ||
45 | ||
46 | char ** | |
47 | env_set(envp, envstr) | |
48 | char **envp; | |
49 | char *envstr; | |
50 | { | |
51 | extern char *realloc(), *savestr(); | |
52 | register int count, found; | |
53 | register char **p; | |
54 | ||
55 | /* | |
56 | * count the number of elements, including the null pointer; | |
57 | * also set 'found' to -1 or index of entry if already in here. | |
58 | */ | |
59 | found = -1; | |
60 | for (count = 0; envp[count] != NULL; count++) | |
61 | { | |
62 | if (!strcmp_until(envp[count], envstr, '=')) | |
63 | found = count; | |
64 | } | |
65 | count++; /* for the null pointer | |
66 | */ | |
67 | ||
68 | if (found != -1) | |
69 | { | |
70 | /* | |
71 | * it exists already, so just free the existing setting, | |
72 | * save our new one there, and return the existing array. | |
73 | */ | |
74 | free(envp[found]); | |
75 | envp[found] = savestr(envstr); | |
76 | return envp; | |
77 | } | |
78 | ||
79 | /* | |
80 | * it doesn't exist yet, so resize the array, move null pointer over | |
81 | * one, save our string over the old null pointer, and return resized | |
82 | * array. | |
83 | */ | |
84 | p = (char **) realloc( | |
85 | (char *) envp, | |
86 | (unsigned) ((count+1) * sizeof(char **)) | |
87 | ); | |
88 | p[count] = p[count-1]; | |
89 | p[count-1] = savestr(envstr); | |
90 | return p; | |
91 | } | |
92 | ||
93 | ||
94 | int | |
95 | load_env(envstr, f) | |
96 | char *envstr; | |
97 | FILE *f; | |
98 | { | |
99 | /* return ERR = end of file | |
100 | * FALSE = not an env setting (file was repositioned) | |
101 | * TRUE = was an env setting | |
102 | */ | |
103 | long filepos; | |
104 | int fileline; | |
105 | char name[MAX_TEMPSTR], val[MAX_ENVSTR]; | |
106 | int fields, strdtb(); | |
107 | void skip_comments(); | |
108 | ||
109 | filepos = ftell(f); | |
110 | fileline = LineNumber; | |
111 | skip_comments(f); | |
112 | if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) | |
113 | return ERR; | |
114 | ||
115 | Debug(DPARS, ("load_env, read <%s>\n", envstr)) | |
116 | ||
117 | name[0] = val[0] = '\0'; | |
118 | fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val); | |
119 | if (fields != 2) | |
120 | { | |
121 | Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields)) | |
122 | fseek(f, filepos, 0); | |
123 | Set_LineNum(fileline); | |
124 | return FALSE; | |
125 | } | |
126 | ||
127 | /* 2 fields from scanf; looks like an env setting | |
128 | */ | |
129 | ||
130 | /* | |
131 | * process value string | |
132 | */ | |
133 | { | |
134 | int len = strdtb(val); | |
135 | ||
136 | if (len >= 2) | |
137 | if (val[0] == '\'' || val[0] == '"') | |
138 | if (val[len-1] == val[0]) | |
139 | { | |
140 | val[len-1] = '\0'; | |
141 | (void) strcpy(val, val+1); | |
142 | } | |
143 | } | |
144 | ||
145 | (void) sprintf(envstr, "%s=%s", name, val); | |
146 | Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) | |
147 | return TRUE; | |
148 | } | |
149 | ||
150 | ||
151 | char * | |
152 | env_get(name, envp) | |
153 | char *name; | |
154 | char **envp; | |
155 | { | |
156 | char *index(); | |
157 | ||
158 | for (; *envp; envp++) | |
159 | if (!strcmp_until(*envp, name, '=')) | |
160 | return index(*envp, '=') + 1; | |
161 | return NULL; | |
162 | } |