On OpenBSD 4.0, the tdelete() function has an incorrect return value when removing the last element from a tree. This works around it.
2008-01-09 Bruno Haible <[EMAIL PROTECTED]> Work around OpenBSD 4.0 tdelete() bug. * m4/tsearch.m4 (gl_FUNC_TSEARCH): Also check tdelete's return value. * lib/search.in.h: If REPLACE_TSEARCH is 1, define tsearch etc. as macros and don't redefine the enum values. * m4/search_h.m4 (gl_SEARCH_H_DEFAULTS): Initialize REPLACE_TSEARCH. * modules/search (Makefile.am): Also substitute REPLACE_TSEARCH. * doc/functions/tdelete.texi: Document the OpenBSD 4.0 bug. *** doc/functions/tdelete.texi.orig 2008-01-10 02:38:09.000000000 +0100 --- doc/functions/tdelete.texi 2008-01-10 02:25:32.000000000 +0100 *************** *** 11,16 **** --- 11,20 ---- @item This function is missing on some platforms: mingw, BeOS. + @item + @code{tdelete} returns @code{NULL} when removing the last element of a tree + on some platforms: + OpenBSD 4.0. @end itemize Portability problems not fixed by Gnulib: *** lib/search.in.h.orig 2008-01-10 02:38:09.000000000 +0100 --- lib/search.in.h 2008-01-10 02:25:32.000000000 +0100 *************** *** 1,6 **** /* A GNU-like <search.h>. ! Copyright (C) 2007 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by --- 1,6 ---- /* A GNU-like <search.h>. ! Copyright (C) 2007-2008 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by *************** *** 35,46 **** #if @GNULIB_TSEARCH@ ! # if [EMAIL PROTECTED]@ /* See <http://www.opengroup.org/susv3xbd/search.h.html>, <http://www.opengroup.org/susv3xsh/tsearch.html> for details. */ typedef enum { preorder, --- 35,53 ---- #if @GNULIB_TSEARCH@ ! # if @REPLACE_TSEARCH@ ! # define tsearch rpl_tsearch ! # define tfind rpl_tfind ! # define tdelete rpl_tdelete ! # define twalk rpl_twalk ! # endif ! # if [EMAIL PROTECTED]@ || @REPLACE_TSEARCH@ /* See <http://www.opengroup.org/susv3xbd/search.h.html>, <http://www.opengroup.org/susv3xsh/tsearch.html> for details. */ + # if [EMAIL PROTECTED]@ typedef enum { preorder, *************** *** 49,54 **** --- 56,62 ---- leaf } VISIT; + # endif /* Searches an element in the tree *VROOTP that compares equal to KEY. If one is found, it is returned. Otherwise, a new element equal to KEY *** m4/search_h.m4.orig 2008-01-10 02:38:09.000000000 +0100 --- m4/search_h.m4 2008-01-10 02:25:32.000000000 +0100 *************** *** 1,5 **** ! # search_h.m4 serial 2 ! dnl Copyright (C) 2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. --- 1,5 ---- ! # search_h.m4 serial 3 ! dnl Copyright (C) 2007-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. *************** *** 27,31 **** [ GNULIB_TSEARCH=0; AC_SUBST([GNULIB_TSEARCH]) dnl Assume proper GNU behavior unless another module says otherwise. ! HAVE_TSEARCH=1; AC_SUBST([HAVE_TSEARCH]) ]) --- 27,32 ---- [ GNULIB_TSEARCH=0; AC_SUBST([GNULIB_TSEARCH]) dnl Assume proper GNU behavior unless another module says otherwise. ! HAVE_TSEARCH=1; AC_SUBST([HAVE_TSEARCH]) ! REPLACE_TSEARCH=0; AC_SUBST([REPLACE_TSEARCH]) ]) *** m4/tsearch.m4.orig 2008-01-10 02:38:09.000000000 +0100 --- m4/tsearch.m4 2008-01-10 02:25:32.000000000 +0100 *************** *** 1,5 **** ! # tsearch.m4 serial 2 ! dnl Copyright (C) 2006-2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. --- 1,5 ---- ! # tsearch.m4 serial 3 ! dnl Copyright (C) 2006-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. *************** *** 8,14 **** [ AC_REQUIRE([gl_SEARCH_H_DEFAULTS]) AC_CHECK_FUNCS([tsearch]) ! if test $ac_cv_func_tsearch = no; then HAVE_TSEARCH=0 AC_LIBOBJ([tsearch]) gl_PREREQ_TSEARCH --- 8,52 ---- [ AC_REQUIRE([gl_SEARCH_H_DEFAULTS]) AC_CHECK_FUNCS([tsearch]) ! if test $ac_cv_func_tsearch = yes; then ! dnl On OpenBSD 4.0, the return value of tdelete() is incorrect. ! AC_REQUIRE([AC_PROG_CC]) ! AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles ! AC_CACHE_CHECK([whether tdelete works], [gl_cv_func_tdelete_works], ! [ ! AC_TRY_RUN([ ! #include <stddef.h> ! #include <search.h> ! static int ! cmp_fn (const void *a, const void *b) ! { ! return *(const int *) a - *(const int *) b; ! } ! int ! main () ! { ! int x = 0; ! void *root = NULL; ! if (!(tfind (&x, &root, cmp_fn) == NULL)) return 1; ! tsearch (&x, &root, cmp_fn); ! if (!(tfind (&x, &root, cmp_fn) != NULL)) return 1; ! if (!(tdelete (&x, &root, cmp_fn) != NULL)) return 1; ! return 0; ! }], [gl_cv_func_tdelete_works=yes], [gl_cv_func_tdelete_works=no], ! [case "$host_os" in ! openbsd*) gl_cv_func_tdelete_works="guessing no";; ! *) gl_cv_func_tdelete_works="guessing yes";; ! esac ! ]) ! ]) ! case "$gl_cv_func_tdelete_works" in ! *no) ! REPLACE_TSEARCH=1 ! AC_LIBOBJ([tsearch]) ! gl_PREREQ_TSEARCH ! ;; ! esac ! else HAVE_TSEARCH=0 AC_LIBOBJ([tsearch]) gl_PREREQ_TSEARCH *** modules/search.orig 2008-01-10 02:38:09.000000000 +0100 --- modules/search 2008-01-10 02:25:32.000000000 +0100 *************** *** 25,30 **** --- 25,31 ---- -e 's|@''NEXT_SEARCH_H''@|$(NEXT_SEARCH_H)|g' \ -e 's|@''GNULIB_TSEARCH''@|$(GNULIB_TSEARCH)|g' \ -e 's|@''HAVE_TSEARCH''@|$(HAVE_TSEARCH)|g' \ + -e 's|@''REPLACE_TSEARCH''@|$(REPLACE_TSEARCH)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ < $(srcdir)/search.in.h; \ } > [EMAIL PROTECTED]