]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
df13dab18bcc9b315c93fd7fadd6b898cd9fc96c
[thirdparty/cups.git] / scheduler / env.c
1 /*
2 * "$Id: env.c 4719 2005-09-28 21:12:44Z mike $"
3 *
4 * Environment management routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 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 * 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 */
46 static char *common_env[100]; /* Common env vars */
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
79 #ifdef __APPLE__
80 /*
81 * Add special voodoo magic for MacOS X - this allows MacOS X programs
82 * to access their bundle resources properly...
83 */
84
85 cupsdSetString(common_env, "<CFProcessPath>");
86 num_common_env = 1;
87 #endif /* __APPLE__ */
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);
106 cupsdSetEnvf("PATH", "%s/filter:/bin:/usr/bin", ServerBin);
107 cupsdSetEnv("SHLIB_PATH", NULL);
108 cupsdSetEnv("SOFTWARE", CUPS_MINIMAL);
109 cupsdSetEnv("TMPDIR", TempDir);
110 cupsdSetEnv("TZ", NULL);
111 cupsdSetEnv("USER", "root");
112 cupsdSetEnv("VG_ARGS", NULL);
113 }
114
115
116 /*
117 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
118 */
119
120 int /* O - Number of environment variables */
121 cupsdLoadEnv(char *envp[], /* I - Environment array */
122 int envmax) /* I - Maximum number of elements */
123 {
124 int i; /* Looping var */
125
126
127 /*
128 * Leave room for a NULL pointer at the end...
129 */
130
131 envmax --;
132
133 /*
134 * Copy pointers to the environment...
135 */
136
137 for (i = 0; i < num_common_env && i < envmax; i ++)
138 envp[i] = common_env[i];
139
140 /*
141 * NULL terminate the environment array and return the number of
142 * elements we added...
143 */
144
145 envp[i] = NULL;
146
147 return (i);
148 }
149
150
151 /*
152 * 'cupsdSetEnv()' - Set a common environment variable.
153 */
154
155 void
156 cupsdSetEnv(const char *name, /* I - Name of variable */
157 const char *value) /* I - Value of variable */
158 {
159 /*
160 * Check for room...
161 */
162
163 if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0])))
164 {
165 cupsdLogMessage(CUPSD_LOG_ERROR,
166 "cupsdSetEnv: Too many environment variables set!");
167 return;
168 }
169
170 /*
171 * If "value" is NULL, try getting value from current environment...
172 */
173
174 if (!value)
175 value = getenv(name);
176
177 if (!value)
178 return;
179
180 /*
181 * Set the new environment variable...
182 */
183
184 cupsdSetStringf(common_env + num_common_env, "%s=%s", name, value);
185
186 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetEnv: %s\n",
187 common_env[num_common_env]);
188
189 num_common_env ++;
190 }
191
192
193 /*
194 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
195 */
196
197 void
198 cupsdSetEnvf(const char *name, /* I - Name of variable */
199 const char *value, /* I - Printf-style value of variable */
200 ...) /* I - Additional args as needed */
201 {
202 char v[4096]; /* Formatting string value */
203 va_list ap; /* Argument pointer */
204
205
206 /*
207 * Format the value string...
208 */
209
210 va_start(ap, value);
211 vsnprintf(v, sizeof(v), value, ap);
212 va_end(ap);
213
214 /*
215 * Set the env variable...
216 */
217
218 cupsdSetEnv(name, v);
219 }
220
221
222 /*
223 * End of "$Id: env.c 4719 2005-09-28 21:12:44Z mike $".
224 */