]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/env.c
Merge changes from CUPS 1.5svn-r9407.
[thirdparty/cups.git] / scheduler / env.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: env.c 7673 2008-06-18 22:31:26Z mike $"
ef416fc2 3 *
10d09e33 4 * Environment management routines for the CUPS scheduler.
ef416fc2 5 *
10d09e33 6 * Copyright 2007-2010 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
36static int num_common_env = 0; /* Number of common env vars */
e00b005a 37static char *common_env[MAX_ENV]; /* Common env vars */
ef416fc2 38
39
40/*
e1d6a774 41 * Local functions...
ef416fc2 42 */
43
e1d6a774 44static void clear_env(void);
ef416fc2 45
46
47/*
48 * 'cupsdInitEnv()' - Initialize the current environment with standard variables.
49 */
50
51void
52cupsdInitEnv(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);
acb056cb 85 cupsdSetEnv("HOME", TempDir);
ef416fc2 86 cupsdSetEnv("LD_ASSUME_KERNEL", NULL);
87 cupsdSetEnv("LD_LIBRARY_PATH", NULL);
88 cupsdSetEnv("LD_PRELOAD", NULL);
89 cupsdSetEnv("NLSPATH", NULL);
fa73b229 90 cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR
91 ":/bin:/usr/bin", ServerBin);
92 cupsdSetEnv("SERVER_ADMIN", ServerAdmin);
ef416fc2 93 cupsdSetEnv("SHLIB_PATH", NULL);
94 cupsdSetEnv("SOFTWARE", CUPS_MINIMAL);
95 cupsdSetEnv("TMPDIR", TempDir);
96 cupsdSetEnv("TZ", NULL);
97 cupsdSetEnv("USER", "root");
98 cupsdSetEnv("VG_ARGS", NULL);
99}
100
101
102/*
103 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
104 */
105
106int /* O - Number of environment variables */
107cupsdLoadEnv(char *envp[], /* I - Environment array */
108 int envmax) /* I - Maximum number of elements */
109{
110 int i; /* Looping var */
111
112
113 /*
114 * Leave room for a NULL pointer at the end...
115 */
116
117 envmax --;
118
119 /*
120 * Copy pointers to the environment...
121 */
122
123 for (i = 0; i < num_common_env && i < envmax; i ++)
124 envp[i] = common_env[i];
125
126 /*
127 * NULL terminate the environment array and return the number of
128 * elements we added...
129 */
130
131 envp[i] = NULL;
132
133 return (i);
134}
135
136
137/*
138 * 'cupsdSetEnv()' - Set a common environment variable.
139 */
140
141void
142cupsdSetEnv(const char *name, /* I - Name of variable */
143 const char *value) /* I - Value of variable */
144{
480ef0fe 145 int i, /* Looping var */
146 namelen; /* Length of name */
ef416fc2 147
ef416fc2 148
149 /*
150 * If "value" is NULL, try getting value from current environment...
151 */
152
153 if (!value)
154 value = getenv(name);
155
156 if (!value)
157 return;
158
159 /*
480ef0fe 160 * See if this variable has already been defined...
ef416fc2 161 */
162
480ef0fe 163 for (i = 0, namelen = strlen(name); i < num_common_env; i ++)
164 if (!strncmp(common_env[i], name, namelen) && common_env[i][namelen] == '=')
165 break;
166
167 if (i >= num_common_env)
168 {
169 /*
170 * Check for room...
171 */
172
173 if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0])))
174 {
175 cupsdLogMessage(CUPSD_LOG_ERROR,
176 "cupsdSetEnv: Too many environment variables set!");
177 return;
178 }
179
180 num_common_env ++;
181 }
182
183 /*
184 * Set the new environment variable...
185 */
ef416fc2 186
480ef0fe 187 cupsdSetStringf(common_env + i, "%s=%s", name, value);
ef416fc2 188
75bd9771 189 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]);
ef416fc2 190}
191
192
193/*
194 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
195 */
196
197void
198cupsdSetEnvf(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/*
e1d6a774 223 * 'clear_env()' - Clear common environment variables.
224 */
225
226static void
227clear_env(void)
228{
229 int i; /* Looping var */
230
231
232 for (i = 0; i < num_common_env; i ++)
233 cupsdClearString(common_env + i);
234
235 num_common_env = 0;
236}
237
238
239/*
75bd9771 240 * End of "$Id: env.c 7673 2008-06-18 22:31:26Z mike $".
ef416fc2 241 */