Commit | Line | Data |
---|---|---|
6441d3de WJ |
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 | ||
23 | ||
24 | #include "cron.h" | |
25 | ||
26 | ||
27 | char ** | |
28 | env_init() | |
29 | { | |
30 | extern char *malloc(); | |
31 | register char **p = (char **) malloc(sizeof(char **)); | |
32 | ||
33 | p[0] = NULL; | |
34 | return p; | |
35 | } | |
36 | ||
37 | ||
38 | char ** | |
39 | env_set(envp, envstr) | |
40 | char **envp; | |
41 | char *envstr; | |
42 | { | |
43 | extern char *realloc(), *savestr(); | |
44 | register int count, found; | |
45 | register char **p; | |
46 | ||
47 | /* | |
48 | * count the number of elements, including the null pointer; | |
49 | * also set 'found' to -1 or index of entry if already in here. | |
50 | */ | |
51 | found = -1; | |
52 | for (count = 0; envp[count] != NULL; count++) | |
53 | { | |
54 | if (!strcmp_until(envp[count], envstr, '=')) | |
55 | found = count; | |
56 | } | |
57 | count++; /* for the null pointer | |
58 | */ | |
59 | ||
60 | if (found != -1) | |
61 | { | |
62 | /* | |
63 | * it exists already, so just free the existing setting, | |
64 | * save our new one there, and return the existing array. | |
65 | */ | |
66 | free(envp[found]); | |
67 | envp[found] = savestr(envstr); | |
68 | return envp; | |
69 | } | |
70 | ||
71 | /* | |
72 | * it doesn't exist yet, so resize the array, move null pointer over | |
73 | * one, save our string over the old null pointer, and return resized | |
74 | * array. | |
75 | */ | |
76 | p = (char **) realloc( | |
77 | (char *) envp, | |
78 | (unsigned) ((count+1) * sizeof(char **)) | |
79 | ); | |
80 | p[count] = p[count-1]; | |
81 | p[count-1] = savestr(envstr); | |
82 | return p; | |
83 | } | |
84 | ||
85 | ||
86 | int | |
87 | load_env(envstr, f) | |
88 | char *envstr; | |
89 | FILE *f; | |
90 | { | |
91 | /* return ERR = end of file | |
92 | * FALSE = not an env setting (file was repositioned) | |
93 | * TRUE = was an env setting | |
94 | */ | |
95 | char *strcpy(), *sprintf(); | |
96 | long filepos; | |
97 | int fileline; | |
98 | char name[MAX_TEMPSTR], val[MAX_ENVSTR]; | |
99 | int fields, strdtb(); | |
100 | void skip_comments(); | |
101 | ||
102 | filepos = ftell(f); | |
103 | fileline = LineNumber; | |
104 | skip_comments(f); | |
105 | if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) | |
106 | return ERR; | |
107 | ||
108 | Debug(DPARS, ("load_env, read <%s>\n", envstr)) | |
109 | ||
110 | name[0] = val[0] = '\0'; | |
111 | fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val); | |
112 | if (fields != 2) | |
113 | { | |
114 | Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields)) | |
115 | fseek(f, filepos, 0); | |
116 | Set_LineNum(fileline); | |
117 | return FALSE; | |
118 | } | |
119 | ||
120 | /* 2 fields from scanf; looks like an env setting | |
121 | */ | |
122 | ||
123 | /* | |
124 | * process value string | |
125 | */ | |
126 | { | |
127 | int len = strdtb(val); | |
128 | ||
129 | if (len >= 2) | |
130 | if (val[0] == '\'' || val[0] == '"') | |
131 | if (val[len-1] == val[0]) | |
132 | { | |
133 | val[len-1] = '\0'; | |
134 | (void) strcpy(val, val+1); | |
135 | } | |
136 | } | |
137 | ||
138 | (void) sprintf(envstr, "%s=%s", name, val); | |
139 | Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) | |
140 | return TRUE; | |
141 | } | |
142 | ||
143 | ||
144 | char * | |
145 | env_get(name, envp) | |
146 | char *name; | |
147 | char **envp; | |
148 | { | |
149 | char *index(); | |
150 | ||
151 | for (; *envp; envp++) | |
152 | if (!strcmp_until(*envp, name, '=')) | |
153 | return index(*envp, '=') + 1; | |
154 | return NULL; | |
155 | } |