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