]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
e1d6a774 | 2 | * "$Id: env.c 5305 2006-03-18 03:05:12Z mike $" |
ef416fc2 | 3 | * |
4 | * Environment management routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
e00b005a | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
ef416fc2 | 26 | * cupsdInitEnv() - Initialize the current environment with standard |
27 | * variables. | |
28 | * cupsdLoadEnv() - Copy common environment variables into an array. | |
29 | * cupsdSetEnv() - Set a common environment variable. | |
30 | * cupsdSetEnvf() - Set a formatted common environment variable. | |
e1d6a774 | 31 | * clear_env() - Clear common environment variables. |
ef416fc2 | 32 | */ |
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include "cupsd.h" | |
39 | ||
40 | ||
41 | /* | |
42 | * Local globals... | |
43 | */ | |
44 | ||
45 | static int num_common_env = 0; /* Number of common env vars */ | |
e00b005a | 46 | static char *common_env[MAX_ENV]; /* Common env vars */ |
ef416fc2 | 47 | |
48 | ||
49 | /* | |
e1d6a774 | 50 | * Local functions... |
ef416fc2 | 51 | */ |
52 | ||
e1d6a774 | 53 | static void clear_env(void); |
ef416fc2 | 54 | |
55 | ||
56 | /* | |
57 | * 'cupsdInitEnv()' - Initialize the current environment with standard variables. | |
58 | */ | |
59 | ||
60 | void | |
61 | cupsdInitEnv(void) | |
62 | { | |
63 | /* | |
64 | * Clear existing environment variables... | |
65 | */ | |
66 | ||
e1d6a774 | 67 | clear_env(); |
ef416fc2 | 68 | |
bd7854cb | 69 | #if defined(__APPLE__) |
ef416fc2 | 70 | /* |
e00b005a | 71 | * Add special voodoo magic for MacOS X 10.4 and later - this allows MacOS |
72 | * X programs to access their bundle resources properly... | |
bd7854cb | 73 | * |
74 | * This string is replaced in cupsdStartProcess() when we are running on | |
75 | * versions of MacOS X prior to 10.4... | |
ef416fc2 | 76 | */ |
77 | ||
78 | cupsdSetString(common_env, "<CFProcessPath>"); | |
79 | num_common_env = 1; | |
bd7854cb | 80 | #endif /* __APPLE__ */ |
ef416fc2 | 81 | |
82 | /* | |
83 | * Set common variables... | |
84 | */ | |
85 | ||
86 | cupsdSetEnv("CUPS_CACHEDIR", CacheDir); | |
87 | cupsdSetEnv("CUPS_DATADIR", DataDir); | |
88 | cupsdSetEnv("CUPS_DOCROOT", DocumentRoot); | |
89 | cupsdSetEnv("CUPS_FONTPATH", FontPath); | |
90 | cupsdSetEnv("CUPS_REQUESTROOT", RequestRoot); | |
91 | cupsdSetEnv("CUPS_SERVERBIN", ServerBin); | |
92 | cupsdSetEnv("CUPS_SERVERROOT", ServerRoot); | |
93 | cupsdSetEnv("CUPS_STATEDIR", StateDir); | |
94 | cupsdSetEnv("DYLD_LIBRARY_PATH", NULL); | |
95 | cupsdSetEnv("LD_ASSUME_KERNEL", NULL); | |
96 | cupsdSetEnv("LD_LIBRARY_PATH", NULL); | |
97 | cupsdSetEnv("LD_PRELOAD", NULL); | |
98 | cupsdSetEnv("NLSPATH", NULL); | |
fa73b229 | 99 | cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR |
100 | ":/bin:/usr/bin", ServerBin); | |
101 | cupsdSetEnv("SERVER_ADMIN", ServerAdmin); | |
ef416fc2 | 102 | cupsdSetEnv("SHLIB_PATH", NULL); |
103 | cupsdSetEnv("SOFTWARE", CUPS_MINIMAL); | |
104 | cupsdSetEnv("TMPDIR", TempDir); | |
105 | cupsdSetEnv("TZ", NULL); | |
106 | cupsdSetEnv("USER", "root"); | |
107 | cupsdSetEnv("VG_ARGS", NULL); | |
108 | } | |
109 | ||
110 | ||
111 | /* | |
112 | * 'cupsdLoadEnv()' - Copy common environment variables into an array. | |
113 | */ | |
114 | ||
115 | int /* O - Number of environment variables */ | |
116 | cupsdLoadEnv(char *envp[], /* I - Environment array */ | |
117 | int envmax) /* I - Maximum number of elements */ | |
118 | { | |
119 | int i; /* Looping var */ | |
120 | ||
121 | ||
122 | /* | |
123 | * Leave room for a NULL pointer at the end... | |
124 | */ | |
125 | ||
126 | envmax --; | |
127 | ||
128 | /* | |
129 | * Copy pointers to the environment... | |
130 | */ | |
131 | ||
132 | for (i = 0; i < num_common_env && i < envmax; i ++) | |
133 | envp[i] = common_env[i]; | |
134 | ||
135 | /* | |
136 | * NULL terminate the environment array and return the number of | |
137 | * elements we added... | |
138 | */ | |
139 | ||
140 | envp[i] = NULL; | |
141 | ||
142 | return (i); | |
143 | } | |
144 | ||
145 | ||
146 | /* | |
147 | * 'cupsdSetEnv()' - Set a common environment variable. | |
148 | */ | |
149 | ||
150 | void | |
151 | cupsdSetEnv(const char *name, /* I - Name of variable */ | |
152 | const char *value) /* I - Value of variable */ | |
153 | { | |
480ef0fe | 154 | int i, /* Looping var */ |
155 | namelen; /* Length of name */ | |
ef416fc2 | 156 | |
ef416fc2 | 157 | |
158 | /* | |
159 | * If "value" is NULL, try getting value from current environment... | |
160 | */ | |
161 | ||
162 | if (!value) | |
163 | value = getenv(name); | |
164 | ||
165 | if (!value) | |
166 | return; | |
167 | ||
168 | /* | |
480ef0fe | 169 | * See if this variable has already been defined... |
ef416fc2 | 170 | */ |
171 | ||
480ef0fe | 172 | for (i = 0, namelen = strlen(name); i < num_common_env; i ++) |
173 | if (!strncmp(common_env[i], name, namelen) && common_env[i][namelen] == '=') | |
174 | break; | |
175 | ||
176 | if (i >= num_common_env) | |
177 | { | |
178 | /* | |
179 | * Check for room... | |
180 | */ | |
181 | ||
182 | if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0]))) | |
183 | { | |
184 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
185 | "cupsdSetEnv: Too many environment variables set!"); | |
186 | return; | |
187 | } | |
188 | ||
189 | num_common_env ++; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Set the new environment variable... | |
194 | */ | |
ef416fc2 | 195 | |
480ef0fe | 196 | cupsdSetStringf(common_env + i, "%s=%s", name, value); |
ef416fc2 | 197 | |
480ef0fe | 198 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s\n", common_env[i]); |
ef416fc2 | 199 | } |
200 | ||
201 | ||
202 | /* | |
203 | * 'cupsdSetEnvf()' - Set a formatted common environment variable. | |
204 | */ | |
205 | ||
206 | void | |
207 | cupsdSetEnvf(const char *name, /* I - Name of variable */ | |
208 | const char *value, /* I - Printf-style value of variable */ | |
209 | ...) /* I - Additional args as needed */ | |
210 | { | |
211 | char v[4096]; /* Formatting string value */ | |
212 | va_list ap; /* Argument pointer */ | |
213 | ||
214 | ||
215 | /* | |
216 | * Format the value string... | |
217 | */ | |
218 | ||
219 | va_start(ap, value); | |
220 | vsnprintf(v, sizeof(v), value, ap); | |
221 | va_end(ap); | |
222 | ||
223 | /* | |
224 | * Set the env variable... | |
225 | */ | |
226 | ||
227 | cupsdSetEnv(name, v); | |
228 | } | |
229 | ||
230 | ||
231 | /* | |
e1d6a774 | 232 | * 'clear_env()' - Clear common environment variables. |
233 | */ | |
234 | ||
235 | static void | |
236 | clear_env(void) | |
237 | { | |
238 | int i; /* Looping var */ | |
239 | ||
240 | ||
241 | for (i = 0; i < num_common_env; i ++) | |
242 | cupsdClearString(common_env + i); | |
243 | ||
244 | num_common_env = 0; | |
245 | } | |
246 | ||
247 | ||
248 | /* | |
249 | * End of "$Id: env.c 5305 2006-03-18 03:05:12Z mike $". | |
ef416fc2 | 250 | */ |