Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / nwlib.c
blob3c95b38493ebb2181aa46ab80de9fc7ca02e50da
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: nwlib.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #ifdef NETWARE /* Novell NetWare */
26 #include <stdlib.h>
28 #ifdef __NOVELL_LIBC__
29 /* For native LibC-based NLM we need to register as a real lib. */
30 #include <errno.h>
31 #include <string.h>
32 #include <library.h>
33 #include <netware.h>
34 #include <screen.h>
35 #include <nks/thread.h>
36 #include <nks/synch.h>
39 typedef struct
41 int _errno;
42 void *twentybytes;
43 } libthreaddata_t;
45 typedef struct
47 int x;
48 int y;
49 int z;
50 void *tenbytes;
51 NXKey_t perthreadkey; /* if -1, no key obtained... */
52 NXMutex_t *lock;
53 } libdata_t;
55 int gLibId = -1;
56 void *gLibHandle = (void *) NULL;
57 rtag_t gAllocTag = (rtag_t) NULL;
58 NXMutex_t *gLibLock = (NXMutex_t *) NULL;
60 /* internal library function prototypes... */
61 int DisposeLibraryData ( void * );
62 void DisposeThreadData ( void * );
63 int GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );
66 int _NonAppStart( void *NLMHandle,
67 void *errorScreen,
68 const char *cmdLine,
69 const char *loadDirPath,
70 size_t uninitializedDataLength,
71 void *NLMFileHandle,
72 int (*readRoutineP)( int conn,
73 void *fileHandle, size_t offset,
74 size_t nbytes,
75 size_t *bytesRead,
76 void *buffer ),
77 size_t customDataOffset,
78 size_t customDataSize,
79 int messageCount,
80 const char **messages )
82 NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
84 #ifndef __GNUC__
85 #pragma unused(cmdLine)
86 #pragma unused(loadDirPath)
87 #pragma unused(uninitializedDataLength)
88 #pragma unused(NLMFileHandle)
89 #pragma unused(readRoutineP)
90 #pragma unused(customDataOffset)
91 #pragma unused(customDataSize)
92 #pragma unused(messageCount)
93 #pragma unused(messages)
94 #endif
97 ** Here we process our command line, post errors (to the error screen),
98 ** perform initializations and anything else we need to do before being able
99 ** to accept calls into us. If we succeed, we return non-zero and the NetWare
100 ** Loader will leave us up, otherwise we fail to load and get dumped.
102 gAllocTag = AllocateResourceTag(NLMHandle,
103 "<library-name> memory allocations",
104 AllocSignature);
106 if(!gAllocTag) {
107 OutputToScreen(errorScreen, "Unable to allocate resource tag for "
108 "library memory allocations.\n");
109 return -1;
112 gLibId = register_library(DisposeLibraryData);
114 if(gLibId < -1) {
115 OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
116 return -1;
119 gLibHandle = NLMHandle;
121 gLibLock = NXMutexAlloc(0, 0, &liblock);
123 if(!gLibLock) {
124 OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
125 return -1;
128 return 0;
132 ** Here we clean up any resources we allocated. Resource tags is a big part
133 ** of what we created, but NetWare doesn't ask us to free those.
135 void _NonAppStop( void )
137 (void) unregister_library(gLibId);
138 NXMutexFree(gLibLock);
142 ** This function cannot be the first in the file for if the file is linked
143 ** first, then the check-unload function's offset will be nlmname.nlm+0
144 ** which is how to tell that there isn't one. When the check function is
145 ** first in the linked objects, it is ambiguous. For this reason, we will
146 ** put it inside this file after the stop function.
148 ** Here we check to see if it's alright to ourselves to be unloaded. If not,
149 ** we return a non-zero value. Right now, there isn't any reason not to allow
150 ** it.
152 int _NonAppCheckUnload( void )
154 return 0;
157 int GetOrSetUpData(int id, libdata_t **appData,
158 libthreaddata_t **threadData )
160 int err;
161 libdata_t *app_data;
162 libthreaddata_t *thread_data;
163 NXKey_t key;
164 NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
166 err = 0;
167 thread_data = (libthreaddata_t *) NULL;
170 ** Attempt to get our data for the application calling us. This is where we
171 ** store whatever application-specific information we need to carry in support
172 ** of calling applications.
174 app_data = (libdata_t *) get_app_data(id);
176 if(!app_data) {
178 ** This application hasn't called us before; set up application AND per-thread
179 ** data. Of course, just in case a thread from this same application is calling
180 ** us simultaneously, we better lock our application data-creation mutex. We
181 ** also need to recheck for data after we acquire the lock because WE might be
182 ** that other thread that was too late to create the data and the first thread
183 ** in will have created it.
185 NXLock(gLibLock);
187 if(!(app_data = (libdata_t *) get_app_data(id))) {
188 app_data = (libdata_t *) malloc(sizeof(libdata_t));
190 if(app_data) {
191 memset(app_data, 0, sizeof(libdata_t));
193 app_data->tenbytes = malloc(10);
194 app_data->lock = NXMutexAlloc(0, 0, &liblock);
196 if(!app_data->tenbytes || !app_data->lock) {
197 if(app_data->lock)
198 NXMutexFree(app_data->lock);
200 free(app_data);
201 app_data = (libdata_t *) NULL;
202 err = ENOMEM;
205 if(app_data) {
207 ** Here we burn in the application data that we were trying to get by calling
208 ** get_app_data(). Next time we call the first function, we'll get this data
209 ** we're just now setting. We also go on here to establish the per-thread data
210 ** for the calling thread, something we'll have to do on each application
211 ** thread the first time it calls us.
213 err = set_app_data(gLibId, app_data);
215 if(err) {
216 free(app_data);
217 app_data = (libdata_t *) NULL;
218 err = ENOMEM;
220 else {
221 /* create key for thread-specific data... */
222 err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
224 if(err) /* (no more keys left?) */
225 key = -1;
227 app_data->perthreadkey = key;
233 NXUnlock(gLibLock);
236 if(app_data) {
237 key = app_data->perthreadkey;
239 if(key != -1 /* couldn't create a key? no thread data */
240 && !(err = NXKeyGetValue(key, (void **) &thread_data))
241 && !thread_data) {
243 ** Allocate the per-thread data for the calling thread. Regardless of whether
244 ** there was already application data or not, this may be the first call by a
245 ** a new thread. The fact that we allocation 20 bytes on a pointer is not very
246 ** important, this just helps to demonstrate that we can have arbitrarily
247 ** complex per-thread data.
249 thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t));
251 if(thread_data) {
252 thread_data->_errno = 0;
253 thread_data->twentybytes = malloc(20);
255 if(!thread_data->twentybytes) {
256 free(thread_data);
257 thread_data = (libthreaddata_t *) NULL;
258 err = ENOMEM;
261 if((err = NXKeySetValue(key, thread_data))) {
262 free(thread_data->twentybytes);
263 free(thread_data);
264 thread_data = (libthreaddata_t *) NULL;
270 if(appData)
271 *appData = app_data;
273 if(threadData)
274 *threadData = thread_data;
276 return err;
279 int DisposeLibraryData( void *data )
281 if(data) {
282 void *tenbytes = ((libdata_t *) data)->tenbytes;
284 if(tenbytes)
285 free(tenbytes);
287 free(data);
290 return 0;
293 void DisposeThreadData( void *data )
295 if(data) {
296 void *twentybytes = ((libthreaddata_t *) data)->twentybytes;
298 if(twentybytes)
299 free(twentybytes);
301 free(data);
305 #else /* __NOVELL_LIBC__ */
306 /* For native CLib-based NLM seems we can do a bit more simple. */
307 #include <nwthread.h>
309 int main ( void )
311 /* initialize any globals here... */
313 /* do this if any global initializing was done
314 SynchronizeStart();
316 ExitThread (TSR_THREAD, 0);
317 return 0;
320 #endif /* __NOVELL_LIBC__ */
322 #endif /* NETWARE */