delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2005/01/07/13:38:35

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
From: <ams AT ludd DOT ltu DOT se>
Message-Id: <200501071838.j07IcEL7029283@speedy.ludd.ltu.se>
Subject: Re: lsearch() and lfind() patch
In-Reply-To: <01c4f29f$Blat.v2.2.2$722ead40@zahav.net.il> "from Eli Zaretskii
at Jan 4, 2005 10:52:27 pm"
To: djgpp-workers AT delorie DOT com
Date: Fri, 7 Jan 2005 19:38:14 +0100 (CET)
X-Mailer: ELM [version 2.4ME+ PL78 (25)]
MIME-Version: 1.0
X-ltu-MailScanner-Information: Please contact the ISP for more information
X-ltu-MailScanner: Found to be clean
X-MailScanner-From: ams AT ludd DOT ltu DOT se
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

According to Eli Zaretskii:
> > +identified by @var{key} using the comparision function @var{compar}.  
>                                      ^^^^^^^^^^^
> A typo.

Thanks.

Meanwhile I improved the documentation and hacked away at my first
test program until I got it working so now we have two test programs
(albeit very similar). So here's a new patch. I only post the diffs
that have changed.


Right,

						MartinS

Index: djgpp/src/libc/posix/search/lfind.txh
===================================================================
RCS file: djgpp/src/libc/posix/search/lfind.txh
diff -N djgpp/src/libc/posix/search/lfind.txh
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/src/libc/posix/search/lfind.txh	7 Jan 2005 18:15:56 -0000
@@ -0,0 +1,151 @@
+@ignore
+ * File lfind.txh.
+ *
+ * Copyright (C) 2005 Martin Str@"omberg <ams AT ludd DOT luth DOT se>.
+ *
+ * This software may be used freely so long as this copyright notice is
+ * left intact. There is no warranty on this software.
+ *
+@end ignore
+
+@node lfind, misc
+@findex lfind
+@subheading Syntax
+
+@example
+#include <search.h>
+
+void * lfind(const void *key, void *base, size_t *nelp, size_t width, 
+             int (*compar)(const void *, const void *));  
+@end example
+
+@subheading Description
+
+This function searches the array @var{base} of elements of size
+@var{width}, currently containing @var{nelp} elements, for the element
+identified by @var{key} using the comparison function @var{compar}.  
+
+The function @var{compar} should return 0 when its inputs are equal
+and not 0 otherwise.  
+
+See also @code{lsearch} (@pxref{lsearch}).  
+
+@subheading Return Value
+
+A pointer to the found element or @code{NULL} if not present.  
+
+@subheading Portability
+
+@portability !ansi, posix
+
+@subheading Example
+
+@example
+#include <search.h>
+#include <stdio.h>
+#include <string.h>
+
+#define N_ELEM       50
+#define ELEM_SIZE    50
+#define COMPARE_FUN  (int (*)(const void *, const void *)) strcmp
+
+char arr[N_ELEM][ELEM_SIZE];
+size_t n_arr = 0;
+
+int main(void)
+@{
+  char *entry;
+
+  entry = lfind("Anyone there?", arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( entry )
+  @{
+    printf("Someone there: %s!\n", entry);
+  @}
+  else
+  @{
+    printf("Noone there...\n");
+  @}
+
+  return 0;
+@}
+
+@end example
+
+@c ----------------------------------------------------------------------
+@node lsearch, misc
+@findex lsearch
+@subheading Syntax
+
+@example
+#include <search.h>
+
+void * lsearch(const void *key, void *base, size_t *nelp, size_t width, 
+               int (*compar)(const void *, const void *));
+@end example
+
+@subheading Description
+
+This function searches the array @var{base} of elements of size
+@var{width}, currently containing @var{nelp} elements, for the element
+identified by @var{key} using the comparison function @var{compar}.  
+
+If the @var{key} isn't found it is added to the array @var{base} and
+@var{*nelp} is incremented.  
+
+The function @var{compar} should return 0 when its inputs are equal
+and not 0 otherwise.  
+
+It is the caller's responsibility that the array has enough room for
+an additional element.  
+
+See also @code{lfind} (@pxref{lfind}).  
+
+@subheading Return Value
+
+A pointer to the found or inserted element.  
+
+@subheading Portability
+
+@portability !ansi, posix
+
+@subheading Example
+
+@example
+#include <search.h>
+#include <stdio.h>
+#include <string.h>
+
+#define N_ELEM       50
+#define ELEM_SIZE    sizeof(char *)
+#define COMPARE_FUN  my_p_strcmp
+
+char *arr[N_ELEM];
+size_t n_arr = 0;
+
+
+int my_p_strcmp( const void *el1, const void *el2)
+@{
+  return strcmp( *(const char **)el1, *(const char **)el2 );
+@}
+
+
+int main(void)
+@{
+  const char *nentry;
+  char *entry;
+
+  nentry = "Anyone there?";
+  entry = lsearch(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( entry )
+  @{
+    printf("Someone there: %s, %ld\n", entry, n_arr);
+  @}
+  else
+  @{
+    printf("Error, noone there: %ld...\n", n_arr);
+  @}
+
+  return 0;
+@}
+
+@end example
Index: djgpp/tests/libc/posix/search/lfind2.c
===================================================================
RCS file: djgpp/tests/libc/posix/search/lfind2.c
diff -N djgpp/tests/libc/posix/search/lfind2.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/tests/libc/posix/search/lfind2.c	7 Jan 2005 18:15:59 -0000
@@ -0,0 +1,137 @@
+/*
+ * File lfind2.c.
+ *
+ * Copyright (C) 2005 Martin Str@"omberg <ams AT ludd DOT ltu DOT se>.
+ *
+ * This software may be used freely so long as this copyright notice is
+ * left intact. There is no warranty on this software.
+ *
+ */
+
+#include <search.h>
+#include <stdio.h>
+#include <string.h>
+
+#define N_ELEM       50
+#define ELEM_SIZE    sizeof(char *)
+#define COMPARE_FUN  my_p_strcmp
+
+char *arr[N_ELEM];
+size_t n_arr = 0;
+
+
+int my_p_strcmp( const void *el1, const void *el2)
+{
+  return strcmp( *(const char **)el1, *(const char **)el2 );
+}
+
+
+int main(void)
+{
+  const char *nentry;
+  char **entry;
+  size_t i;
+
+  /* Add entries. */
+  nentry = "Alpha";
+  entry = lsearch(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( !entry || n_arr != 1 )
+  {
+    printf("Error: failed to insert 'Alpha': entry = %p = '%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+  nentry = "Bravo";
+  entry = lsearch(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( !entry || n_arr != 2 )
+  {
+    printf("Error: failed to insert 'Bravo': entry = %p = '%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+  nentry = "Charlie";
+  entry = lsearch(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( !entry || n_arr != 3 )
+  {
+    printf("Error: failed to insert 'Charlie': entry = %p = '%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+
+  /* Print out table. */
+  printf("Array now contains:\n");
+  for( i = 0; i < n_arr; i++ )
+  {
+    printf("\tIndex %ld: '%s'\n", i, arr[i]);
+  }
+
+  /* Search for the added entries. */
+  nentry = "Bravo";
+  entry = lfind(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( n_arr != 3 )
+  {
+    printf("Error: n_arr = %ld, expected 3.\n", n_arr);
+    exit(1);
+  }
+  if( !entry || strcmp("Bravo", *entry) )
+  {
+    printf("Error: failed to find 'Bravo': entry = %p->'%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+  nentry = "Charlie";
+  entry = lfind(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( n_arr != 3 )
+  {
+    printf("Error: n_arr = %ld, expected 3.\n", n_arr);
+    exit(1);
+  }
+  if( !entry || strcmp("Charlie", *entry) )
+  {
+    printf("Error: failed to find 'Charlie': entry = %p->'%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+  nentry = "Alpha";
+  entry = lfind(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( n_arr != 3 )
+  {
+    printf("Error: n_arr = %ld, expected 3.\n", n_arr);
+    exit(1);
+  }
+  if( !entry || strcmp("Alpha", *entry) )
+  {
+    printf("Error: failed to find 'Alpha': entry = %p->'%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+
+  /* Search for something not there. */
+  nentry = "Zebra";
+  entry = lfind(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( n_arr != 3 )
+  {
+    printf("Error: n_arr = %ld, expected 3.\n", n_arr);
+    exit(1);
+  }
+  if( entry )
+  {
+    printf("Error: found 'Zebra': entry = %p->'%s', n_arr = %ld.\n", 
+	   entry, entry?*entry:"NULL pointer!", n_arr);
+    exit(1);
+  }
+
+  /* Try to add one already present. */
+  nentry = "Bravo";
+  entry = lsearch(&nentry, arr, &n_arr, ELEM_SIZE, COMPARE_FUN);
+  if( !entry || n_arr != 3 )
+  {
+    printf("Error: failed to NOT insert 'Bravo': entry = %p, n_arr = %ld.\n", 
+	   entry, n_arr);
+    exit(1);
+  }
+
+
+  printf("All tests successful.\n");
+  exit(0);
+}
Index: djgpp/tests/libc/posix/search/makefile
===================================================================
RCS file: djgpp/tests/libc/posix/search/makefile
diff -N djgpp/tests/libc/posix/search/makefile
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/tests/libc/posix/search/makefile	7 Jan 2005 18:15:59 -0000
@@ -0,0 +1,6 @@
+TOP=../..
+
+SRC += lfind.c
+SRC += lfind2.c
+
+include $(TOP)/../makefile.inc

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019