]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
Add simple copyright notice.
[openldap] / libraries / libldap / util-int.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * util-int.c   Various functions to replace missing threadsafe ones.
7  *                                Without the real *_r funcs, things will work, but won't be
8  *                                threadsafe. 
9  * 
10  * Written by Bart Hartgers.
11  *
12  * Copyright 1998, A. Hartgers, All rights reserved.
13  * This software is not subject to any license of Eindhoven University of
14  * Technology, since it was written in my spare time.
15  *                      
16  * Redistribution and use in source and binary forms are permitted only
17  * as authorized by the OpenLDAP Public License.  A copy of this
18  * license is available at http://www.OpenLDAP.org/license.html or
19  * in file LICENSE in the top-level directory of the distribution.
20  */ 
21
22 #include "portable.h"
23
24 #include <stdlib.h>
25
26 #include <ac/errno.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "ldap-int.h"
32
33 char *ldap_int_strtok( char *str, const char *delim, char **pos )
34 {
35 #ifdef HAVE_STRTOK_R
36         return strtok_r(str, delim, pos);
37 #else
38         return strtok(str, delim);
39 #endif
40 }
41
42 char *ldap_int_ctime( const time_t *tp, char *buf )
43 {
44 #ifdef HAVE_CTIME_R
45         return ctime_r(tp,buf);
46 #else
47         return ctime(tp);
48 #endif  
49 }
50
51 #define BUFSTART 1024
52 #define BUFMAX (32*1024)
53
54 static char *safe_realloc( char **buf, int len )
55 {
56         char *tmpbuf;
57         tmpbuf = realloc( *buf, len );
58         if (tmpbuf) {
59                 *buf=tmpbuf;
60         } 
61         return tmpbuf;
62 }
63  
64 int ldap_int_gethostbyname_a(
65         const char *name, 
66         struct hostent *resbuf,
67         char **buf,
68         struct hostent **result,
69         int *herrno_ptr )
70 {
71 #ifdef HAVE_GETHOSTBYNAME_R
72         int r;
73         int buflen=BUFSTART;
74
75         if (safe_realloc( buf, buflen)) {
76                 for(;buflen<BUFMAX;) {
77                         r = gethostbyname_r( name, resbuf, *buf,
78                                 buflen, result, herrno_ptr );
79 #ifdef NETDB_INTERNAL
80                         if ((r<0) &&
81                                 (*herrno_ptr==NETDB_INTERNAL) &&
82                                 (errno==ERANGE))
83                         {
84                                 if (safe_realloc( buf, buflen*=2 )) {
85                                                 continue;
86                                 }
87                         }
88 #endif
89                         return r;
90                 }
91         }
92
93 #else /* gethostbyname() */
94         *result = gethostbyname( name );
95
96         if (*result!=NULL) {
97                 return 0;
98         }
99
100         *herrno_ptr = h_errno;
101 #endif  
102
103         return -1;
104 }
105          
106 int ldap_int_gethostbyaddr_a(
107         const char *addr,
108         int len,
109         int type,
110         struct hostent *resbuf,
111         char **buf,
112         struct hostent **result,
113         int *herrno_ptr )
114 {
115 #ifdef HAVE_GETHOSTBYADDR_R
116         int r;
117         int buflen=BUFSTART;
118         if (safe_realloc( buf, buflen)) {
119                 for(;buflen<BUFMAX;) {
120                         r = gethostbyaddr_r( addr, len, type,
121                                 resbuf, *buf, buflen, 
122                                 result, herrno_ptr );
123 #ifdef NETDB_INTERNAL
124                         if ((r<0) &&
125                                 (*herrno_ptr==NETDB_INTERNAL) &&
126                                 (errno==ERANGE))
127                         {
128                                 if (safe_realloc( buf, buflen*=2))
129                                         continue;
130                         }
131 #endif
132                         return r;
133                 }
134         }
135
136 #else /* gethostbyaddr() */
137         *result = gethostbyaddr( addr, len, type );
138
139         if (*result!=NULL) {
140                 return 0;
141         }
142 #endif  
143
144         return -1;
145 }