From owner-svn-src-head@freebsd.org Fri Jul 14 17:07:29 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6231CDA82B5; Fri, 14 Jul 2017 17:07:29 +0000 (UTC) (envelope-from brd@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2F7B06E57B; Fri, 14 Jul 2017 17:07:29 +0000 (UTC) (envelope-from brd@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v6EH7SML076956; Fri, 14 Jul 2017 17:07:28 GMT (envelope-from brd@FreeBSD.org) Received: (from brd@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v6EH7STB076955; Fri, 14 Jul 2017 17:07:28 GMT (envelope-from brd@FreeBSD.org) Message-Id: <201707141707.v6EH7STB076955@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: brd set sender to brd@FreeBSD.org using -f From: Brad Davis Date: Fri, 14 Jul 2017 17:07:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r320992 - head/lib/libc/stdlib X-SVN-Group: head X-SVN-Commit-Author: brd X-SVN-Commit-Paths: head/lib/libc/stdlib X-SVN-Commit-Revision: 320992 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Jul 2017 17:07:29 -0000 Author: brd (doc,ports committer) Date: Fri Jul 14 17:07:28 2017 New Revision: 320992 URL: https://svnweb.freebsd.org/changeset/base/320992 Log: Add a complete example to tsearch(3) Reviewed by: wblock, sevan, bruffer MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D11053 Modified: head/lib/libc/stdlib/tsearch.3 Modified: head/lib/libc/stdlib/tsearch.3 ============================================================================== --- head/lib/libc/stdlib/tsearch.3 Fri Jul 14 16:45:46 2017 (r320991) +++ head/lib/libc/stdlib/tsearch.3 Fri Jul 14 17:07:28 2017 (r320992) @@ -27,7 +27,7 @@ .\" OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp .\" $FreeBSD$ .\" -.Dd October 9, 2016 +.Dd June 4, 2017 .Dt TSEARCH 3 .Os .Sh NAME @@ -130,6 +130,64 @@ is NULL or the datum cannot be found. The .Fn twalk function returns no value. +.Sh EXAMPLES +This example uses +.Fn tsearch +to search for four strings in +.Dv root . +Because the strings are not already present, they are added. +.Fn tsearch +is called twice on the fourth string to demonstrate that a string is not added when it is already present. +.Fn tfind +is used to find the single instance of the fourth string, and +.Fn tdelete +removes it. +Finally, +.Fn twalk +is used to return and display the resulting binary search tree. +.Bd -literal +#include +#include +#include + +int +comp(const void *a, const void *b) +{ + + return strcmp(a, b); +} + +void +printwalk(const posix_tnode * node, VISIT v, int __unused0) +{ + + if (v == postorder || v == leaf) { + printf("node: %s\n\\", *(char **)node); + } +} + +int +main(void) +{ + posix_tnode *root = NULL; + + char one[] = "blah1"; + char two[] = "blah-2"; + char three[] = "blah-3"; + char four[] = "blah-4"; + + tsearch(one, &root, comp); + tsearch(two, &root, comp); + tsearch(three, &root, comp); + tsearch(four, &root, comp); + tsearch(four, &root, comp); + printf("four: %s\n", *(char **)tfind(four, &root, comp)); + tdelete(four, &root, comp); + + twalk(root, printwalk); + return 0; +} +.Ed .Sh SEE ALSO .Xr bsearch 3 , .Xr hsearch 3 ,