]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/util-int.c
Improved ldap_int_strtok. If strtok_r does not exists, it will be worked
[openldap] / libraries / libldap / util-int.c
index 3ace0c799b5bc6956339193bae48d4ccc3192f07..2eb4a8c23ad40b7e7f03a1227151b6bc166f2e74 100644 (file)
 
 #include "ldap-int.h"
 
+static int int_strspn( const char *str, const char *delim )
+{
+#if defined( HAVE_STRSPN )
+       return strspn( str, delim );
+#else
+       int pos;
+       const char *p=delim;
+       for( pos=0; (*str) ; pos++,str++) {
+               if (*str!=*p)
+                       for( p=delim; (*p) ; p++ ) {
+                               if (*str==*p)
+                                       break;
+                       }
+               if (*p=='\0')
+                       return pos;
+       }
+       return pos;
+#endif 
+}
+
+static char *int_strpbrk( const char *str, const char *accept )
+{
+#if defined( HAVE_STRPBRK )
+       return strpbrk( str, accept );
+#else
+       const char *p;
+       for( ; (*str) ; str++ ) {
+               for( p=accept; (*p) ; p++) {
+                       if (*str==*p)
+                               return str;
+               }
+       }
+       return NULL;
+#endif
+}
+
 char *ldap_int_strtok( char *str, const char *delim, char **pos )
 {
 #ifdef HAVE_STRTOK_R
        return strtok_r(str, delim, pos);
 #else
-       return strtok(str, delim);
+       char *p;
+
+       if (pos==NULL)
+               return NULL;
+       if (str==NULL) {
+               if (*pos==NULL)
+                       return NULL;
+               str=*pos;
+       }
+       /* skip any initial delimiters */
+       str += int_strspn( str, delim );
+       if (*str == '\0')
+               return NULL;
+       p = int_strpbrk( str, delim );
+       if (p==NULL) {
+               *pos = NULL;
+       } else {
+               *p ='\0';
+               *pos = p+1;
+       }
+       return str;
 #endif
 }
 
 char *ldap_int_ctime( const time_t *tp, char *buf )
 {
-#ifdef HAVE_CTIME_R
-# if defined( ARGS_CTIME_R_2 )
-       return ctime_r(tp,buf);
-# elif defined( ARGS_CTIME_R_3 )
+#if defined( HAVE_CTIME_R ) && defined( CTIME_R_NARGS )
+# if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
+       choke me!  nargs should have 2 or 3
+# elif CTIME_R_NARGS > 2
        return ctime_r(tp,buf,26);
 # else
-       Do not know how many arguments ctime_r takes, so generating error
+       return ctime_r(tp,buf);
 # endif          
 #else
-       return ctime(tp);
+       memcpy( buf, ctime(tp), 26 );
+       return buf;
 #endif 
 }
 
@@ -66,7 +123,7 @@ static char *safe_realloc( char **buf, int len )
        } 
        return tmpbuf;
 }
+
 int ldap_int_gethostbyname_a(
        const char *name, 
        struct hostent *resbuf,
@@ -75,28 +132,27 @@ int ldap_int_gethostbyname_a(
        int *herrno_ptr )
 {
 #ifdef HAVE_GETHOSTBYNAME_R
-       int r;
+       int r=-1;
        int buflen=BUFSTART;
-
-       if (safe_realloc( buf, buflen)) {
-               for(;buflen<BUFMAX;) {
-                       r = gethostbyname_r( name, resbuf, *buf,
-                               buflen, result, herrno_ptr );
+       *buf = NULL;
+       for(;buflen<BUFMAX;) {
+               if (safe_realloc( buf, buflen )==NULL)
+                       return r;
+               r = gethostbyname_r( name, resbuf, *buf,
+                       buflen, result, herrno_ptr );
 #ifdef NETDB_INTERNAL
-                       if ((r<0) &&
-                               (*herrno_ptr==NETDB_INTERNAL) &&
-                               (errno==ERANGE))
-                       {
-                               if (safe_realloc( buf, buflen*=2 )) {
-                                               continue;
-                               }
-                       }
+               if ((r<0) &&
+                       (*herrno_ptr==NETDB_INTERNAL) &&
+                       (errno==ERANGE))
+               {
+                       buflen*=2;
+                       continue;
+               }
 #endif
-                       return r;
-               }
+               return r;
        }
-
-#else /* gethostbyname() */
+       return -1;
+#else  
        *result = gethostbyname( name );
 
        if (*result!=NULL) {
@@ -104,9 +160,9 @@ int ldap_int_gethostbyname_a(
        }
 
        *herrno_ptr = h_errno;
-#endif 
-
+       
        return -1;
+#endif 
 }
         
 int ldap_int_gethostbyaddr_a(
@@ -119,33 +175,33 @@ int ldap_int_gethostbyaddr_a(
        int *herrno_ptr )
 {
 #ifdef HAVE_GETHOSTBYADDR_R
-       int r;
+       int r=-1;
        int buflen=BUFSTART;
-       if (safe_realloc( buf, buflen)) {
-               for(;buflen<BUFMAX;) {
-                       r = gethostbyaddr_r( addr, len, type,
-                               resbuf, *buf, buflen, 
-                               result, herrno_ptr );
-#ifdef NETDB_INTERNAL
-                       if ((r<0) &&
-                               (*herrno_ptr==NETDB_INTERNAL) &&
-                               (errno==ERANGE))
-                       {
-                               if (safe_realloc( buf, buflen*=2))
-                                       continue;
-                       }
-#endif
+       *buf = NULL;   
+       for(;buflen<BUFMAX;) {
+               if (safe_realloc( buf, buflen )==NULL)
                        return r;
+               r = gethostbyaddr_r( addr, len, type,
+                       resbuf, *buf, buflen, 
+                       result, herrno_ptr );
+#ifdef NETDB_INTERNAL
+               if ((r<0) &&
+                       (*herrno_ptr==NETDB_INTERNAL) &&
+                       (errno==ERANGE))
+               {
+                       buflen*=2;
+                       continue;
                }
+#endif
+               return r;
        }
-
+       return -1;
 #else /* gethostbyaddr() */
        *result = gethostbyaddr( addr, len, type );
 
        if (*result!=NULL) {
                return 0;
        }
-#endif 
-
        return -1;
+#endif 
 }