2 * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $"
4 * Global variable access routines for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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().
26 * Include necessary headers...
29 #include "cups-private.h"
33 * 'cups_env_init()' - Initialize environment variables.
37 cups_env_init(_cups_globals_t
*g
) /* I - Global data */
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 */
48 * Open the registry...
51 strcpy(installdir
, "C:/Program Files/cups.org");
53 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
57 * Grab the installation directory...
60 size
= sizeof(installdir
);
61 RegQueryValueEx(key
, "installdir", NULL
, NULL
, installdir
, &size
);
65 snprintf(confdir
, sizeof(confdir
), "%s/conf", installdir
);
66 snprintf(localedir
, sizeof(localedir
), "%s/locale", installdir
);
68 if ((g
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
69 g
->cups_datadir
= installdir
;
71 if ((g
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
72 g
->cups_serverbin
= installdir
;
74 if ((g
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
75 g
->cups_serverroot
= confdir
;
77 if ((g
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
78 g
->cups_statedir
= confdir
;
80 if ((g
->localedir
= getenv("LOCALEDIR")) == NULL
)
81 g
->localedir
= localedir
;
84 if ((g
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
85 g
->cups_datadir
= CUPS_DATADIR
;
87 if ((g
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
88 g
->cups_serverbin
= CUPS_SERVERBIN
;
90 if ((g
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
91 g
->cups_serverroot
= CUPS_SERVERROOT
;
93 if ((g
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
94 g
->cups_statedir
= CUPS_STATEDIR
;
96 if ((g
->localedir
= getenv("LOCALEDIR")) == NULL
)
97 g
->localedir
= CUPS_LOCALEDIR
;
102 #ifdef HAVE_PTHREAD_H
104 * Implement per-thread globals...
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 */
121 static void globals_init();
122 static void globals_destructor(void *value
);
126 * '_cupsGlobals()' - Return a pointer to thread local storage
129 _cups_globals_t
* /* O - Pointer to global data */
132 _cups_globals_t
*globals
; /* Pointer to global data */
136 * Initialize the global data exactly once...
139 pthread_once(&globals_key_once
, globals_init
);
142 * See if we have allocated the data yet...
145 if ((globals
= (_cups_globals_t
*)pthread_getspecific(globals_key
)) == NULL
)
148 * No, allocate memory as set the pointer for the key...
151 globals
= calloc(1, sizeof(_cups_globals_t
));
152 pthread_setspecific(globals_key
, globals
);
155 * Initialize variables that have non-zero values
158 globals
->encryption
= (http_encryption_t
)-1;
159 globals
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
161 cups_env_init(globals
);
165 * Return the pointer to the data...
173 * 'globals_init()' - Initialize globals once.
179 pthread_key_create(&globals_key
, globals_destructor
);
184 * 'globals_destructor()' - Free memory allocated by _cupsGlobals().
188 globals_destructor(void *value
) /* I - Data to free */
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 */
196 cg
= (_cups_globals_t
*)value
;
200 for (i
= 0; i
< 3; i
++)
201 cupsFileClose(cg
->stdio_files
[i
]);
203 if (cg
->last_status_message
)
204 _cupsStrFree(cg
->last_status_message
);
206 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
208 for (buffer
= cg
->ipp_buffers
; buffer
; buffer
= next
)
214 cupsArrayDelete(cg
->pwg_size_lut
);
215 cupsArrayDelete(cg
->leg_size_lut
);
223 * Implement static globals...
227 * '_cupsGlobals()' - Return a pointer to thread local storage.
230 _cups_globals_t
* /* O - Pointer to global data */
233 static _cups_globals_t globals
; /* Global data */
234 static int initialized
= 0;/* Global data initialized? */
238 * Initialize global data as needed...
246 * Initialize global variables...
249 memset(&globals
, 0, sizeof(globals
));
251 globals
.encryption
= (http_encryption_t
)-1;
252 globals
.password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
254 cups_env_init(&globals
);
259 #endif /* HAVE_PTHREAD_H */
263 * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $".