]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
e00b005a | 2 | * "$Id: env.c 5046 2006-02-01 22:11:58Z 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 | * | |
26 | * cupsdClearEnv() - Clear common environment variables. | |
27 | * cupsdInitEnv() - Initialize the current environment with standard | |
28 | * variables. | |
29 | * cupsdLoadEnv() - Copy common environment variables into an array. | |
30 | * cupsdSetEnv() - Set a common environment variable. | |
31 | * cupsdSetEnvf() - Set a formatted common environment variable. | |
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 | /* | |
50 | * 'cupsdClearEnv()' - Clear common environment variables. | |
51 | */ | |
52 | ||
53 | void | |
54 | cupsdClearEnv(void) | |
55 | { | |
56 | int i; /* Looping var */ | |
57 | ||
58 | ||
59 | for (i = 0; i < num_common_env; i ++) | |
60 | cupsdClearString(common_env + i); | |
61 | ||
62 | num_common_env = 0; | |
63 | } | |
64 | ||
65 | ||
66 | /* | |
67 | * 'cupsdInitEnv()' - Initialize the current environment with standard variables. | |
68 | */ | |
69 | ||
70 | void | |
71 | cupsdInitEnv(void) | |
72 | { | |
73 | /* | |
74 | * Clear existing environment variables... | |
75 | */ | |
76 | ||
77 | cupsdClearEnv(); | |
78 | ||
e00b005a | 79 | #if defined(__APPLE__) && __GNUC__ > 3 |
ef416fc2 | 80 | /* |
e00b005a | 81 | * Add special voodoo magic for MacOS X 10.4 and later - this allows MacOS |
82 | * X programs to access their bundle resources properly... | |
ef416fc2 | 83 | */ |
84 | ||
85 | cupsdSetString(common_env, "<CFProcessPath>"); | |
86 | num_common_env = 1; | |
e00b005a | 87 | #endif /* __APPLE__ && __GNUC__ > 3 */ |
ef416fc2 | 88 | |
89 | /* | |
90 | * Set common variables... | |
91 | */ | |
92 | ||
93 | cupsdSetEnv("CUPS_CACHEDIR", CacheDir); | |
94 | cupsdSetEnv("CUPS_DATADIR", DataDir); | |
95 | cupsdSetEnv("CUPS_DOCROOT", DocumentRoot); | |
96 | cupsdSetEnv("CUPS_FONTPATH", FontPath); | |
97 | cupsdSetEnv("CUPS_REQUESTROOT", RequestRoot); | |
98 | cupsdSetEnv("CUPS_SERVERBIN", ServerBin); | |
99 | cupsdSetEnv("CUPS_SERVERROOT", ServerRoot); | |
100 | cupsdSetEnv("CUPS_STATEDIR", StateDir); | |
101 | cupsdSetEnv("DYLD_LIBRARY_PATH", NULL); | |
102 | cupsdSetEnv("LD_ASSUME_KERNEL", NULL); | |
103 | cupsdSetEnv("LD_LIBRARY_PATH", NULL); | |
104 | cupsdSetEnv("LD_PRELOAD", NULL); | |
105 | cupsdSetEnv("NLSPATH", NULL); | |
fa73b229 | 106 | cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR |
107 | ":/bin:/usr/bin", ServerBin); | |
108 | cupsdSetEnv("SERVER_ADMIN", ServerAdmin); | |
ef416fc2 | 109 | cupsdSetEnv("SHLIB_PATH", NULL); |
110 | cupsdSetEnv("SOFTWARE", CUPS_MINIMAL); | |
111 | cupsdSetEnv("TMPDIR", TempDir); | |
112 | cupsdSetEnv("TZ", NULL); | |
113 | cupsdSetEnv("USER", "root"); | |
114 | cupsdSetEnv("VG_ARGS", NULL); | |
115 | } | |
116 | ||
117 | ||
118 | /* | |
119 | * 'cupsdLoadEnv()' - Copy common environment variables into an array. | |
120 | */ | |
121 | ||
122 | int /* O - Number of environment variables */ | |
123 | cupsdLoadEnv(char *envp[], /* I - Environment array */ | |
124 | int envmax) /* I - Maximum number of elements */ | |
125 | { | |
126 | int i; /* Looping var */ | |
127 | ||
128 | ||
129 | /* | |
130 | * Leave room for a NULL pointer at the end... | |
131 | */ | |
132 | ||
133 | envmax --; | |
134 | ||
135 | /* | |
136 | * Copy pointers to the environment... | |
137 | */ | |
138 | ||
139 | for (i = 0; i < num_common_env && i < envmax; i ++) | |
140 | envp[i] = common_env[i]; | |
141 | ||
142 | /* | |
143 | * NULL terminate the environment array and return the number of | |
144 | * elements we added... | |
145 | */ | |
146 | ||
147 | envp[i] = NULL; | |
148 | ||
149 | return (i); | |
150 | } | |
151 | ||
152 | ||
153 | /* | |
154 | * 'cupsdSetEnv()' - Set a common environment variable. | |
155 | */ | |
156 | ||
157 | void | |
158 | cupsdSetEnv(const char *name, /* I - Name of variable */ | |
159 | const char *value) /* I - Value of variable */ | |
160 | { | |
161 | /* | |
162 | * Check for room... | |
163 | */ | |
164 | ||
165 | if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0]))) | |
166 | { | |
167 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
168 | "cupsdSetEnv: Too many environment variables set!"); | |
169 | return; | |
170 | } | |
171 | ||
172 | /* | |
173 | * If "value" is NULL, try getting value from current environment... | |
174 | */ | |
175 | ||
176 | if (!value) | |
177 | value = getenv(name); | |
178 | ||
179 | if (!value) | |
180 | return; | |
181 | ||
182 | /* | |
183 | * Set the new environment variable... | |
184 | */ | |
185 | ||
186 | cupsdSetStringf(common_env + num_common_env, "%s=%s", name, value); | |
187 | ||
188 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetEnv: %s\n", | |
189 | common_env[num_common_env]); | |
190 | ||
191 | num_common_env ++; | |
192 | } | |
193 | ||
194 | ||
195 | /* | |
196 | * 'cupsdSetEnvf()' - Set a formatted common environment variable. | |
197 | */ | |
198 | ||
199 | void | |
200 | cupsdSetEnvf(const char *name, /* I - Name of variable */ | |
201 | const char *value, /* I - Printf-style value of variable */ | |
202 | ...) /* I - Additional args as needed */ | |
203 | { | |
204 | char v[4096]; /* Formatting string value */ | |
205 | va_list ap; /* Argument pointer */ | |
206 | ||
207 | ||
208 | /* | |
209 | * Format the value string... | |
210 | */ | |
211 | ||
212 | va_start(ap, value); | |
213 | vsnprintf(v, sizeof(v), value, ap); | |
214 | va_end(ap); | |
215 | ||
216 | /* | |
217 | * Set the env variable... | |
218 | */ | |
219 | ||
220 | cupsdSetEnv(name, v); | |
221 | } | |
222 | ||
223 | ||
224 | /* | |
e00b005a | 225 | * End of "$Id: env.c 5046 2006-02-01 22:11:58Z mike $". |
ef416fc2 | 226 | */ |