]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
Update ctime_r() argument macro to cache results and to produce
[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 #if defined( HAVE_CTIME_R ) && defined( CTIME_R_NARGS )
45 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
46         choke me!  nargs should have 2 or 3
47 # elif CTIME_R_NARGS > 2
48         return ctime_r(tp,buf,26);
49 # else
50         return ctime_r(tp,buf);
51 # endif   
52 #else
53         return ctime(tp);
54 #endif  
55 }
56
57 #define BUFSTART 1024
58 #define BUFMAX (32*1024)
59
60 static char *safe_realloc( char **buf, int len )
61 {
62         char *tmpbuf;
63         tmpbuf = realloc( *buf, len );
64         if (tmpbuf) {
65                 *buf=tmpbuf;
66         } 
67         return tmpbuf;
68 }
69  
70 int ldap_int_gethostbyname_a(
71         const char *name, 
72         struct hostent *resbuf,
73         char **buf,
74         struct hostent **result,
75         int *herrno_ptr )
76 {
77 #ifdef HAVE_GETHOSTBYNAME_R
78         int r;
79         int buflen=BUFSTART;
80
81         if (safe_realloc( buf, buflen)) {
82                 for(;buflen<BUFMAX;) {
83                         r = gethostbyname_r( name, resbuf, *buf,
84                                 buflen, result, herrno_ptr );
85 #ifdef NETDB_INTERNAL
86                         if ((r<0) &&
87                                 (*herrno_ptr==NETDB_INTERNAL) &&
88                                 (errno==ERANGE))
89                         {
90                                 if (safe_realloc( buf, buflen*=2 )) {
91                                                 continue;
92                                 }
93                         }
94 #endif
95                         return r;
96                 }
97         }
98
99 #else /* gethostbyname() */
100         *result = gethostbyname( name );
101
102         if (*result!=NULL) {
103                 return 0;
104         }
105
106         *herrno_ptr = h_errno;
107 #endif  
108
109         return -1;
110 }
111          
112 int ldap_int_gethostbyaddr_a(
113         const char *addr,
114         int len,
115         int type,
116         struct hostent *resbuf,
117         char **buf,
118         struct hostent **result,
119         int *herrno_ptr )
120 {
121 #ifdef HAVE_GETHOSTBYADDR_R
122         int r;
123         int buflen=BUFSTART;
124         if (safe_realloc( buf, buflen)) {
125                 for(;buflen<BUFMAX;) {
126                         r = gethostbyaddr_r( addr, len, type,
127                                 resbuf, *buf, buflen, 
128                                 result, herrno_ptr );
129 #ifdef NETDB_INTERNAL
130                         if ((r<0) &&
131                                 (*herrno_ptr==NETDB_INTERNAL) &&
132                                 (errno==ERANGE))
133                         {
134                                 if (safe_realloc( buf, buflen*=2))
135                                         continue;
136                         }
137 #endif
138                         return r;
139                 }
140         }
141
142 #else /* gethostbyaddr() */
143         *result = gethostbyaddr( addr, len, type );
144
145         if (*result!=NULL) {
146                 return 0;
147         }
148 #endif  
149
150         return -1;
151 }