2 * "$Id: env.c 9459 2011-01-11 03:48:42Z mike $"
4 * Environment management routines for the CUPS scheduler.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
17 * cupsdInitEnv() - Initialize the current environment with standard
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 * cupsdUpdateEnv() - Update the environment for the configured directories.
23 * clear_env() - Clear common environment variables.
24 * find_env() - Find a common environment variable.
28 * Include necessary headers...
38 static int num_common_env = 0; /* Number of common env vars */
39 static char *common_env[MAX_ENV]; /* Common env vars */
46 static void clear_env(void);
47 static int find_env(const char *name);
51 * 'cupsdInitEnv()' - Initialize the current environment with standard variables.
58 * Clear existing environment variables...
63 #if defined(__APPLE__)
65 * Add special voodoo magic for MacOS X - this allows MacOS X
66 * programs to access their bundle resources properly...
68 * This string is replaced in cupsdStartProcess()...
71 cupsdSetString(common_env, "<CFProcessPath>");
73 #endif /* __APPLE__ */
78 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
81 int /* O - Number of environment variables */
82 cupsdLoadEnv(char *envp[], /* I - Environment array */
83 int envmax) /* I - Maximum number of elements */
85 int i; /* Looping var */
89 * Leave room for a NULL pointer at the end...
95 * Copy pointers to the environment...
98 for (i = 0; i < num_common_env && i < envmax; i ++)
99 envp[i] = common_env[i];
102 * NULL terminate the environment array and return the number of
103 * elements we added...
113 * 'cupsdSetEnv()' - Set a common environment variable.
117 cupsdSetEnv(const char *name, /* I - Name of variable */
118 const char *value) /* I - Value of variable */
120 int i; /* Index into environent array */
124 * If "value" is NULL, try getting value from current environment...
128 value = getenv(name);
134 * See if this variable has already been defined...
137 if ((i = find_env(name)) < 0)
143 if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0])))
145 cupsdLogMessage(CUPSD_LOG_ERROR,
146 "cupsdSetEnv: Too many environment variables set!");
155 * Set the new environment variable...
158 cupsdSetStringf(common_env + i, "%s=%s", name, value);
160 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]);
165 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
169 cupsdSetEnvf(const char *name, /* I - Name of variable */
170 const char *value, /* I - Printf-style value of variable */
171 ...) /* I - Additional args as needed */
173 char v[4096]; /* Formatting string value */
174 va_list ap; /* Argument pointer */
178 * Format the value string...
182 vsnprintf(v, sizeof(v), value, ap);
186 * Set the env variable...
189 cupsdSetEnv(name, v);
194 * 'cupsdUpdateEnv()' - Update the environment for the configured directories.
201 * Set common variables...
204 #define set_if_undefined(name,value) if (find_env(name) < 0) cupsdSetEnv(name,value)
206 set_if_undefined("CUPS_CACHEDIR", CacheDir);
207 set_if_undefined("CUPS_DATADIR", DataDir);
208 set_if_undefined("CUPS_DOCROOT", DocumentRoot);
209 set_if_undefined("CUPS_FONTPATH", FontPath);
210 set_if_undefined("CUPS_REQUESTROOT", RequestRoot);
211 set_if_undefined("CUPS_SERVERBIN", ServerBin);
212 set_if_undefined("CUPS_SERVERROOT", ServerRoot);
213 set_if_undefined("CUPS_STATEDIR", StateDir);
214 set_if_undefined("DYLD_LIBRARY_PATH", NULL);
215 set_if_undefined("HOME", TempDir);
216 set_if_undefined("LD_ASSUME_KERNEL", NULL);
217 set_if_undefined("LD_LIBRARY_PATH", NULL);
218 set_if_undefined("LD_PRELOAD", NULL);
219 set_if_undefined("NLSPATH", NULL);
220 if (find_env("PATH") < 0)
221 cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR
222 ":/bin:/usr/bin", ServerBin);
223 set_if_undefined("SERVER_ADMIN", ServerAdmin);
224 set_if_undefined("SHLIB_PATH", NULL);
225 set_if_undefined("SOFTWARE", CUPS_MINIMAL);
226 set_if_undefined("TMPDIR", TempDir);
227 set_if_undefined("TZ", NULL);
228 set_if_undefined("USER", "root");
229 set_if_undefined("VG_ARGS", NULL);
234 * 'clear_env()' - Clear common environment variables.
240 int i; /* Looping var */
243 for (i = 0; i < num_common_env; i ++)
244 cupsdClearString(common_env + i);
251 * 'find_env()' - Find a common environment variable.
254 static int /* O - Index or -1 if not found */
255 find_env(const char *name) /* I - Variable name */
257 int i; /* Looping var */
258 size_t namelen; /* Length of name */
261 for (i = 0, namelen = strlen(name); i < num_common_env; i ++)
262 if (!strncmp(common_env[i], name, namelen) && common_env[i][namelen] == '=')
270 * End of "$Id: env.c 9459 2011-01-11 03:48:42Z mike $".