]> git.sur5r.net Git - openldap/blobdiff - libraries/libavl/avl.c
Fix up SASL interact error checking
[openldap] / libraries / libavl / avl.c
index f90cd3366abfad3fa644889ac27eabaf269a822b..63a543f8e4fb82f048d2387a887031dfa8119c18 100644 (file)
@@ -1,4 +1,9 @@
 /* avl.c - routines to implement an avl tree */
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
 /*
  * Copyright (c) 1993 Regents of the University of Michigan.
  * All rights reserved.
@@ -11,7 +16,6 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #ifndef lint
@@ -20,15 +24,15 @@ static char avl_version[] = "AVL library version 1.0\n";
 #endif
 
 #include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
+#include <ac/stdlib.h>
 
+#define AVL_INTERNAL
 #include "avl.h"
 
 #define ROTATERIGHT(x) { \
        Avlnode *tmp;\
        if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
-               (void) printf("RR error\n"); exit(1); \
+               (void) fputs("RR error\n", stderr); exit( EXIT_FAILURE ); \
        }\
        tmp = (*(x))->avl_left;\
        (*(x))->avl_left = tmp->avl_right;\
@@ -38,11 +42,11 @@ static char avl_version[] = "AVL library version 1.0\n";
 #define ROTATELEFT(x)  { \
        Avlnode *tmp;\
        if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
-               (void) printf("RL error\n"); exit(1); \
+               (void) fputs("RL error\n", stderr); exit( EXIT_FAILURE ); \
        }\
        tmp = (*(x))->avl_right;\
        (*(x))->avl_right = tmp->avl_left;\
-       tmp->avl_left = *x;\
+       tmp->avl_left = *(x);\
        *(x) = tmp;\
 }
 
@@ -51,14 +55,15 @@ static char avl_version[] = "AVL library version 1.0\n";
  * and balance of an avl tree.
  */
 
-static
-int ravl_insert( iroot, data, taller, fcmp, fdup, depth )
-    Avlnode    **iroot;
-    caddr_t    data;
-    int                *taller;
-    IFP                fcmp;           /* comparison function */
-    IFP                fdup;           /* function to call for duplicates */
-    int                depth;
+static int
+ravl_insert(
+    Avlnode    **iroot,
+    void*      data,
+    int                *taller,
+    AVL_CMP            fcmp,                   /* comparison function */
+    AVL_DUP            fdup,                   /* function to call for duplicates */
+    int                depth
+)
 {
        int     rc, cmp, tallersub;
        Avlnode *l, *r;
@@ -205,11 +210,8 @@ int ravl_insert( iroot, data, taller, fcmp, fdup, depth )
  * NOTE: this routine may malloc memory
  */
 
-int avl_insert( root, data, fcmp, fdup )
-    Avlnode    **root;
-    caddr_t    data;
-    IFP                fcmp;
-    IFP                fdup;
+int
+avl_insert( Avlnode **root, void* data, AVL_CMP fcmp, AVL_DUP fdup )
 {
        int     taller;
 
@@ -222,8 +224,7 @@ int avl_insert( root, data, fcmp, fdup )
  */
 
 static int
-right_balance( root )
-    Avlnode    **root;
+right_balance( Avlnode **root )
 {
        int     shorter = -1;
        Avlnode *r, *l;
@@ -286,9 +287,8 @@ right_balance( root )
  * been shortened because of a deletion.
  */
 
-static
-int left_balance( root )
-    Avlnode    **root;
+static int
+left_balance( Avlnode **root )
 {
        int     shorter = -1;
        Avlnode *r, *l;
@@ -353,16 +353,12 @@ int left_balance( root )
  * rebalancing.
  */
 
-static caddr_t
-ravl_delete( root, data, fcmp, shorter )
-    Avlnode    **root;
-    caddr_t    data;
-    IFP                fcmp;
-    int                *shorter;
+static void*
+ravl_delete( Avlnode **root, void* data, AVL_CMP fcmp, int *shorter )
 {
        int     shortersubtree = 0;
        int     cmp;
-       caddr_t savedata;
+       void*   savedata;
        Avlnode *minnode, *savenode;
 
        if ( *root == NULLAVL )
@@ -446,23 +442,16 @@ ravl_delete( root, data, fcmp, shorter )
  * the avl tree rooted at root.
  */
 
-caddr_t
-avl_delete( root, data, fcmp )
-    Avlnode    **root;
-    caddr_t    data;
-    IFP                fcmp;
+void*
+avl_delete( Avlnode **root, void* data, AVL_CMP fcmp )
 {
        int     shorter;
 
        return( ravl_delete( root, data, fcmp, &shorter ) );
 }
 
-static
-int avl_inapply( root, fn, arg, stopflag )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
+static int
+avl_inapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
 {
        if ( root == 0 )
                return( AVL_NOMORE );
@@ -481,12 +470,8 @@ int avl_inapply( root, fn, arg, stopflag )
                return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
 }
 
-static
-int avl_postapply( root, fn, arg, stopflag )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
+static int
+avl_postapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
 {
        if ( root == 0 )
                return( AVL_NOMORE );
@@ -504,12 +489,8 @@ int avl_postapply( root, fn, arg, stopflag )
        return( (*fn)( root->avl_data, arg ) );
 }
 
-static
-int avl_preapply( root, fn, arg, stopflag )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
+static int
+avl_preapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
 {
        if ( root == 0 )
                return( AVL_NOMORE );
@@ -536,12 +517,8 @@ int avl_preapply( root, fn, arg, stopflag )
  * of nodes.
  */
 
-int avl_apply( root, fn, arg, stopflag, type )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
-    int                type;
+int
+avl_apply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag, int type )
 {
        switch ( type ) {
        case AVL_INORDER:
@@ -569,21 +546,23 @@ int avl_apply( root, fn, arg, stopflag, type )
  * AVL_NOMORE is returned.
  */
 
-int avl_prefixapply( root, data, fmatch, marg, fcmp, carg, stopflag )
-    Avlnode    *root;
-    caddr_t    data;
-    IFP                fmatch;
-    caddr_t    marg;
-    IFP                fcmp;
-    caddr_t    carg;
-    int                stopflag;
+int
+avl_prefixapply(
+    Avlnode    *root,
+    void*      data,
+    AVL_CMP            fmatch,
+    void*      marg,
+    AVL_CMP            fcmp,
+    void*      carg,
+    int                stopflag
+)
 {
        int     cmp;
 
        if ( root == 0 )
                return( AVL_NOMORE );
 
-       cmp = (*fcmp)( data, root->avl_data, carg );
+       cmp = (*fcmp)( data, root->avl_data /* , carg */);
        if ( cmp == 0 ) {
                if ( (*fmatch)( root->avl_data, marg ) == stopflag )
                        return( stopflag );
@@ -618,9 +597,8 @@ int avl_prefixapply( root, data, fmatch, marg, fcmp, carg, stopflag )
  * number of items actually freed is returned.
  */
 
-int avl_free( root, dfree )
-    Avlnode    *root;
-    IFP                dfree;
+int
+avl_free( Avlnode *root, AVL_FREE dfree )
 {
        int     nleft, nright;
 
@@ -636,6 +614,7 @@ int avl_free( root, dfree )
 
        if ( dfree )
                (*dfree)( root->avl_data );
+       free( root );
 
        return( nleft + nright + 1 );
 }
@@ -647,11 +626,8 @@ int avl_free( root, dfree )
  * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
  */
 
-caddr_t
-avl_find( root, data, fcmp )
-    Avlnode    *root;
-    caddr_t    data;
-    IFP        fcmp;
+void*
+avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
        int     cmp;
 
@@ -672,13 +648,10 @@ avl_find( root, data, fcmp )
  * they match, non-zero otherwise.
  */
 
-caddr_t
-avl_find_lin( root, data, fcmp )
-    Avlnode    *root;
-    caddr_t    data;
-    IFP                fcmp;
+void*
+avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
-       caddr_t res;
+       void*   res;
 
        if ( root == 0 )
                return( NULL );
@@ -697,28 +670,28 @@ avl_find_lin( root, data, fcmp )
                return( avl_find_lin( root->avl_right, data, fcmp ) );
 }
 
-static caddr_t *avl_list;
+/* NON-REENTRANT INTERFACE */
+
+static void*   *avl_list;
 static int     avl_maxlist;
 static int     avl_nextlist;
 
 #define AVL_GRABSIZE   100
 
 /* ARGSUSED */
-static
-int avl_buildlist( data, arg )
-    caddr_t    data;
-    int        arg;
+static int
+avl_buildlist( void* data, void* arg )
 {
        static int      slots;
 
-       if ( avl_list == (caddr_t *) 0 ) {
-               avl_list = (caddr_t *) malloc(AVL_GRABSIZE * sizeof(caddr_t));
+       if ( avl_list == (void* *) 0 ) {
+               avl_list = (void* *) malloc(AVL_GRABSIZE * sizeof(void*));
                slots = AVL_GRABSIZE;
                avl_maxlist = 0;
        } else if ( avl_maxlist == slots ) {
                slots += AVL_GRABSIZE;
-               avl_list = (caddr_t *) realloc( (char *) avl_list,
-                   (unsigned) slots * sizeof(caddr_t));
+               avl_list = (void* *) realloc( (char *) avl_list,
+                   (unsigned) slots * sizeof(void*));
        }
 
        avl_list[ avl_maxlist++ ] = data;
@@ -738,13 +711,12 @@ int avl_buildlist( data, arg )
  * different trees) cannot be active at once.
  */
 
-caddr_t
-avl_getfirst( root )
-    Avlnode    *root;
+void*
+avl_getfirst( Avlnode *root )
 {
        if ( avl_list ) {
                free( (char *) avl_list);
-               avl_list = (caddr_t *) 0;
+               avl_list = (void* *) 0;
        }
        avl_maxlist = 0;
        avl_nextlist = 0;
@@ -752,32 +724,37 @@ avl_getfirst( root )
        if ( root == 0 )
                return( 0 );
 
-       (void) avl_apply( root, avl_buildlist, (caddr_t) 0, -1, AVL_INORDER );
+       (void) avl_apply( root, avl_buildlist, (void*) 0, -1, AVL_INORDER );
 
        return( avl_list[ avl_nextlist++ ] );
 }
 
-caddr_t
-avl_getnext()
+void*
+avl_getnext( void )
 {
        if ( avl_list == 0 )
                return( 0 );
 
        if ( avl_nextlist == avl_maxlist ) {
-               free( (caddr_t) avl_list);
-               avl_list = (caddr_t *) 0;
+               free( (void*) avl_list);
+               avl_list = (void* *) 0;
                return( 0 );
        }
 
        return( avl_list[ avl_nextlist++ ] );
 }
 
-int avl_dup_error()
+/* end non-reentrant code */
+
+
+int
+avl_dup_error( void* left, void* right )
 {
        return( -1 );
 }
 
-int avl_dup_ok()
+int
+avl_dup_ok( void* left, void* right )
 {
        return( 0 );
 }