]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strverscmp.3
fuse.4: fuse_entry_out: rework discussion of uniqueness of nodeid + generation
[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 2016-10-08 "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 .br
33 .B #include <string.h>
34 .sp
35 .BI "int strverscmp(const char *" s1 ", const char *" s2 );
36 .fi
37 .SH DESCRIPTION
38 Often one has files
39 .IR jan1 ", " jan2 ", ..., " jan9 ", " jan10 ", ..."
40 and it feels wrong when
41 .BR ls (1)
42 orders them
43 .IR jan1 ", " jan10 ", ..., " jan2 ", ..., " jan9 .
44 .\" classical solution: "rename jan jan0 jan?"
45 In order to rectify this, GNU introduced the
46 .I \-v
47 option to
48 .BR ls (1),
49 which is implemented using
50 .BR versionsort (3),
51 which again uses
52 .BR strverscmp ().
53
54 Thus, the task of
55 .BR strverscmp ()
56 is to compare two strings and find the "right" order, while
57 .BR strcmp (3)
58 finds only the lexicographic order.
59 This function does not use
60 the locale category
61 .BR LC_COLLATE ,
62 so is meant mostly for situations
63 where the strings are expected to be in ASCII.
64
65 What this function does is the following.
66 If both strings are equal, return 0.
67 Otherwise, find the position
68 between two bytes with the property that before it both strings are equal,
69 while directly after it there is a difference.
70 Find the largest consecutive digit strings containing (or starting at,
71 or ending at) this position.
72 If one or both of these is empty,
73 then return what
74 .BR strcmp (3)
75 would have returned (numerical ordering of byte values).
76 Otherwise, compare both digit strings numerically, where digit strings with
77 one or more leading zeros are interpreted as if they have a decimal point
78 in front (so that in particular digit strings with more leading zeros
79 come before digit strings with fewer leading zeros).
80 Thus, the ordering is
81 .IR 000 ", " 00 ", " 01 ", " 010 ", " 09 ", " 0 ", " 1 ", " 9 ", " 10 .
82 .SH RETURN VALUE
83 The
84 .BR strverscmp ()
85 function returns an integer
86 less than, equal to, or greater than zero if
87 .I s1
88 is found, respectively, to be earlier than, equal to,
89 or later than
90 .IR s2 .
91 .SH ATTRIBUTES
92 For an explanation of the terms used in this section, see
93 .BR attributes (7).
94 .TS
95 allbox;
96 lb lb lb
97 l l l.
98 Interface Attribute Value
99 T{
100 .BR strverscmp ()
101 T} Thread safety MT-Safe
102 .TE
103 .\" FIXME: The marking is different from that in the glibc manual,
104 .\" which has:
105 .\"
106 .\" strverscmp: MT-Safe locale
107 .\"
108 .\" glibc manual says strverscmp should have marking locale because it calls
109 .\" isdigit() multiple times and isdigit() uses locale variable.
110 .\" But isdigit() has two implementations. With different compiling conditions,
111 .\" we may call isdigit() in macro, then strverscmp() should not have locale
112 .\" problem.
113 .SH CONFORMING TO
114 This function is a GNU extension.
115 .SH EXAMPLE
116 The program below can be used to demonstrate the behavior of
117 .BR strverscmp ().
118 It uses
119 .BR strverscmp ()
120 to compare the two strings given as its command-line arguments.
121 An example of its use is the following:
122
123 .in
124 .in +4n
125 .nf
126 $ i\fB./a.out jan1 jan10\fP
127 jan1 < jan10
128 .fi
129 .in
130 .SS Program source
131 \&
132 .nf
133 #define _GNU_SOURCE
134 #include <string.h>
135 #include <stdio.h>
136 #include <stdlib.h>
137
138 int
139 main(int argc, char *argv[])
140 {
141 int res;
142
143 if (argc != 3) {
144 fprintf(stderr, "Usage: %s <string1> <string2>\\n", argv[0]);
145 exit(EXIT_FAILURE);
146 }
147
148 res = strverscmp(argv[1], argv[2]);
149
150 printf("%s %s %s\\n", argv[1],
151 (res == \-1) ? "<" : (res == 0) ? "==" : ">", argv[2]);
152
153 exit(EXIT_SUCCESS);
154 }
155 .fi
156 .SH SEE ALSO
157 .BR rename (1),
158 .BR strcasecmp (3),
159 .BR strcmp (3),
160 .BR strcoll (3)