]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strverscmp.3
tsearch.3: SYNOPSIS: clarify that twalk_r() requires _GNU_SOURCE
[thirdparty/man-pages.git] / man3 / strverscmp.3
1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>
2 .\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH STRVERSCMP 3 2019-03-06 "GNU" "Linux Programmer's Manual"
27 .SH NAME
28 strverscmp \- compare two version strings
29 .SH SYNOPSIS
30 .nf
31 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
32 .B #include <string.h>
33 .PP
34 .BI "int strverscmp(const char *" s1 ", const char *" s2 );
35 .fi
36 .SH DESCRIPTION
37 Often one has files
38 .IR jan1 ", " jan2 ", ..., " jan9 ", " jan10 ", ..."
39 and it feels wrong when
40 .BR ls (1)
41 orders them
42 .IR jan1 ", " jan10 ", ..., " jan2 ", ..., " jan9 .
43 .\" classical solution: "rename jan jan0 jan?"
44 In order to rectify this, GNU introduced the
45 .I \-v
46 option to
47 .BR ls (1),
48 which is implemented using
49 .BR versionsort (3),
50 which again uses
51 .BR strverscmp ().
52 .PP
53 Thus, the task of
54 .BR strverscmp ()
55 is to compare two strings and find the "right" order, while
56 .BR strcmp (3)
57 finds only the lexicographic order.
58 This function does not use
59 the locale category
60 .BR LC_COLLATE ,
61 so is meant mostly for situations
62 where the strings are expected to be in ASCII.
63 .PP
64 What this function does is the following.
65 If both strings are equal, return 0.
66 Otherwise, find the position
67 between two bytes with the property that before it both strings are equal,
68 while directly after it there is a difference.
69 Find the largest consecutive digit strings containing (or starting at,
70 or ending at) this position.
71 If one or both of these is empty,
72 then return what
73 .BR strcmp (3)
74 would have returned (numerical ordering of byte values).
75 Otherwise, compare both digit strings numerically, where digit strings with
76 one or more leading zeros are interpreted as if they have a decimal point
77 in front (so that in particular digit strings with more leading zeros
78 come before digit strings with fewer leading zeros).
79 Thus, the ordering is
80 .IR 000 ", " 00 ", " 01 ", " 010 ", " 09 ", " 0 ", " 1 ", " 9 ", " 10 .
81 .SH RETURN VALUE
82 The
83 .BR strverscmp ()
84 function returns an integer
85 less than, equal to, or greater than zero if
86 .I s1
87 is found, respectively, to be earlier than, equal to,
88 or later than
89 .IR s2 .
90 .SH ATTRIBUTES
91 For an explanation of the terms used in this section, see
92 .BR attributes (7).
93 .TS
94 allbox;
95 lb lb lb
96 l l l.
97 Interface Attribute Value
98 T{
99 .BR strverscmp ()
100 T} Thread safety MT-Safe
101 .TE
102 .\" FIXME: The marking is different from that in the glibc manual,
103 .\" which has:
104 .\"
105 .\" strverscmp: MT-Safe locale
106 .\"
107 .\" glibc manual says strverscmp should have marking locale because it calls
108 .\" isdigit() multiple times and isdigit() uses locale variable.
109 .\" But isdigit() has two implementations. With different compiling conditions,
110 .\" we may call isdigit() in macro, then strverscmp() should not have locale
111 .\" problem.
112 .SH CONFORMING TO
113 This function is a GNU extension.
114 .SH EXAMPLE
115 The program below can be used to demonstrate the behavior of
116 .BR strverscmp ().
117 It uses
118 .BR strverscmp ()
119 to compare the two strings given as its command-line arguments.
120 An example of its use is the following:
121 .PP
122 .in +4n
123 .EX
124 $ \fB./a.out jan1 jan10\fP
125 jan1 < jan10
126 .EE
127 .in
128 .SS Program source
129 \&
130 .EX
131 #define _GNU_SOURCE
132 #include <string.h>
133 #include <stdio.h>
134 #include <stdlib.h>
135
136 int
137 main(int argc, char *argv[])
138 {
139 int res;
140
141 if (argc != 3) {
142 fprintf(stderr, "Usage: %s <string1> <string2>\en", argv[0]);
143 exit(EXIT_FAILURE);
144 }
145
146 res = strverscmp(argv[1], argv[2]);
147
148 printf("%s %s %s\en", argv[1],
149 (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);
150
151 exit(EXIT_SUCCESS);
152 }
153 .EE
154 .SH SEE ALSO
155 .BR rename (1),
156 .BR strcasecmp (3),
157 .BR strcmp (3),
158 .BR strcoll (3)