2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
|
|
|
.\" References consulted:
|
|
|
|
.\" Linux libc source code
|
|
|
|
.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
|
|
|
|
.\" 386BSD man pages
|
|
|
|
.\"
|
|
|
|
.\" Modified 1993-03-29, David Metcalfe
|
|
|
|
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
|
2006-01-14 18:25:27 +00:00
|
|
|
.\" 2006-01-15, mtk, Added example program.
|
2012-03-07 21:37:26 +00:00
|
|
|
.\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
|
|
|
|
.\" and Ben Bacarisse <software@bsb.me.uk>
|
|
|
|
.\" Document qsort_r()
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2012-03-07 21:37:26 +00:00
|
|
|
.TH QSORT 3 2012-03-08 "" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
2012-03-07 21:32:51 +00:00
|
|
|
qsort, qsort_r \- sort an array
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <stdlib.h>
|
|
|
|
.sp
|
|
|
|
.BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
|
2012-03-07 21:32:51 +00:00
|
|
|
.BI " int (*" compar ")(const void *, const void *));"
|
2012-03-07 21:29:50 +00:00
|
|
|
.sp
|
|
|
|
.BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size ,
|
2012-03-07 21:32:51 +00:00
|
|
|
.BI " int (*" compar ")(const void *, const void *, void *),"
|
2012-03-07 21:29:50 +00:00
|
|
|
.BI " void *" arg ");"
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2012-03-07 21:32:51 +00:00
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.ad l
|
|
|
|
.BR qsort_r ():
|
|
|
|
_GNU_SOURCE
|
|
|
|
.ad b
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2007-05-12 09:06:04 +00:00
|
|
|
The
|
|
|
|
.BR qsort ()
|
|
|
|
function sorts an array with \fInmemb\fP elements of
|
2007-04-12 22:42:49 +00:00
|
|
|
size \fIsize\fP.
|
|
|
|
The \fIbase\fP argument points to the start of the
|
2004-11-03 13:51:07 +00:00
|
|
|
array.
|
|
|
|
.PP
|
|
|
|
The contents of the array are sorted in ascending order according to a
|
|
|
|
comparison function pointed to by \fIcompar\fP, which is called with two
|
|
|
|
arguments that point to the objects being compared.
|
|
|
|
.PP
|
|
|
|
The comparison function must return an integer less than, equal to, or
|
|
|
|
greater than zero if the first argument is considered to be respectively
|
2007-04-12 22:42:49 +00:00
|
|
|
less than, equal to, or greater than the second.
|
2012-03-07 21:37:26 +00:00
|
|
|
If two members compare as equal,
|
|
|
|
their order in the sorted array is undefined.
|
2012-03-07 21:29:50 +00:00
|
|
|
.PP
|
|
|
|
The
|
|
|
|
.BR qsort_r ()
|
|
|
|
function is identical to
|
|
|
|
.BR qsort ()
|
|
|
|
except that the comparison function
|
|
|
|
.I compar
|
2012-03-07 21:37:26 +00:00
|
|
|
takes a third argument.
|
|
|
|
A pointer is passed to the comparison function via
|
2012-03-07 21:29:50 +00:00
|
|
|
.IR arg .
|
|
|
|
In this way, the comparison function does not need to use global variables to
|
2012-03-07 21:37:26 +00:00
|
|
|
pass through arbitrary arguments, and is therefore reentrant and safe to
|
2012-03-07 21:29:50 +00:00
|
|
|
use in threads.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
2007-05-12 09:06:04 +00:00
|
|
|
The
|
|
|
|
.BR qsort ()
|
2012-03-07 21:29:50 +00:00
|
|
|
and
|
|
|
|
.BR qsort_r ()
|
|
|
|
functions return no value.
|
2012-03-07 21:37:51 +00:00
|
|
|
.SH VERSIONS
|
|
|
|
.BR qsort_r ()
|
|
|
|
was added to glibc in version 2.8.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
2012-03-07 21:29:50 +00:00
|
|
|
The
|
|
|
|
.BR qsort ()
|
|
|
|
function conforms to SVr4, 4.3BSD, C89, C99.
|
2007-05-16 02:54:18 +00:00
|
|
|
.SH NOTES
|
2004-11-03 13:51:07 +00:00
|
|
|
Library routines suitable for use as the
|
|
|
|
.I compar
|
2012-03-07 21:29:50 +00:00
|
|
|
argument to
|
|
|
|
.BR qsort ()
|
|
|
|
include
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR alphasort (3)
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR versionsort (3).
|
2007-04-12 22:42:49 +00:00
|
|
|
To compare C strings, the comparison function can call
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR strcmp (3),
|
2006-10-06 13:57:33 +00:00
|
|
|
as shown in the example below.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH EXAMPLE
|
2006-01-14 18:25:27 +00:00
|
|
|
For one example of use, see the example under
|
|
|
|
.BR bsearch (3).
|
|
|
|
|
2007-07-09 21:45:31 +00:00
|
|
|
Another example is the following program,
|
2006-01-16 19:48:56 +00:00
|
|
|
which sorts the strings given in its command-line arguments:
|
2006-01-14 18:25:27 +00:00
|
|
|
.sp
|
|
|
|
.nf
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmpstringp(const void *p1, const void *p2)
|
|
|
|
{
|
2006-05-08 06:41:59 +00:00
|
|
|
/* The actual arguments to this function are "pointers to
|
2007-06-14 06:25:37 +00:00
|
|
|
pointers to char", but strcmp(3) arguments are "pointers
|
2006-01-14 18:25:27 +00:00
|
|
|
to char", hence the following cast plus dereference */
|
|
|
|
|
2006-05-08 06:41:59 +00:00
|
|
|
return strcmp(* (char * const *) p1, * (char * const *) p2);
|
2006-01-14 18:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
execve.2, pipe.2, tee.2, fmemopen.3, mq_notify.3, qsort.3: Replace use of assert() by code that checks argc
See http://bugzilla.kernel.org/show_bug.cgi?id=13569
As noted by Andrey:
The purpose of the assert macro, defined in <assert.h>,
is to provide a tool to check for programming mistakes
or program logic errors. However, the assert macro must
never be used to perform checks for run time errors,
since, with the NDEBUG macro defined, expressions within
the assert macro invocations are not evaluated/checked
for, resulting in behavior that was not originally intended.
...
The pages affected in the core package are
execve(2)
pipe(2)
tee(2)
fmemopen(3)
mq_notify(3)
qsort(3)
Reported-by: Andrey Vihrov <vihrov@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-09-15 04:46:16 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "Usage: %s <string>...\\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2006-01-14 18:25:27 +00:00
|
|
|
|
2007-06-20 21:39:45 +00:00
|
|
|
qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
|
2006-01-14 18:25:27 +00:00
|
|
|
|
|
|
|
for (j = 1; j < argc; j++)
|
|
|
|
puts(argv[j]);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
.fi
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR sort (1),
|
|
|
|
.BR alphasort (3),
|
|
|
|
.BR strcmp (3),
|
|
|
|
.BR versionsort (3)
|