]>
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 | .\" |
3fde8c2e | 37 | .TH QSORT 3 2020-11-01 "" "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 *));" |
a094fe6d | 46 | .BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size , |
b2db77f5 | 47 | .BI " int (*" compar ")(const void *, const void *, void *)," |
a094fe6d | 48 | .BI " void *" arg ");" |
fea681da | 49 | .fi |
68e4db0a | 50 | .PP |
d39ad78f | 51 | .RS -4 |
b2db77f5 BB |
52 | Feature Test Macro Requirements for glibc (see |
53 | .BR feature_test_macros (7)): | |
d39ad78f | 54 | .RE |
68e4db0a | 55 | .PP |
b2db77f5 BB |
56 | .ad l |
57 | .BR qsort_r (): | |
58 | _GNU_SOURCE | |
e36f10f9 | 59 | .ad |
fea681da | 60 | .SH DESCRIPTION |
60a90ecd MK |
61 | The |
62 | .BR qsort () | |
63 | function sorts an array with \fInmemb\fP elements of | |
c13182ef MK |
64 | size \fIsize\fP. |
65 | The \fIbase\fP argument points to the start of the | |
fea681da MK |
66 | array. |
67 | .PP | |
68 | The contents of the array are sorted in ascending order according to a | |
69 | comparison function pointed to by \fIcompar\fP, which is called with two | |
70 | arguments that point to the objects being compared. | |
71 | .PP | |
72 | The comparison function must return an integer less than, equal to, or | |
73 | greater than zero if the first argument is considered to be respectively | |
c13182ef | 74 | less than, equal to, or greater than the second. |
6d388dc9 MK |
75 | If two members compare as equal, |
76 | their order in the sorted array is undefined. | |
a094fe6d MB |
77 | .PP |
78 | The | |
79 | .BR qsort_r () | |
80 | function is identical to | |
81 | .BR qsort () | |
82 | except that the comparison function | |
83 | .I compar | |
6d388dc9 MK |
84 | takes a third argument. |
85 | A pointer is passed to the comparison function via | |
a094fe6d MB |
86 | .IR arg . |
87 | In this way, the comparison function does not need to use global variables to | |
6d388dc9 | 88 | pass through arbitrary arguments, and is therefore reentrant and safe to |
a094fe6d | 89 | use in threads. |
47297adb | 90 | .SH RETURN VALUE |
60a90ecd MK |
91 | The |
92 | .BR qsort () | |
a094fe6d MB |
93 | and |
94 | .BR qsort_r () | |
95 | functions return no value. | |
3b52faf1 MK |
96 | .SH VERSIONS |
97 | .BR qsort_r () | |
98 | was added to glibc in version 2.8. | |
b24d9e5a ZL |
99 | .SH ATTRIBUTES |
100 | For an explanation of the terms used in this section, see | |
101 | .BR attributes (7). | |
102 | .TS | |
103 | allbox; | |
104 | lbw18 lb lb | |
105 | l l l. | |
106 | Interface Attribute Value | |
107 | T{ | |
108 | .BR qsort (), | |
109 | .BR qsort_r () | |
110 | T} Thread safety MT-Safe | |
111 | .TE | |
847e0d88 | 112 | .sp 1 |
47297adb | 113 | .SH CONFORMING TO |
adc3e77f MK |
114 | .BR qsort (): |
115 | POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. | |
19c98696 | 116 | .SH NOTES |
c13182ef | 117 | To compare C strings, the comparison function can call |
fb186734 | 118 | .BR strcmp (3), |
cb478668 | 119 | as shown in the example below. |
a14af333 | 120 | .SH EXAMPLES |
788be124 MK |
121 | For one example of use, see the example under |
122 | .BR bsearch (3). | |
847e0d88 | 123 | .PP |
a057d61f | 124 | Another example is the following program, |
48a1cb49 | 125 | which sorts the strings given in its command-line arguments: |
bdd915e2 MK |
126 | .PP |
127 | .EX | |
788be124 MK |
128 | #include <stdio.h> |
129 | #include <stdlib.h> | |
788be124 | 130 | #include <string.h> |
788be124 MK |
131 | |
132 | static int | |
133 | cmpstringp(const void *p1, const void *p2) | |
134 | { | |
7c5bd0eb | 135 | /* The actual arguments to this function are "pointers to |
9daa4fb9 | 136 | pointers to char", but strcmp(3) arguments are "pointers |
46b20ca1 | 137 | to char", hence the following cast plus dereference. */ |
788be124 | 138 | |
9ba82f22 | 139 | return strcmp(*(const char **) p1, *(const char **) p2); |
788be124 MK |
140 | } |
141 | ||
142 | int | |
143 | main(int argc, char *argv[]) | |
144 | { | |
6b34fb3f | 145 | if (argc < 2) { |
d1a71985 | 146 | fprintf(stderr, "Usage: %s <string>...\en", argv[0]); |
5a6194a4 | 147 | exit(EXIT_FAILURE); |
6b34fb3f | 148 | } |
788be124 | 149 | |
29059a65 | 150 | qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); |
788be124 | 151 | |
88893a77 | 152 | for (int j = 1; j < argc; j++) |
788be124 MK |
153 | puts(argv[j]); |
154 | exit(EXIT_SUCCESS); | |
155 | } | |
bdd915e2 | 156 | .EE |
47297adb | 157 | .SH SEE ALSO |
fea681da MK |
158 | .BR sort (1), |
159 | .BR alphasort (3), | |
160 | .BR strcmp (3), | |
161 | .BR versionsort (3) |