From owner-svn-src-stable@freebsd.org Sat May 18 08:28:52 2019 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7C24815ABFB3; Sat, 18 May 2019 08:28:52 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id AC3506EE59; Sat, 18 May 2019 08:28:51 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id x4I8ScTM039608 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 18 May 2019 11:28:41 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua x4I8ScTM039608 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id x4I8ScNg039607; Sat, 18 May 2019 11:28:38 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 18 May 2019 11:28:38 +0300 From: Konstantin Belousov To: Benedict Reuschling Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: Re: svn commit: r347951 - stable/12/lib/libc/stdlib Message-ID: <20190518082838.GW2748@kib.kiev.ua> References: <201905180315.x4I3F8g2080553@repo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201905180315.x4I3F8g2080553@repo.freebsd.org> User-Agent: Mutt/1.11.4 (2019-03-13) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 18 May 2019 08:28:52 -0000 On Sat, May 18, 2019 at 03:15:08AM +0000, Benedict Reuschling wrote: > Author: bcr (doc committer) > Date: Sat May 18 03:15:07 2019 > New Revision: 347951 > URL: https://svnweb.freebsd.org/changeset/base/347951 > > Log: > MFC r347617: > Add small EXAMPLE section to bsearch.3. > > Submitted by: fernape (via Phabricator) > Reviewed by: bcr, jilles, dab > Approved by: bcr (man pages), jilles (src) > Differential Revision: https://reviews.freebsd.org/D19902 > > Modified: > stable/12/lib/libc/stdlib/bsearch.3 > Directory Properties: > stable/12/ (props changed) > > Modified: stable/12/lib/libc/stdlib/bsearch.3 > ============================================================================== > --- stable/12/lib/libc/stdlib/bsearch.3 Sat May 18 02:02:14 2019 (r347950) > +++ stable/12/lib/libc/stdlib/bsearch.3 Sat May 18 03:15:07 2019 (r347951) > @@ -32,7 +32,7 @@ > .\" @(#)bsearch.3 8.3 (Berkeley) 4/19/94 > .\" $FreeBSD$ > .\" > -.Dd February 22, 2013 > +.Dd May 15, 2019 > .Dt BSEARCH 3 > .Os > .Sh NAME > @@ -83,6 +83,61 @@ The > function returns a pointer to a matching member of the array, or a null > pointer if no match is found. > If two members compare as equal, which member is matched is unspecified. > +.Sh EXAMPLES > +A sample program that searches people by age in a sorted array: > +.Bd -literal > +#include > +#include > +#include > +#include > +#include > + > +struct person { > + char name[5]; > + int age; > +}; > + > +int > +compare(const void *key, const void *array_member) > +{ > + int age = (intptr_t) key; > + struct person person = *(struct person *) array_member; These two lines contain at least three style(9) bugs, and at least one warning at higher warning level. > + > + return (age - person.age); > +} > + > +int > +main() Why use K&R definition ? > +{ > + struct person *friend; > + > + /* Sorted array */ > + struct person friends[6] = { > + { "paul", 22 }, > + { "anne", 25 }, > + { "fred", 25 }, > + { "mary", 27 }, > + { "mark", 35 }, > + { "bill", 50 } > + }; > + > + size_t array_size = sizeof(friends) / sizeof(struct person); Since you used const elsewere, why did not you used it there ? > + > + friend = bsearch((void *)22, &friends, array_size, sizeof(struct person), compare); Taking address of an array is weird. Line is too long. > + assert(strcmp(friend->name, "paul") == 0); > + printf("name: %s\enage: %d\en", friend->name, friend->age); > + > + friend = bsearch((void *)25, &friends, array_size, sizeof(struct person), compare); > + assert(strcmp(friend->name, "fred") == 0 || strcmp(friend->name, "anne") == 0); > + printf("name: %s\enage: %d\en", friend->name, friend->age); > + > + friend = bsearch((void *)30, &friends, array_size, sizeof(struct person), compare); > + assert(friend == NULL); > + printf("friend aged 30 not found\en"); > + > + return (EXIT_SUCCESS); > +} > +.Ed > .Sh SEE ALSO > .Xr db 3 , > .Xr lsearch 3 ,