]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
Remove svn:keywords since they cause svn_load_dirs.pl to complain about every file.
[thirdparty/cups.git] / scheduler / env.c
1 /*
2 * "$Id: env.c 177 2006-06-21 00:20:03Z jlovell $"
3 *
4 * Environment management routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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 * 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.
31 * clear_env() - Clear common environment variables.
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 */
46 static char *common_env[MAX_ENV]; /* Common env vars */
47
48
49 /*
50 * Local functions...
51 */
52
53 static void clear_env(void);
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
67 clear_env();
68
69 #if defined(__APPLE__)
70 /*
71 * Add special voodoo magic for MacOS X - this allows MacOS X
72 * programs to access their bundle resources properly...
73 *
74 * This string is replaced in cupsdStartProcess()...
75 */
76
77 cupsdSetString(common_env, "<CFProcessPath>");
78 num_common_env = 1;
79 #endif /* __APPLE__ */
80
81 /*
82 * Set common variables...
83 */
84
85 cupsdSetEnv("CUPS_CACHEDIR", CacheDir);
86 cupsdSetEnv("CUPS_DATADIR", DataDir);
87 cupsdSetEnv("CUPS_DOCROOT", DocumentRoot);
88 cupsdSetEnv("CUPS_FONTPATH", FontPath);
89 cupsdSetEnv("CUPS_REQUESTROOT", RequestRoot);
90 cupsdSetEnv("CUPS_SERVERBIN", ServerBin);
91 cupsdSetEnv("CUPS_SERVERROOT", ServerRoot);
92 cupsdSetEnv("CUPS_STATEDIR", StateDir);
93 cupsdSetEnv("DYLD_LIBRARY_PATH", NULL);
94 cupsdSetEnv("LD_ASSUME_KERNEL", NULL);
95 cupsdSetEnv("LD_LIBRARY_PATH", NULL);
96 cupsdSetEnv("LD_PRELOAD", NULL);
97 cupsdSetEnv("NLSPATH", NULL);
98 cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR
99 ":/bin:/usr/bin", ServerBin);
100 cupsdSetEnv("SERVER_ADMIN", ServerAdmin);
101 cupsdSetEnv("SHLIB_PATH", NULL);
102 cupsdSetEnv("SOFTWARE", CUPS_MINIMAL);
103 cupsdSetEnv("TMPDIR", TempDir);
104 cupsdSetEnv("TZ", NULL);
105 cupsdSetEnv("USER", "root");
106 cupsdSetEnv("VG_ARGS", NULL);
107 }
108
109
110 /*
111 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
112 */
113
114 int /* O - Number of environment variables */
115 cupsdLoadEnv(char *envp[], /* I - Environment array */
116 int envmax) /* I - Maximum number of elements */
117 {
118 int i; /* Looping var */
119
120
121 /*
122 * Leave room for a NULL pointer at the end...
123 */
124
125 envmax --;
126
127 /*
128 * Copy pointers to the environment...
129 */
130
131 for (i = 0; i < num_common_env && i < envmax; i ++)
132 envp[i] = common_env[i];
133
134 /*
135 * NULL terminate the environment array and return the number of
136 * elements we added...
137 */
138
139 envp[i] = NULL;
140
141 return (i);
142 }
143
144
145 /*
146 * 'cupsdSetEnv()' - Set a common environment variable.
147 */
148
149 void
150 cupsdSetEnv(const char *name, /* I - Name of variable */
151 const char *value) /* I - Value of variable */
152 {
153 int i, /* Looping var */
154 namelen; /* Length of name */
155
156
157 /*
158 * If "value" is NULL, try getting value from current environment...
159 */
160
161 if (!value)
162 value = getenv(name);
163
164 if (!value)
165 return;
166
167 /*
168 * See if this variable has already been defined...
169 */
170
171 for (i = 0, namelen = strlen(name); i < num_common_env; i ++)
172 if (!strncmp(common_env[i], name, namelen) && common_env[i][namelen] == '=')
173 break;
174
175 if (i >= num_common_env)
176 {
177 /*
178 * Check for room...
179 */
180
181 if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0])))
182 {
183 cupsdLogMessage(CUPSD_LOG_ERROR,
184 "cupsdSetEnv: Too many environment variables set!");
185 return;
186 }
187
188 num_common_env ++;
189 }
190
191 /*
192 * Set the new environment variable...
193 */
194
195 cupsdSetStringf(common_env + i, "%s=%s", name, value);
196
197 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s\n", common_env[i]);
198 }
199
200
201 /*
202 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
203 */
204
205 void
206 cupsdSetEnvf(const char *name, /* I - Name of variable */
207 const char *value, /* I - Printf-style value of variable */
208 ...) /* I - Additional args as needed */
209 {
210 char v[4096]; /* Formatting string value */
211 va_list ap; /* Argument pointer */
212
213
214 /*
215 * Format the value string...
216 */
217
218 va_start(ap, value);
219 vsnprintf(v, sizeof(v), value, ap);
220 va_end(ap);
221
222 /*
223 * Set the env variable...
224 */
225
226 cupsdSetEnv(name, v);
227 }
228
229
230 /*
231 * 'clear_env()' - Clear common environment variables.
232 */
233
234 static void
235 clear_env(void)
236 {
237 int i; /* Looping var */
238
239
240 for (i = 0; i < num_common_env; i ++)
241 cupsdClearString(common_env + i);
242
243 num_common_env = 0;
244 }
245
246
247 /*
248 * End of "$Id: env.c 177 2006-06-21 00:20:03Z jlovell $".
249 */