From owner-freebsd-doc@FreeBSD.ORG Sat Feb 16 18:00:00 2013 Return-Path: Delivered-To: freebsd-doc@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id CCF55D54 for ; Sat, 16 Feb 2013 18:00:00 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) by mx1.freebsd.org (Postfix) with ESMTP id B13A3714 for ; Sat, 16 Feb 2013 18:00:00 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.6/8.14.6) with ESMTP id r1GI00kf029858 for ; Sat, 16 Feb 2013 18:00:00 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.6/8.14.6/Submit) id r1GI00Nb029853; Sat, 16 Feb 2013 18:00:00 GMT (envelope-from gnats) Resent-Date: Sat, 16 Feb 2013 18:00:00 GMT Resent-Message-Id: <201302161800.r1GI00Nb029853@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-doc@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Fernando Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 44F76B06 for ; Sat, 16 Feb 2013 17:50:31 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id 212FC6DE for ; Sat, 16 Feb 2013 17:50:31 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.5/8.14.5) with ESMTP id r1GHoU4t018084 for ; Sat, 16 Feb 2013 17:50:30 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.5/8.14.5/Submit) id r1GHoUDv018083; Sat, 16 Feb 2013 17:50:30 GMT (envelope-from nobody) Message-Id: <201302161750.r1GHoUDv018083@red.freebsd.org> Date: Sat, 16 Feb 2013 17:50:30 GMT From: Fernando To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Subject: docs/176197: [PATCH] Add example to qsort.3 X-BeenThere: freebsd-doc@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Documentation project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Feb 2013 18:00:00 -0000 >Number: 176197 >Category: docs >Synopsis: [PATCH] Add example to qsort.3 >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-doc >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Sat Feb 16 18:00:00 UTC 2013 >Closed-Date: >Last-Modified: >Originator: Fernando >Release: 9.0-RELEASE >Organization: Open Sistemas >Environment: FreeBSD hammer 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan 3 07:46:30 UTC 2012 root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64 >Description: qsort(3) lacks an example in the man page. If we want to have the best documentation available, we need examples in some man pages. >How-To-Repeat: man qsort It doesn't show an example of use >Fix: Apply the attached patch. Patch attached with submission follows: --- lib/libc/stdlib/qsort.3 2012-01-03 04:26:05.000000000 +0100 +++ /home/fernape/Programming/FreeBSD/qsort_man/qsort.3 2013-02-16 18:42:44.000000000 +0100 @@ -211,6 +211,52 @@ did not permit the comparison routine itself to call .Fn qsort 3 . This is no longer true. +.Sh EXAMPLES +Sort an array of integers. +.Bd -literal +#include +#include +#include + +/* + * Custom comparison function + */ +static int +str_compare(const void *p1, const void *p2) +{ + + /* Cast and dereference */ + if (*(int *)p1 < *(int *)p2) + return (-1); + + if (*(int *)p1 > *(int *)p2) + return (1); + + return (0); + +} + +/* + * Sort an array of integers + */ +int +main(int argc, char **argv) +{ + int i; + int int_array[] = {4, 5, 9, 3, 1, 7, 2, 8, 6}; + const int array_size = sizeof(int_array) / sizeof(int); + + /* Sort array */ + qsort(&int_array, array_size, sizeof(int), str_compare); + + /* Print out sorted array */ + for (i = 0; i < array_size; i++) { + printf("%d\n", int_array[i]); + } + + exit(EXIT_SUCCESS); +} +.Ed .Sh ERRORS The .Fn heapsort >Release-Note: >Audit-Trail: >Unformatted: