]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/globals.c
Merge changes from CUPS 1.5svn-r9049 (private header support)
[thirdparty/cups.git] / cups / globals.c
1 /*
2 * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $"
3 *
4 * Global variable access routines for CUPS.
5 *
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 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 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * _cupsGlobals() - Return a pointer to thread local storage.
20 * cups_env_init() - Initialize environment variables.
21 * globals_init() - Initialize globals once.
22 * globals_destructor() - Free memory allocated by _cupsGlobals().
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include "cups-private.h"
30
31
32 /*
33 * 'cups_env_init()' - Initialize environment variables.
34 */
35
36 static void
37 cups_env_init(_cups_globals_t *g) /* I - Global data */
38 {
39 #ifdef WIN32
40 HKEY key; /* Registry key */
41 DWORD size; /* Size of string */
42 static char installdir[1024], /* Install directory */
43 confdir[1024], /* Server root directory */
44 localedir[1024]; /* Locale directory */
45
46
47 /*
48 * Open the registry...
49 */
50
51 strcpy(installdir, "C:/Program Files/cups.org");
52
53 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ,
54 &key))
55 {
56 /*
57 * Grab the installation directory...
58 */
59
60 size = sizeof(installdir);
61 RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size);
62 RegCloseKey(key);
63 }
64
65 snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
66 snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
67
68 if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
69 g->cups_datadir = installdir;
70
71 if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
72 g->cups_serverbin = installdir;
73
74 if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
75 g->cups_serverroot = confdir;
76
77 if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
78 g->cups_statedir = confdir;
79
80 if ((g->localedir = getenv("LOCALEDIR")) == NULL)
81 g->localedir = localedir;
82
83 #else
84 if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
85 g->cups_datadir = CUPS_DATADIR;
86
87 if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
88 g->cups_serverbin = CUPS_SERVERBIN;
89
90 if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
91 g->cups_serverroot = CUPS_SERVERROOT;
92
93 if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
94 g->cups_statedir = CUPS_STATEDIR;
95
96 if ((g->localedir = getenv("LOCALEDIR")) == NULL)
97 g->localedir = CUPS_LOCALEDIR;
98 #endif /* WIN32 */
99 }
100
101
102 #ifdef HAVE_PTHREAD_H
103 /*
104 * Implement per-thread globals...
105 */
106
107 /*
108 * Local globals...
109 */
110
111 static pthread_key_t globals_key = -1;
112 /* Thread local storage key */
113 static pthread_once_t globals_key_once = PTHREAD_ONCE_INIT;
114 /* One-time initialization object */
115
116
117 /*
118 * Local functions...
119 */
120
121 static void globals_init();
122 static void globals_destructor(void *value);
123
124
125 /*
126 * '_cupsGlobals()' - Return a pointer to thread local storage
127 */
128
129 _cups_globals_t * /* O - Pointer to global data */
130 _cupsGlobals(void)
131 {
132 _cups_globals_t *globals; /* Pointer to global data */
133
134
135 /*
136 * Initialize the global data exactly once...
137 */
138
139 pthread_once(&globals_key_once, globals_init);
140
141 /*
142 * See if we have allocated the data yet...
143 */
144
145 if ((globals = (_cups_globals_t *)pthread_getspecific(globals_key)) == NULL)
146 {
147 /*
148 * No, allocate memory as set the pointer for the key...
149 */
150
151 globals = calloc(1, sizeof(_cups_globals_t));
152 pthread_setspecific(globals_key, globals);
153
154 /*
155 * Initialize variables that have non-zero values
156 */
157
158 globals->encryption = (http_encryption_t)-1;
159 globals->password_cb = (cups_password_cb2_t)_cupsGetPassword;
160
161 cups_env_init(globals);
162 }
163
164 /*
165 * Return the pointer to the data...
166 */
167
168 return (globals);
169 }
170
171
172 /*
173 * 'globals_init()' - Initialize globals once.
174 */
175
176 static void
177 globals_init()
178 {
179 pthread_key_create(&globals_key, globals_destructor);
180 }
181
182
183 /*
184 * 'globals_destructor()' - Free memory allocated by _cupsGlobals().
185 */
186
187 static void
188 globals_destructor(void *value) /* I - Data to free */
189 {
190 int i; /* Looping var */
191 _ipp_buffer_t *buffer, /* Current IPP read/write buffer */
192 *next; /* Next buffer */
193 _cups_globals_t *cg; /* Global data */
194
195
196 cg = (_cups_globals_t *)value;
197
198 httpClose(cg->http);
199
200 for (i = 0; i < 3; i ++)
201 cupsFileClose(cg->stdio_files[i]);
202
203 if (cg->last_status_message)
204 _cupsStrFree(cg->last_status_message);
205
206 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
207
208 for (buffer = cg->ipp_buffers; buffer; buffer = next)
209 {
210 next = buffer->next;
211 free(buffer);
212 }
213
214 cupsArrayDelete(cg->pwg_size_lut);
215 cupsArrayDelete(cg->leg_size_lut);
216
217 free(value);
218 }
219
220
221 #else
222 /*
223 * Implement static globals...
224 */
225
226 /*
227 * '_cupsGlobals()' - Return a pointer to thread local storage.
228 */
229
230 _cups_globals_t * /* O - Pointer to global data */
231 _cupsGlobals(void)
232 {
233 static _cups_globals_t globals; /* Global data */
234 static int initialized = 0;/* Global data initialized? */
235
236
237 /*
238 * Initialize global data as needed...
239 */
240
241 if (!initialized)
242 {
243 initialized = 1;
244
245 /*
246 * Initialize global variables...
247 */
248
249 memset(&globals, 0, sizeof(globals));
250
251 globals.encryption = (http_encryption_t)-1;
252 globals.password_cb = (cups_password_cb2_t)_cupsGetPassword;
253
254 cups_env_init(&globals);
255 }
256
257 return (&globals);
258 }
259 #endif /* HAVE_PTHREAD_H */
260
261
262 /*
263 * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $".
264 */