]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
fea681da MK |
24 | .\" |
25 | .\" References consulted: | |
26 | .\" Linux libc source code | |
27 | .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) | |
28 | .\" 386BSD man pages | |
29 | .\" | |
30 | .\" Modified 1993-03-29, David Metcalfe | |
31 | .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu) | |
788be124 | 32 | .\" 2006-01-15, mtk, Added example program. |
6d388dc9 MK |
33 | .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net> |
34 | .\" and Ben Bacarisse <software@bsb.me.uk> | |
35 | .\" Document qsort_r() | |
fea681da | 36 | .\" |
4b8c67d9 | 37 | .TH QSORT 3 2017-09-15 "" "Linux Programmer's Manual" |
fea681da | 38 | .SH NAME |
b2db77f5 | 39 | qsort, qsort_r \- sort an array |
fea681da MK |
40 | .SH SYNOPSIS |
41 | .nf | |
42 | .B #include <stdlib.h> | |
68e4db0a | 43 | .PP |
fea681da | 44 | .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size , |
b2db77f5 | 45 | .BI " int (*" compar ")(const void *, const void *));" |
68e4db0a | 46 | .PP |
a094fe6d | 47 | .BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size , |
b2db77f5 | 48 | .BI " int (*" compar ")(const void *, const void *, void *)," |
a094fe6d | 49 | .BI " void *" arg ");" |
fea681da | 50 | .fi |
68e4db0a | 51 | .PP |
b2db77f5 BB |
52 | .in -4n |
53 | Feature Test Macro Requirements for glibc (see | |
54 | .BR feature_test_macros (7)): | |
55 | .in | |
68e4db0a | 56 | .PP |
b2db77f5 BB |
57 | .ad l |
58 | .BR qsort_r (): | |
59 | _GNU_SOURCE | |
60 | .ad b | |
fea681da | 61 | .SH DESCRIPTION |
60a90ecd MK |
62 | The |
63 | .BR qsort () | |
64 | function sorts an array with \fInmemb\fP elements of | |
c13182ef MK |
65 | size \fIsize\fP. |
66 | The \fIbase\fP argument points to the start of the | |
fea681da MK |
67 | array. |
68 | .PP | |
69 | The contents of the array are sorted in ascending order according to a | |
70 | comparison function pointed to by \fIcompar\fP, which is called with two | |
71 | arguments that point to the objects being compared. | |
72 | .PP | |
73 | The comparison function must return an integer less than, equal to, or | |
74 | greater than zero if the first argument is considered to be respectively | |
c13182ef | 75 | less than, equal to, or greater than the second. |
6d388dc9 MK |
76 | If two members compare as equal, |
77 | their order in the sorted array is undefined. | |
a094fe6d MB |
78 | .PP |
79 | The | |
80 | .BR qsort_r () | |
81 | function is identical to | |
82 | .BR qsort () | |
83 | except that the comparison function | |
84 | .I compar | |
6d388dc9 MK |
85 | takes a third argument. |
86 | A pointer is passed to the comparison function via | |
a094fe6d MB |
87 | .IR arg . |
88 | In this way, the comparison function does not need to use global variables to | |
6d388dc9 | 89 | pass through arbitrary arguments, and is therefore reentrant and safe to |
a094fe6d | 90 | use in threads. |
47297adb | 91 | .SH RETURN VALUE |
60a90ecd MK |
92 | The |
93 | .BR qsort () | |
a094fe6d MB |
94 | and |
95 | .BR qsort_r () | |
96 | functions return no value. | |
3b52faf1 MK |
97 | .SH VERSIONS |
98 | .BR qsort_r () | |
99 | was added to glibc in version 2.8. | |
b24d9e5a ZL |
100 | .SH ATTRIBUTES |
101 | For an explanation of the terms used in this section, see | |
102 | .BR attributes (7). | |
103 | .TS | |
104 | allbox; | |
105 | lbw18 lb lb | |
106 | l l l. | |
107 | Interface Attribute Value | |
108 | T{ | |
109 | .BR qsort (), | |
110 | .BR qsort_r () | |
111 | T} Thread safety MT-Safe | |
112 | .TE | |
847e0d88 | 113 | .sp 1 |
47297adb | 114 | .SH CONFORMING TO |
adc3e77f MK |
115 | .BR qsort (): |
116 | POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. | |
19c98696 | 117 | .SH NOTES |
c13182ef | 118 | To compare C strings, the comparison function can call |
fb186734 | 119 | .BR strcmp (3), |
cb478668 | 120 | as shown in the example below. |
fea681da | 121 | .SH EXAMPLE |
788be124 MK |
122 | For one example of use, see the example under |
123 | .BR bsearch (3). | |
847e0d88 | 124 | .PP |
a057d61f | 125 | Another example is the following program, |
48a1cb49 | 126 | which sorts the strings given in its command-line arguments: |
bdd915e2 MK |
127 | .PP |
128 | .EX | |
788be124 MK |
129 | #include <stdio.h> |
130 | #include <stdlib.h> | |
788be124 | 131 | #include <string.h> |
788be124 MK |
132 | |
133 | static int | |
134 | cmpstringp(const void *p1, const void *p2) | |
135 | { | |
7c5bd0eb | 136 | /* The actual arguments to this function are "pointers to |
9daa4fb9 | 137 | pointers to char", but strcmp(3) arguments are "pointers |
788be124 MK |
138 | to char", hence the following cast plus dereference */ |
139 | ||
7c5bd0eb | 140 | return strcmp(* (char * const *) p1, * (char * const *) p2); |
788be124 MK |
141 | } |
142 | ||
143 | int | |
144 | main(int argc, char *argv[]) | |
145 | { | |
146 | int j; | |
147 | ||
6b34fb3f | 148 | if (argc < 2) { |
5a6194a4 MK |
149 | fprintf(stderr, "Usage: %s <string>...\\n", argv[0]); |
150 | exit(EXIT_FAILURE); | |
6b34fb3f | 151 | } |
788be124 | 152 | |
29059a65 | 153 | qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); |
788be124 MK |
154 | |
155 | for (j = 1; j < argc; j++) | |
156 | puts(argv[j]); | |
157 | exit(EXIT_SUCCESS); | |
158 | } | |
bdd915e2 | 159 | .EE |
47297adb | 160 | .SH SEE ALSO |
fea681da MK |
161 | .BR sort (1), |
162 | .BR alphasort (3), | |
163 | .BR strcmp (3), | |
164 | .BR versionsort (3) |