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.
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2008-06-11 09:55:35 +00:00
|
|
|
.\" FIXME glibc 2.8 added qsort_r(), which needs to be documented.
|
|
|
|
.\"
|
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
|
|
|
.TH QSORT 3 2009-09-15 "" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
qsort \- sorts an array
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <stdlib.h>
|
|
|
|
.sp
|
|
|
|
.BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
|
2007-12-19 06:57:44 +00:00
|
|
|
.BI " int(*" compar ")(const void *, const void *));"
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
|
|
|
.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.
|
|
|
|
If two members compare
|
2004-11-03 13:51:07 +00:00
|
|
|
as equal, their order in the sorted array is undefined.
|
|
|
|
.SH "RETURN VALUE"
|
2007-05-12 09:06:04 +00:00
|
|
|
The
|
|
|
|
.BR qsort ()
|
|
|
|
function returns no value.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
2006-12-16 22:11:08 +00:00
|
|
|
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
|
|
|
|
argument 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)
|