docs/177025: [PATCH] lsearch.3 manual page example addition

Fernando fernando.apesteguia at gmail.com
Sat Mar 16 17:40:01 UTC 2013


>Number:         177025
>Category:       docs
>Synopsis:       [PATCH] lsearch.3 manual page example addition
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-doc
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Mar 16 17:40:01 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     Fernando
>Release:        9.0-RELEASE
>Organization:
OpenSistemas
>Environment:
FreeBSD beastie 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan  3 07:46:30 UTC 2012     root at farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
We lack an example for the lsearch(3)/lfind(3) man pages.
We should improve documentation with more examples.
>How-To-Repeat:
man lsearch
>Fix:
Apply the attached patch.

Since lsearch is referenced by the hsearch(3) and bsearch(3) man pages, and all these functions work pretty much the same way, I think this one should suffice as a reference for all of them

Patch attached with submission follows:

--- /usr/src/lib/libc/stdlib/lsearch.3	2012-01-03 04:26:05.000000000 +0100
+++ lsearch.3	2013-03-15 20:27:22.000000000 +0100
@@ -81,6 +81,49 @@
 Both functions return
 .Dv NULL
 if an error occurs.
+.Sh EXAMPLES
+.Bd -literal
+
+#include <search.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int
+element_compare(const void *p1, const void *p2)
+{
+	int left = *(const int *)p1;
+	int right = *(const int *)p2;
+
+	return (left - right);
+}
+
+int
+main(int argc, char **argv)
+{
+	const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+	size_t element_size = sizeof(array[0]);
+	size_t array_size = sizeof(array) / element_size;
+	int key;	
+	void *element;
+
+	printf("Enter a number: ");
+	if (scanf("%d", &key) != 1) {
+		printf("Bad input\n");
+		return (EXIT_FAILURE);
+	}
+
+	element = lfind (&key, array, &array_size, element_size, element_compare);
+
+	if (element != NULL)
+		printf("Element found: %d\n", *(int *)element);
+	else
+		printf("Element not found\n");
+
+	return (EXIT_SUCCESS);
+}
+
+
+.Ed
 .Sh SEE ALSO
 .Xr bsearch 3 ,
 .Xr hsearch 3 ,


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-doc mailing list