From owner-svn-src-stable-12@freebsd.org Wed Jul 24 06:29:20 2019 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 584FDBE29F; Wed, 24 Jul 2019 06:29:20 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3555E6AAE3; Wed, 24 Jul 2019 06:29:20 +0000 (UTC) (envelope-from kib@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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 08F09A1FE; Wed, 24 Jul 2019 06:29:20 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x6O6TJic046871; Wed, 24 Jul 2019 06:29:19 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x6O6TJRr046870; Wed, 24 Jul 2019 06:29:19 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201907240629.x6O6TJRr046870@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Wed, 24 Jul 2019 06:29:19 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r350272 - stable/12/lib/libc/stdlib X-SVN-Group: stable-12 X-SVN-Commit-Author: kib X-SVN-Commit-Paths: stable/12/lib/libc/stdlib X-SVN-Commit-Revision: 350272 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 3555E6AAE3 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.92 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.92)[-0.919,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 Jul 2019 06:29:20 -0000 Author: kib Date: Wed Jul 24 06:29:19 2019 New Revision: 350272 URL: https://svnweb.freebsd.org/changeset/base/350272 Log: MFC r350091: bsearch.3: Improve the example 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 Wed Jul 24 04:48:04 2019 (r350271) +++ stable/12/lib/libc/stdlib/bsearch.3 Wed Jul 24 06:29:19 2019 (r350272) @@ -32,7 +32,7 @@ .\" @(#)bsearch.3 8.3 (Berkeley) 4/19/94 .\" $FreeBSD$ .\" -.Dd May 15, 2019 +.Dd July 17, 2019 .Dt BSEARCH 3 .Os .Sh NAME @@ -93,26 +93,29 @@ A sample program that searches people by age in a sort #include struct person { - char name[5]; - int age; + const char *name; + int age; }; -int -compare(const void *key, const void *array_member) +static int +compare(const void *a, const void *b) { - int age = (intptr_t) key; - struct person person = *(struct person *) array_member; + const int *age; + const struct person *person; - return (age - person.age); + age = a; + person = b; + + return (*age - person->age); } int -main() +main(void) { struct person *friend; - + int age; /* Sorted array */ - struct person friends[6] = { + const struct person friends[] = { { "paul", 22 }, { "anne", 25 }, { "fred", 25 }, @@ -120,22 +123,28 @@ main() { "mark", 35 }, { "bill", 50 } }; + const size_t len = sizeof(friends) / sizeof(friends[0]); - size_t array_size = sizeof(friends) / sizeof(struct person); - - friend = bsearch((void *)22, &friends, array_size, sizeof(struct person), compare); + age = 22; + friend = bsearch(&age, friends, len, sizeof(friends[0]), compare); 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); + age = 25; + friend = bsearch(&age, friends, len, sizeof(friends[0]), compare); + + /* + * For multiple elements with the same key, it is implementation + * defined which will be returned + */ + 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); + age = 30; + friend = bsearch(&age, friends, len, sizeof(friends[0]), compare); assert(friend == NULL); printf("friend aged 30 not found\en"); - - return (EXIT_SUCCESS); } .Ed .Sh SEE ALSO