1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*647f063dSAndrew Rist * distributed with this work for additional information
6*647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist * with the License. You may obtain a copy of the License at
10*647f063dSAndrew Rist *
11*647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*647f063dSAndrew Rist *
13*647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist * software distributed under the License is distributed on an
15*647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist * KIND, either express or implied. See the License for the
17*647f063dSAndrew Rist * specific language governing permissions and limitations
18*647f063dSAndrew Rist * under the License.
19*647f063dSAndrew Rist *
20*647f063dSAndrew Rist *************************************************************/
21*647f063dSAndrew Rist
22*647f063dSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #ifdef NO_PTHREAD_RTL
27cdf0e10cSrcweir
28cdf0e10cSrcweir static pthread_mutex_t getrtl_mutex = PTHREAD_MUTEX_INITIALIZER;
29cdf0e10cSrcweir
30cdf0e10cSrcweir /* struct passwd differs on some platforms */
31cdf0e10cSrcweir #if defined NETBSD
getpwnam_r(const char * name,struct passwd * s,char * buffer,int size)32cdf0e10cSrcweir struct passwd *getpwnam_r(const char* name, struct passwd* s, char* buffer, int size )
33cdf0e10cSrcweir {
34cdf0e10cSrcweir struct passwd* res;
35cdf0e10cSrcweir
36cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
37cdf0e10cSrcweir
38cdf0e10cSrcweir if ( (res = getpwnam(name)) )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir int nname, npasswd, nclass, ngecos, ndir;
41cdf0e10cSrcweir
42cdf0e10cSrcweir nname= strlen(res->pw_name)+1;
43cdf0e10cSrcweir npasswd= strlen(res->pw_passwd)+1;
44cdf0e10cSrcweir nclass= strlen(res->pw_class)+1;
45cdf0e10cSrcweir ngecos= strlen(res->pw_gecos)+1;
46cdf0e10cSrcweir ndir= strlen(res->pw_dir)+1;
47cdf0e10cSrcweir
48cdf0e10cSrcweir if (nname+npasswd+nclass+ngecos
49cdf0e10cSrcweir +ndir+strlen(res->pw_shell) < size)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir memcpy(s, res, sizeof(struct passwd));
52cdf0e10cSrcweir
53cdf0e10cSrcweir strcpy(buffer, res->pw_name);
54cdf0e10cSrcweir s->pw_name = buffer;
55cdf0e10cSrcweir buffer += nname;
56cdf0e10cSrcweir
57cdf0e10cSrcweir strcpy(buffer, res->pw_passwd);
58cdf0e10cSrcweir s->pw_passwd = buffer;
59cdf0e10cSrcweir buffer += npasswd;
60cdf0e10cSrcweir
61cdf0e10cSrcweir strcpy(buffer, res->pw_class);
62cdf0e10cSrcweir s->pw_class = buffer;
63cdf0e10cSrcweir buffer += nclass;
64cdf0e10cSrcweir
65cdf0e10cSrcweir strcpy(buffer, res->pw_gecos);
66cdf0e10cSrcweir s->pw_gecos = buffer;
67cdf0e10cSrcweir buffer += ngecos;
68cdf0e10cSrcweir
69cdf0e10cSrcweir strcpy(buffer, res->pw_dir);
70cdf0e10cSrcweir s->pw_dir = buffer;
71cdf0e10cSrcweir buffer += ndir;
72cdf0e10cSrcweir
73cdf0e10cSrcweir strcpy(buffer, res->pw_shell);
74cdf0e10cSrcweir s->pw_shell = buffer;
75cdf0e10cSrcweir
76cdf0e10cSrcweir res = s;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir else
79cdf0e10cSrcweir res = 0;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
83cdf0e10cSrcweir
84cdf0e10cSrcweir return(res);
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
getpwuid_r(uid_t uid,struct passwd * pwd,char * buffer,size_t buflen,struct passwd ** result)87cdf0e10cSrcweir int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
88cdf0e10cSrcweir size_t buflen, struct passwd **result)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir struct passwd* res;
91cdf0e10cSrcweir int retval = 0;
92cdf0e10cSrcweir
93cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
94cdf0e10cSrcweir
95cdf0e10cSrcweir if ( (res = getpwuid(uid)) )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir size_t pw_name, pw_passwd, pw_class, pw_gecos, pw_dir, pw_shell;
98cdf0e10cSrcweir
99cdf0e10cSrcweir pw_name = strlen(res->pw_name)+1;
100cdf0e10cSrcweir pw_passwd = strlen(res->pw_passwd)+1;
101cdf0e10cSrcweir pw_class = strlen(res->pw_class)+1;
102cdf0e10cSrcweir pw_gecos = strlen(res->pw_gecos)+1;
103cdf0e10cSrcweir pw_dir = strlen(res->pw_dir)+1;
104cdf0e10cSrcweir pw_shell = strlen(res->pw_shell)+1;
105cdf0e10cSrcweir
106cdf0e10cSrcweir if (pw_name+pw_passwd+pw_class+pw_gecos
107cdf0e10cSrcweir +pw_dir+pw_shell < buflen)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir memcpy(pwd, res, sizeof(struct passwd));
110cdf0e10cSrcweir
111cdf0e10cSrcweir strncpy(buffer, res->pw_name, pw_name);
112cdf0e10cSrcweir pwd->pw_name = buffer;
113cdf0e10cSrcweir buffer += pw_name;
114cdf0e10cSrcweir
115cdf0e10cSrcweir strncpy(buffer, res->pw_passwd, pw_passwd);
116cdf0e10cSrcweir pwd->pw_passwd = buffer;
117cdf0e10cSrcweir buffer += pw_passwd;
118cdf0e10cSrcweir
119cdf0e10cSrcweir strncpy(buffer, res->pw_class, pw_class);
120cdf0e10cSrcweir pwd->pw_class = buffer;
121cdf0e10cSrcweir buffer += pw_class;
122cdf0e10cSrcweir
123cdf0e10cSrcweir strncpy(buffer, res->pw_gecos, pw_gecos);
124cdf0e10cSrcweir pwd->pw_gecos = buffer;
125cdf0e10cSrcweir buffer += pw_gecos;
126cdf0e10cSrcweir
127cdf0e10cSrcweir strncpy(buffer, res->pw_dir, pw_dir);
128cdf0e10cSrcweir pwd->pw_dir = buffer;
129cdf0e10cSrcweir buffer += pw_dir;
130cdf0e10cSrcweir
131cdf0e10cSrcweir strncpy(buffer, res->pw_shell, pw_shell);
132cdf0e10cSrcweir pwd->pw_shell = buffer;
133cdf0e10cSrcweir buffer += pw_shell;
134cdf0e10cSrcweir
135cdf0e10cSrcweir *result = pwd ;
136cdf0e10cSrcweir retval = 0 ;
137cdf0e10cSrcweir
138cdf0e10cSrcweir }
139cdf0e10cSrcweir else
140cdf0e10cSrcweir retval = ENOMEM;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir else
143cdf0e10cSrcweir retval = errno ;
144cdf0e10cSrcweir
145cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
146cdf0e10cSrcweir
147cdf0e10cSrcweir return retval;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
localtime_r(const time_t * timep,struct tm * buffer)150cdf0e10cSrcweir struct tm *localtime_r(const time_t *timep, struct tm *buffer)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir struct tm* res;
153cdf0e10cSrcweir
154cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
155cdf0e10cSrcweir
156cdf0e10cSrcweir if ( (res = localtime(timep)))
157cdf0e10cSrcweir {
158cdf0e10cSrcweir memcpy(buffer, res, sizeof(struct tm));
159cdf0e10cSrcweir res = buffer;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir
162cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
163cdf0e10cSrcweir
164cdf0e10cSrcweir return res;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
gmtime_r(const time_t * timep,struct tm * buffer)167cdf0e10cSrcweir struct tm *gmtime_r(const time_t *timep, struct tm *buffer)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir struct tm* res;
170cdf0e10cSrcweir
171cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
172cdf0e10cSrcweir
173cdf0e10cSrcweir if ( (res = gmtime(timep)) )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir memcpy(buffer, res, sizeof(struct tm));
176cdf0e10cSrcweir res = buffer;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
180cdf0e10cSrcweir
181cdf0e10cSrcweir return res;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir #endif /* defined NETBSD */
184cdf0e10cSrcweir
185cdf0e10cSrcweir #ifdef SCO
186cdf0e10cSrcweir #include <pwd.h>
187cdf0e10cSrcweir #include <shadow.h>
188cdf0e10cSrcweir #include <sys/types.h>
189cdf0e10cSrcweir
getspnam_r(const char * name,struct spwd * s,char * buffer,int size)190cdf0e10cSrcweir struct spwd *getspnam_r(const char *name, struct spwd* s, char* buffer, int size )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir struct spwd* res;
193cdf0e10cSrcweir
194cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
195cdf0e10cSrcweir
196cdf0e10cSrcweir if ( res = getspnam(name) )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir int nnamp;
199cdf0e10cSrcweir
200cdf0e10cSrcweir nnamp = strlen(res->sp_namp)+1;
201cdf0e10cSrcweir
202cdf0e10cSrcweir if (nnamp+strlen(res->sp_pwdp) < size) {
203cdf0e10cSrcweir memcpy(s, res, sizeof(struct spwd));
204cdf0e10cSrcweir
205cdf0e10cSrcweir strcpy(buffer, res->sp_namp);
206cdf0e10cSrcweir s->sp_namp = buffer;
207cdf0e10cSrcweir buffer += nnamp;
208cdf0e10cSrcweir
209cdf0e10cSrcweir strcpy(buffer, res->sp_pwdp);
210cdf0e10cSrcweir s->sp_pwdp = buffer;
211cdf0e10cSrcweir
212cdf0e10cSrcweir res = s;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir else
215cdf0e10cSrcweir res = 0;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir
218cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
219cdf0e10cSrcweir
220cdf0e10cSrcweir return res;
221cdf0e10cSrcweir }
222cdf0e10cSrcweir
getpwnam_r(const char * name,struct passwd * s,char * buffer,int size)223cdf0e10cSrcweir struct passwd *getpwnam_r(const char* name, struct passwd* s, char* buffer, int size )
224cdf0e10cSrcweir {
225cdf0e10cSrcweir struct passwd* res;
226cdf0e10cSrcweir
227cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
228cdf0e10cSrcweir
229cdf0e10cSrcweir if ( res = getpwnam(name) )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir int nname, npasswd, nage;
232cdf0e10cSrcweir int ncomment, ngecos, ndir;
233cdf0e10cSrcweir
234cdf0e10cSrcweir nname= strlen(res->pw_name)+1;
235cdf0e10cSrcweir npasswd= strlen(res->pw_passwd)+1;
236cdf0e10cSrcweir nage= strlen(res->pw_age)+1;
237cdf0e10cSrcweir ncomment= strlen(res->pw_comment)+1;
238cdf0e10cSrcweir ngecos= strlen(res->pw_gecos)+1;
239cdf0e10cSrcweir ndir= strlen(res->pw_dir)+1;
240cdf0e10cSrcweir
241cdf0e10cSrcweir if (nname+npasswd+nage+ncomment+ngecos+ndir
242cdf0e10cSrcweir +strlen(res->pw_shell) < size)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir memcpy(s, res, sizeof(struct passwd));
245cdf0e10cSrcweir
246cdf0e10cSrcweir strcpy(buffer, res->pw_name);
247cdf0e10cSrcweir s->pw_name = buffer;
248cdf0e10cSrcweir buffer += nname;
249cdf0e10cSrcweir
250cdf0e10cSrcweir strcpy(buffer, res->pw_passwd);
251cdf0e10cSrcweir s->pw_passwd = buffer;
252cdf0e10cSrcweir buffer += npasswd;
253cdf0e10cSrcweir
254cdf0e10cSrcweir strcpy(buffer, res->pw_age);
255cdf0e10cSrcweir s->pw_age = buffer;
256cdf0e10cSrcweir buffer += nage;
257cdf0e10cSrcweir
258cdf0e10cSrcweir strcpy(buffer, res->pw_comment);
259cdf0e10cSrcweir s->pw_comment = buffer;
260cdf0e10cSrcweir buffer += ncomment;
261cdf0e10cSrcweir
262cdf0e10cSrcweir strcpy(buffer, res->pw_gecos);
263cdf0e10cSrcweir s->pw_gecos = buffer;
264cdf0e10cSrcweir buffer += ngecos;
265cdf0e10cSrcweir
266cdf0e10cSrcweir strcpy(buffer, res->pw_dir);
267cdf0e10cSrcweir s->pw_dir = buffer;
268cdf0e10cSrcweir buffer += ndir;
269cdf0e10cSrcweir
270cdf0e10cSrcweir strcpy(buffer, res->pw_shell);
271cdf0e10cSrcweir s->pw_shell = buffer;
272cdf0e10cSrcweir
273cdf0e10cSrcweir res = s;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir else
276cdf0e10cSrcweir res = 0;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir
279cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
280cdf0e10cSrcweir
281cdf0e10cSrcweir return res;
282cdf0e10cSrcweir }
283cdf0e10cSrcweir #endif /* defined SCO */
284cdf0e10cSrcweir
285cdf0e10cSrcweir #if !defined(FREEBSD) || (__FreeBSD_version < 601103)
286cdf0e10cSrcweir
287cdf0e10cSrcweir extern int h_errno;
288cdf0e10cSrcweir
gethostbyname_r(const char * name,struct hostent * result,char * buffer,int buflen,int * h_errnop)289cdf0e10cSrcweir struct hostent *gethostbyname_r(const char *name, struct hostent *result,
290cdf0e10cSrcweir char *buffer, int buflen, int *h_errnop)
291cdf0e10cSrcweir {
292cdf0e10cSrcweir /* buffer layout: name\0
293cdf0e10cSrcweir * array_of_pointer_to_aliases
294cdf0e10cSrcweir * NULL
295cdf0e10cSrcweir * alias1\0...aliasn\0
296cdf0e10cSrcweir * array_of_pointer_to_addresses
297cdf0e10cSrcweir * NULL
298cdf0e10cSrcweir * addr1addr2addr3...addrn
299cdf0e10cSrcweir */
300cdf0e10cSrcweir struct hostent* res;
301cdf0e10cSrcweir
302cdf0e10cSrcweir pthread_mutex_lock(&getrtl_mutex);
303cdf0e10cSrcweir
304cdf0e10cSrcweir if ( (res = gethostbyname(name)) )
305cdf0e10cSrcweir {
306cdf0e10cSrcweir int nname, naliases, naddr_list, naliasesdata, n;
307cdf0e10cSrcweir char **p, **parray, *data;
308cdf0e10cSrcweir
309cdf0e10cSrcweir /* Check buffer size before copying, we want to leave the
310cdf0e10cSrcweir * buffers unmodified in case something goes wrong.
311cdf0e10cSrcweir *
312cdf0e10cSrcweir * Is this required?
313cdf0e10cSrcweir */
314cdf0e10cSrcweir
315cdf0e10cSrcweir nname= strlen(res->h_name)+1;
316cdf0e10cSrcweir
317cdf0e10cSrcweir naliases = naddr_list = naliasesdata = 0;
318cdf0e10cSrcweir
319cdf0e10cSrcweir for ( p = res->h_aliases; *p != NULL; p++) {
320cdf0e10cSrcweir naliases++;
321cdf0e10cSrcweir naliasesdata += strlen(*p)+1;
322cdf0e10cSrcweir }
323cdf0e10cSrcweir
324cdf0e10cSrcweir for ( p = res->h_addr_list; *p != NULL; p++)
325cdf0e10cSrcweir naddr_list++;
326cdf0e10cSrcweir
327cdf0e10cSrcweir if ( nname
328cdf0e10cSrcweir + (naliases+1)*sizeof(char*) + naliasesdata
329cdf0e10cSrcweir + (naddr_list+1)*sizeof(char*) + naddr_list*res->h_length
330cdf0e10cSrcweir <= buflen )
331cdf0e10cSrcweir {
332cdf0e10cSrcweir memcpy(result, res, sizeof(struct hostent));
333cdf0e10cSrcweir
334cdf0e10cSrcweir strcpy(buffer, res->h_name);
335cdf0e10cSrcweir result->h_name = buffer;
336cdf0e10cSrcweir buffer += nname;
337cdf0e10cSrcweir
338cdf0e10cSrcweir parray = (char**)buffer;
339cdf0e10cSrcweir result->h_aliases = parray;
340cdf0e10cSrcweir data = buffer + (naliases+1)*sizeof(char*);
341cdf0e10cSrcweir for ( p = res->h_aliases; *p != NULL; p++) {
342cdf0e10cSrcweir n = strlen(*p)+1;
343cdf0e10cSrcweir *parray++ = data;
344cdf0e10cSrcweir memcpy(data, *p, n);
345cdf0e10cSrcweir data += n;
346cdf0e10cSrcweir }
347cdf0e10cSrcweir *parray = NULL;
348cdf0e10cSrcweir buffer = data;
349cdf0e10cSrcweir parray = (char**)buffer;
350cdf0e10cSrcweir result->h_addr_list = parray;
351cdf0e10cSrcweir data = buffer + (naddr_list+1)*sizeof(char*);
352cdf0e10cSrcweir for ( p = res->h_addr_list; *p != NULL; p++) {
353cdf0e10cSrcweir *parray++ = data;
354cdf0e10cSrcweir memcpy(data, *p, res->h_length);
355cdf0e10cSrcweir data += res->h_length;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir *parray = NULL;
358cdf0e10cSrcweir
359cdf0e10cSrcweir res = result;
360cdf0e10cSrcweir }
361cdf0e10cSrcweir else
362cdf0e10cSrcweir {
363cdf0e10cSrcweir errno = ERANGE;
364cdf0e10cSrcweir res = NULL;
365cdf0e10cSrcweir }
366cdf0e10cSrcweir }
367cdf0e10cSrcweir else
368cdf0e10cSrcweir {
369cdf0e10cSrcweir *h_errnop = h_errno;
370cdf0e10cSrcweir }
371cdf0e10cSrcweir
372cdf0e10cSrcweir pthread_mutex_unlock(&getrtl_mutex);
373cdf0e10cSrcweir
374cdf0e10cSrcweir return res;
375cdf0e10cSrcweir }
376cdf0e10cSrcweir #endif /* !defined(FREEBSD) || (__FreeBSD_version < 601103) */
377cdf0e10cSrcweir
378cdf0e10cSrcweir #if defined(MACOSX)
379cdf0e10cSrcweir /*
380cdf0e10cSrcweir * Add support for resolving Mac native alias files (not the same as unix alias files)
381cdf0e10cSrcweir * returns 0 on success.
382cdf0e10cSrcweir */
macxp_resolveAlias(char * path,int buflen)383cdf0e10cSrcweir int macxp_resolveAlias(char *path, int buflen)
384cdf0e10cSrcweir {
385cdf0e10cSrcweir FSRef aFSRef;
386cdf0e10cSrcweir OSStatus nErr;
387cdf0e10cSrcweir Boolean bFolder;
388cdf0e10cSrcweir Boolean bAliased;
389cdf0e10cSrcweir char *unprocessedPath = path;
390cdf0e10cSrcweir
391cdf0e10cSrcweir if ( *unprocessedPath == '/' )
392cdf0e10cSrcweir unprocessedPath++;
393cdf0e10cSrcweir
394cdf0e10cSrcweir int nRet = 0;
395cdf0e10cSrcweir while ( !nRet && unprocessedPath && *unprocessedPath )
396cdf0e10cSrcweir {
397cdf0e10cSrcweir unprocessedPath = strchr( unprocessedPath, '/' );
398cdf0e10cSrcweir if ( unprocessedPath )
399cdf0e10cSrcweir *unprocessedPath = '\0';
400cdf0e10cSrcweir
401cdf0e10cSrcweir nErr = noErr;
402cdf0e10cSrcweir bFolder = FALSE;
403cdf0e10cSrcweir bAliased = FALSE;
404cdf0e10cSrcweir if ( FSPathMakeRef( (const UInt8 *)path, &aFSRef, 0 ) == noErr )
405cdf0e10cSrcweir {
406cdf0e10cSrcweir nErr = FSResolveAliasFileWithMountFlags( &aFSRef, TRUE, &bFolder, &bAliased, kResolveAliasFileNoUI );
407cdf0e10cSrcweir if ( nErr == nsvErr )
408cdf0e10cSrcweir {
409cdf0e10cSrcweir errno = ENOENT;
410cdf0e10cSrcweir nRet = -1;
411cdf0e10cSrcweir }
412cdf0e10cSrcweir else if ( nErr == noErr && bAliased )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir char tmpPath[ PATH_MAX ];
415cdf0e10cSrcweir if ( FSRefMakePath( &aFSRef, (UInt8 *)tmpPath, PATH_MAX ) == noErr )
416cdf0e10cSrcweir {
417cdf0e10cSrcweir int nLen = strlen( tmpPath ) + ( unprocessedPath ? strlen( unprocessedPath + 1 ) + 1 : 0 );
418cdf0e10cSrcweir if ( nLen < buflen && nLen < PATH_MAX )
419cdf0e10cSrcweir {
420cdf0e10cSrcweir if ( unprocessedPath )
421cdf0e10cSrcweir {
422cdf0e10cSrcweir int nTmpPathLen = strlen( tmpPath );
423cdf0e10cSrcweir strcat( tmpPath, "/" );
424cdf0e10cSrcweir strcat( tmpPath, unprocessedPath + 1 );
425cdf0e10cSrcweir strcpy( path, tmpPath);
426cdf0e10cSrcweir unprocessedPath = path + nTmpPathLen;
427cdf0e10cSrcweir }
428cdf0e10cSrcweir else if ( !unprocessedPath )
429cdf0e10cSrcweir {
430cdf0e10cSrcweir strcpy( path, tmpPath);
431cdf0e10cSrcweir }
432cdf0e10cSrcweir }
433cdf0e10cSrcweir else
434cdf0e10cSrcweir {
435cdf0e10cSrcweir errno = ENAMETOOLONG;
436cdf0e10cSrcweir nRet = -1;
437cdf0e10cSrcweir }
438cdf0e10cSrcweir }
439cdf0e10cSrcweir }
440cdf0e10cSrcweir }
441cdf0e10cSrcweir
442cdf0e10cSrcweir if ( unprocessedPath )
443cdf0e10cSrcweir *unprocessedPath++ = '/';
444cdf0e10cSrcweir }
445cdf0e10cSrcweir
446cdf0e10cSrcweir return nRet;
447cdf0e10cSrcweir }
448cdf0e10cSrcweir
449cdf0e10cSrcweir #endif /* defined MACOSX */
450cdf0e10cSrcweir
451cdf0e10cSrcweir #endif /* NO_PTHREAD_RTL */
452cdf0e10cSrcweir
453cdf0e10cSrcweir #if (defined (LINUX) && (GLIBC >= 2))
454cdf0e10cSrcweir /* The linux kernel thread implemention, always return the pid of the
455cdf0e10cSrcweir thread subprocess and not of the main process. So we save the main
456cdf0e10cSrcweir pid at startup
457cdf0e10cSrcweir */
458cdf0e10cSrcweir
459cdf0e10cSrcweir // Directly from libc.so.6, obviously missing from some unistd.h:
460cdf0e10cSrcweir extern __pid_t __getpid(void);
461cdf0e10cSrcweir
462cdf0e10cSrcweir static pid_t pid = -1;
463cdf0e10cSrcweir
464cdf0e10cSrcweir static void savePid(void) __attribute__((constructor));
465cdf0e10cSrcweir
savePid(void)466cdf0e10cSrcweir static void savePid(void)
467cdf0e10cSrcweir {
468cdf0e10cSrcweir if (pid == -1)
469cdf0e10cSrcweir pid = __getpid();
470cdf0e10cSrcweir }
471cdf0e10cSrcweir
getpid(void)472cdf0e10cSrcweir pid_t getpid(void)
473cdf0e10cSrcweir {
474cdf0e10cSrcweir if (pid == -1)
475cdf0e10cSrcweir savePid();
476cdf0e10cSrcweir
477cdf0e10cSrcweir return (pid);
478cdf0e10cSrcweir }
479cdf0e10cSrcweir #endif /* (defined (LINUX) && (GLIBC >= 2)) */
480cdf0e10cSrcweir
481cdf0e10cSrcweir #ifdef NO_PTHREAD_SEMAPHORES
sem_init(sem_t * sem,int pshared,unsigned int value)482cdf0e10cSrcweir int sem_init(sem_t* sem, int pshared, unsigned int value)
483cdf0e10cSrcweir {
484cdf0e10cSrcweir pthread_mutex_init(&sem->mutex, PTHREAD_MUTEXATTR_DEFAULT);
485cdf0e10cSrcweir pthread_cond_init(&sem->increased, PTHREAD_CONDATTR_DEFAULT);
486cdf0e10cSrcweir
487cdf0e10cSrcweir sem->value = (int)value;
488cdf0e10cSrcweir return 0;
489cdf0e10cSrcweir }
490cdf0e10cSrcweir
sem_destroy(sem_t * sem)491cdf0e10cSrcweir int sem_destroy(sem_t* sem)
492cdf0e10cSrcweir {
493cdf0e10cSrcweir pthread_mutex_destroy(&sem->mutex);
494cdf0e10cSrcweir pthread_cond_destroy(&sem->increased);
495cdf0e10cSrcweir sem->value = 0;
496cdf0e10cSrcweir return 0;
497cdf0e10cSrcweir }
498cdf0e10cSrcweir
sem_wait(sem_t * sem)499cdf0e10cSrcweir int sem_wait(sem_t* sem)
500cdf0e10cSrcweir {
501cdf0e10cSrcweir pthread_mutex_lock(&sem->mutex);
502cdf0e10cSrcweir
503cdf0e10cSrcweir while (sem->value <= 0)
504cdf0e10cSrcweir {
505cdf0e10cSrcweir pthread_cond_wait(&sem->increased, &sem->mutex);
506cdf0e10cSrcweir }
507cdf0e10cSrcweir
508cdf0e10cSrcweir sem->value--;
509cdf0e10cSrcweir pthread_mutex_unlock(&sem->mutex);
510cdf0e10cSrcweir
511cdf0e10cSrcweir return 0;
512cdf0e10cSrcweir }
513cdf0e10cSrcweir
sem_trywait(sem_t * sem)514cdf0e10cSrcweir int sem_trywait(sem_t* sem)
515cdf0e10cSrcweir {
516cdf0e10cSrcweir int result = 0;
517cdf0e10cSrcweir
518cdf0e10cSrcweir pthread_mutex_lock(&sem->mutex);
519cdf0e10cSrcweir
520cdf0e10cSrcweir if (sem->value > 0)
521cdf0e10cSrcweir {
522cdf0e10cSrcweir sem->value--;
523cdf0e10cSrcweir }
524cdf0e10cSrcweir else
525cdf0e10cSrcweir {
526cdf0e10cSrcweir errno = EAGAIN;
527cdf0e10cSrcweir result = -1;
528cdf0e10cSrcweir }
529cdf0e10cSrcweir
530cdf0e10cSrcweir pthread_mutex_unlock(&sem->mutex);
531cdf0e10cSrcweir
532cdf0e10cSrcweir return result;
533cdf0e10cSrcweir }
534cdf0e10cSrcweir
sem_post(sem_t * sem)535cdf0e10cSrcweir int sem_post(sem_t* sem)
536cdf0e10cSrcweir {
537cdf0e10cSrcweir pthread_mutex_lock(&sem->mutex);
538cdf0e10cSrcweir
539cdf0e10cSrcweir sem->value++;
540cdf0e10cSrcweir
541cdf0e10cSrcweir pthread_mutex_unlock(&sem->mutex);
542cdf0e10cSrcweir
543cdf0e10cSrcweir pthread_cond_signal(&sem->increased);
544cdf0e10cSrcweir
545cdf0e10cSrcweir return 0;
546cdf0e10cSrcweir }
547cdf0e10cSrcweir #endif
548cdf0e10cSrcweir
549cdf0e10cSrcweir #if defined(FREEBSD)
fcvt(double value,int ndigit,int * decpt,int * sign)550cdf0e10cSrcweir char *fcvt(double value, int ndigit, int *decpt, int *sign)
551cdf0e10cSrcweir {
552cdf0e10cSrcweir static char ret[256];
553cdf0e10cSrcweir char buf[256],zahl[256],format[256]="%";
554cdf0e10cSrcweir char *v1,*v2;
555cdf0e10cSrcweir
556cdf0e10cSrcweir if (value==0.0) value=1e-30;
557cdf0e10cSrcweir
558cdf0e10cSrcweir if (value<0.0) *sign=1; else *sign=0;
559cdf0e10cSrcweir
560cdf0e10cSrcweir if (value<1.0)
561cdf0e10cSrcweir {
562cdf0e10cSrcweir *decpt=(int)log10(value);
563cdf0e10cSrcweir value*=pow(10.0,1-*decpt);
564cdf0e10cSrcweir ndigit+=*decpt-1;
565cdf0e10cSrcweir if (ndigit<0) ndigit=0;
566cdf0e10cSrcweir }
567cdf0e10cSrcweir else
568cdf0e10cSrcweir {
569cdf0e10cSrcweir *decpt=(int)log10(value)+1;
570cdf0e10cSrcweir }
571cdf0e10cSrcweir
572cdf0e10cSrcweir sprintf(zahl,"%d",ndigit);
573cdf0e10cSrcweir strcat(format,zahl);
574cdf0e10cSrcweir strcat(format,".");
575cdf0e10cSrcweir strcat(format,zahl);
576cdf0e10cSrcweir strcat(format,"f");
577cdf0e10cSrcweir
578cdf0e10cSrcweir sprintf(buf,format,value);
579cdf0e10cSrcweir
580cdf0e10cSrcweir if (ndigit!=0)
581cdf0e10cSrcweir {
582cdf0e10cSrcweir v1=strtok(buf,".");
583cdf0e10cSrcweir v2=strtok(NULL,".");
584cdf0e10cSrcweir strcpy(ret,v1);
585cdf0e10cSrcweir strcat(ret,v2);
586cdf0e10cSrcweir }
587cdf0e10cSrcweir else
588cdf0e10cSrcweir {
589cdf0e10cSrcweir strcpy(ret,buf);
590cdf0e10cSrcweir }
591cdf0e10cSrcweir
592cdf0e10cSrcweir return(ret);
593cdf0e10cSrcweir }
594cdf0e10cSrcweir
595cdf0e10cSrcweir #endif
596