update credits
[LibreOffice.git] / sal / osl / unx / system.c
blob8c8280dff70c73fad3167bf93182ca4ca814ce12
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "system.h"
22 #ifdef NO_PTHREAD_RTL
24 /* struct passwd differs on some platforms */
26 #if defined(MACOSX) || defined(IOS) || defined(OPENBSD) || defined(NETBSD)
28 //No mutex needed on Mac OS X, gethostbyname is thread safe
30 #if defined(MACOSX)
32 #define RTL_MUTEX_LOCK
33 #define RTL_MUTEX_UNLOCK
35 #else //defined(MACOSX)
37 static pthread_mutex_t getrtl_mutex = PTHREAD_MUTEX_INITIALIZER;
39 #define RTL_MUTEX_LOCK pthread_mutex_lock(&getrtl_mutex);
40 #define RTL_MUTEX_UNLOCK pthread_mutex_unlock(&getrtl_mutex);
42 #endif //defined(MACOSX)
44 extern int h_errno;
46 struct hostent *gethostbyname_r(const char *name, struct hostent *result,
47 char *buffer, size_t buflen, int *h_errnop)
49 /* buffer layout: name\0
50 * array_of_pointer_to_aliases
51 * NULL
52 * alias1\0...aliasn\0
53 * array_of_pointer_to_addresses
54 * NULL
55 * addr1addr2addr3...addrn
57 struct hostent* res;
59 RTL_MUTEX_LOCK
61 if ( (res = gethostbyname(name)) )
63 int nname, naliases, naddr_list, naliasesdata, n;
64 char **p, **parray, *data;
66 /* Check buffer size before copying, we want to leave the
67 * buffers unmodified in case something goes wrong.
69 * Is this required?
72 nname= strlen(res->h_name)+1;
74 naliases = naddr_list = naliasesdata = 0;
76 for ( p = res->h_aliases; *p != NULL; p++) {
77 naliases++;
78 naliasesdata += strlen(*p)+1;
81 for ( p = res->h_addr_list; *p != NULL; p++)
82 naddr_list++;
84 if ( nname
85 + (naliases+1)*sizeof(char*) + naliasesdata
86 + (naddr_list+1)*sizeof(char*) + naddr_list*res->h_length
87 <= buflen )
89 memcpy(result, res, sizeof(struct hostent));
91 strcpy(buffer, res->h_name);
92 result->h_name = buffer;
93 buffer += nname;
95 parray = (char**)buffer;
96 result->h_aliases = parray;
97 data = buffer + (naliases+1)*sizeof(char*);
98 for ( p = res->h_aliases; *p != NULL; p++) {
99 n = strlen(*p)+1;
100 *parray++ = data;
101 memcpy(data, *p, n);
102 data += n;
104 *parray = NULL;
105 buffer = data;
106 parray = (char**)buffer;
107 result->h_addr_list = parray;
108 data = buffer + (naddr_list+1)*sizeof(char*);
109 for ( p = res->h_addr_list; *p != NULL; p++) {
110 *parray++ = data;
111 memcpy(data, *p, res->h_length);
112 data += res->h_length;
114 *parray = NULL;
116 res = result;
118 else
120 errno = ERANGE;
121 res = NULL;
124 else
126 *h_errnop = h_errno;
129 RTL_MUTEX_UNLOCK
131 return res;
133 #endif // OSX || IOS || OPENBSD || NETBSD
135 #if defined(MACOSX)
137 * Add support for resolving Mac native alias files (not the same as unix alias files)
138 * returns 0 on success.
140 int macxp_resolveAlias(char *path, int buflen)
142 FSRef aFSRef;
143 OSStatus nErr;
144 Boolean bFolder;
145 Boolean bAliased;
146 char *unprocessedPath = path;
148 if ( *unprocessedPath == '/' )
149 unprocessedPath++;
151 int nRet = 0;
152 while ( !nRet && unprocessedPath && *unprocessedPath )
154 unprocessedPath = strchr( unprocessedPath, '/' );
155 if ( unprocessedPath )
156 *unprocessedPath = '\0';
158 nErr = noErr;
159 bFolder = FALSE;
160 bAliased = FALSE;
161 if ( FSPathMakeRef( (const UInt8 *)path, &aFSRef, 0 ) == noErr )
163 nErr = FSResolveAliasFileWithMountFlags( &aFSRef, TRUE, &bFolder, &bAliased, kResolveAliasFileNoUI );
164 if ( nErr == nsvErr )
166 errno = ENOENT;
167 nRet = -1;
169 else if ( nErr == noErr && bAliased )
171 char tmpPath[ PATH_MAX ];
172 if ( FSRefMakePath( &aFSRef, (UInt8 *)tmpPath, PATH_MAX ) == noErr )
174 int nLen = strlen( tmpPath ) + ( unprocessedPath ? strlen( unprocessedPath + 1 ) + 1 : 0 );
175 if ( nLen < buflen && nLen < PATH_MAX )
177 if ( unprocessedPath )
179 int nTmpPathLen = strlen( tmpPath );
180 strcat( tmpPath, "/" );
181 strcat( tmpPath, unprocessedPath + 1 );
182 strcpy( path, tmpPath);
183 unprocessedPath = path + nTmpPathLen;
185 else if ( !unprocessedPath )
187 strcpy( path, tmpPath);
190 else
192 errno = ENAMETOOLONG;
193 nRet = -1;
199 if ( unprocessedPath )
200 *unprocessedPath++ = '/';
203 return nRet;
206 #endif /* defined MACOSX */
208 #endif /* NO_PTHREAD_RTL */
210 #if defined(FREEBSD)
211 char *fcvt(double value, int ndigit, int *decpt, int *sign)
213 static char ret[256];
214 char buf[256],zahl[256],format[256]="%";
215 char *v1,*v2;
217 if (value==0.0) value=1e-30;
219 if (value<0.0) *sign=1; else *sign=0;
221 if (value<1.0)
223 *decpt=(int)log10(value);
224 value*=pow(10.0,1-*decpt);
225 ndigit+=*decpt-1;
226 if (ndigit<0) ndigit=0;
228 else
230 *decpt=(int)log10(value)+1;
233 sprintf(zahl,"%d",ndigit);
234 strcat(format,zahl);
235 strcat(format,".");
236 strcat(format,zahl);
237 strcat(format,"f");
239 sprintf(buf,format,value);
241 if (ndigit!=0)
243 v1=strtok(buf,".");
244 v2=strtok(NULL,".");
245 strcpy(ret,v1);
246 strcat(ret,v2);
248 else
250 strcpy(ret,buf);
253 return(ret);
256 #endif
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */