]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/getdn.c
rename ldap_pvt_init_utils() to ldap_int_utils_init() and provide
[openldap] / libraries / libldap / getdn.c
index 70c09c7933c4c534a50a634345727a604ceb2761..6691f333e4d2953ba02c37049a406ac9774ae790 100644 (file)
@@ -1,4 +1,8 @@
 /*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1994 Regents of the University of Michigan.
  *  All rights reserved.
  *
@@ -7,10 +11,6 @@
 
 #include "portable.h"
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -21,7 +21,7 @@ static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of
 
 #include "ldap-int.h"
 
-static char **explode_name( char *name, int notypes, int is_dn );
+static char **explode_name( LDAP_CONST char *name, int notypes, int is_dn );
 
 char *
 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
@@ -46,7 +46,7 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
 }
 
 char *
-ldap_dn2ufn( char *dn )
+ldap_dn2ufn( LDAP_CONST char *dn )
 {
        char    *p, *ufn, *r;
        int     state;
@@ -54,7 +54,7 @@ ldap_dn2ufn( char *dn )
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
 
        if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
-               return( strdup( dn ));
+               return( strdup( dn ) );
 
        ufn = strdup( ++p );
 
@@ -92,8 +92,8 @@ ldap_dn2ufn( char *dn )
                                char    *rsave = r;
 
                                *r-- = '\0';
-                               while ( !isspace( *r ) && *r != ';'
-                                   && *r != ',' && r > ufn )
+                               while ( !isspace( (unsigned char) *r )
+                                       && *r != ';' && *r != ',' && r > ufn )
                                        r--;
                                r++;
 
@@ -119,35 +119,51 @@ ldap_dn2ufn( char *dn )
 }
 
 char **
-ldap_explode_dns( char *dn )
+ldap_explode_dns( LDAP_CONST char *dn_in )
 {
-       int     ncomps, maxcomps;
        char    *s;
        char    **rdns;
+       char    *tok_r;
+       char    *dn;
 
-       if ( (rdns = (char **) malloc( 8 * sizeof(char *) )) == NULL ) {
+       int ncomps;
+       int maxcomps = 8;
+
+       if ( (dn = strdup( dn_in )) == NULL ) {
+               return( NULL );
+       }
+
+       if ( (rdns = (char **) malloc( maxcomps * sizeof(char *) )) == NULL ) {
+               free( dn );
                return( NULL );
        }
 
-       maxcomps = 8;
        ncomps = 0;
-       for ( s = strtok( dn, "@." ); s != NULL; s = strtok( NULL, "@." ) ) {
+       for ( s = ldap_pvt_strtok( dn, "@.", &tok_r ); s != NULL; 
+             s = ldap_pvt_strtok( NULL, "@.", &tok_r ) )
+       {
                if ( ncomps == maxcomps ) {
                        maxcomps *= 2;
                        if ( (rdns = (char **) realloc( rdns, maxcomps *
-                           sizeof(char *) )) == NULL ) {
-                               return( NULL );
+                           sizeof(char *) )) == NULL )
+                       {
+                               free( dn );
+                               return NULL;
                        }
                }
                rdns[ncomps++] = strdup( s );
        }
+       free(dn);
+
        rdns[ncomps] = NULL;
 
+       /* trim rdns */
+       rdns = (char **) realloc( rdns, (ncomps+1) * sizeof(char*) );
        return( rdns );
 }
 
 char **
-ldap_explode_dn( char *dn, int notypes )
+ldap_explode_dn( LDAP_CONST char *dn, int notypes )
 {
        Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
 
@@ -158,16 +174,17 @@ ldap_explode_dn( char *dn, int notypes )
 }
 
 char **
-ldap_explode_rdn( char *rdn, int notypes )
+ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
 {
        Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
        return explode_name( rdn, notypes, 0 );
 }
 
 static char **
-explode_name( char *name, int notypes, int is_dn )
+explode_name( LDAP_CONST char *name, int notypes, int is_dn )
 {
-       char    *p, *q, **parts = NULL;
+       const char *p, *q;
+       char **parts = NULL;
        int     state, count = 0, endquote, len;
 
        p = name-1;
@@ -259,7 +276,7 @@ explode_name( char *name, int notypes, int is_dn )
 
 
 int
-ldap_is_dns_dn( char *dn )
+ldap_is_dns_dn( LDAP_CONST char *dn )
 {
        return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
            strchr( dn, ',' ) == NULL );