]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / env.c
1 /*
2 * "$Id: env.c 7673 2008-06-18 22:31:26Z mike $"
3 *
4 * Environment management routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * Contents:
16 *
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.
22 * clear_env() - Clear common environment variables.
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 */
37 static char *common_env[MAX_ENV]; /* Common env vars */
38
39
40 /*
41 * Local functions...
42 */
43
44 static void clear_env(void);
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
58 clear_env();
59
60 #if defined(__APPLE__)
61 /*
62 * Add special voodoo magic for MacOS X - this allows MacOS X
63 * programs to access their bundle resources properly...
64 *
65 * This string is replaced in cupsdStartProcess()...
66 */
67
68 cupsdSetString(common_env, "<CFProcessPath>");
69 num_common_env = 1;
70 #endif /* __APPLE__ */
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);
89 cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR
90 ":/bin:/usr/bin", ServerBin);
91 cupsdSetEnv("SERVER_ADMIN", ServerAdmin);
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 {
144 int i, /* Looping var */
145 namelen; /* Length of name */
146
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 /*
159 * See if this variable has already been defined...
160 */
161
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 */
185
186 cupsdSetStringf(common_env + i, "%s=%s", name, value);
187
188 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]);
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 /*
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 /*
239 * End of "$Id: env.c 7673 2008-06-18 22:31:26Z mike $".
240 */