1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
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)
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
53 * array_of_pointer_to_addresses
55 * addr1addr2addr3...addrn
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.
72 nname
= strlen(res
->h_name
)+1;
74 naliases
= naddr_list
= naliasesdata
= 0;
76 for ( p
= res
->h_aliases
; *p
!= NULL
; p
++) {
78 naliasesdata
+= strlen(*p
)+1;
81 for ( p
= res
->h_addr_list
; *p
!= NULL
; p
++)
85 + (naliases
+1)*sizeof(char*) + naliasesdata
86 + (naddr_list
+1)*sizeof(char*) + naddr_list
*res
->h_length
89 memcpy(result
, res
, sizeof(struct hostent
));
91 strcpy(buffer
, res
->h_name
);
92 result
->h_name
= buffer
;
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
++) {
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
++) {
111 memcpy(data
, *p
, res
->h_length
);
112 data
+= res
->h_length
;
133 #endif // OSX || IOS || OPENBSD || NETBSD
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
)
146 char *unprocessedPath
= path
;
148 if ( *unprocessedPath
== '/' )
152 while ( !nRet
&& unprocessedPath
&& *unprocessedPath
)
154 unprocessedPath
= strchr( unprocessedPath
, '/' );
155 if ( unprocessedPath
)
156 *unprocessedPath
= '\0';
161 if ( FSPathMakeRef( (const UInt8
*)path
, &aFSRef
, 0 ) == noErr
)
163 nErr
= FSResolveAliasFileWithMountFlags( &aFSRef
, TRUE
, &bFolder
, &bAliased
, kResolveAliasFileNoUI
);
164 if ( nErr
== nsvErr
)
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
);
192 errno
= ENAMETOOLONG
;
199 if ( unprocessedPath
)
200 *unprocessedPath
++ = '/';
206 #endif /* defined MACOSX */
208 #endif /* NO_PTHREAD_RTL */
211 char *fcvt(double value
, int ndigit
, int *decpt
, int *sign
)
213 static char ret
[256];
214 char buf
[256],zahl
[256],format
[256]="%";
217 if (value
==0.0) value
=1e-30;
219 if (value
<0.0) *sign
=1; else *sign
=0;
223 *decpt
=(int)log10(value
);
224 value
*=pow(10.0,1-*decpt
);
226 if (ndigit
<0) ndigit
=0;
230 *decpt
=(int)log10(value
)+1;
233 sprintf(zahl
,"%d",ndigit
);
239 sprintf(buf
,format
,value
);
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */