]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strverscmp.3
proc.5: Note kernel version for /proc/PID/smaps VmFlags "wf" flag
[thirdparty/man-pages.git] / man3 / strverscmp.3
CommitLineData
fea681da 1.\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>
1f73bfaf 2.\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da 25.\"
9ba01802 26.TH STRVERSCMP 3 2019-03-06 "GNU" "Linux Programmer's Manual"
fea681da
MK
27.SH NAME
28strverscmp \- compare two version strings
29.SH SYNOPSIS
30.nf
b80f966b 31.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
fea681da 32.B #include <string.h>
68e4db0a 33.PP
fea681da
MK
34.BI "int strverscmp(const char *" s1 ", const char *" s2 );
35.fi
36.SH DESCRIPTION
37Often one has files
38.IR jan1 ", " jan2 ", ..., " jan9 ", " jan10 ", ..."
39and it feels wrong when
f19a0f03 40.BR ls (1)
fea681da 41orders them
c10859eb 42.IR jan1 ", " jan10 ", ..., " jan2 ", ..., " jan9 .
fea681da
MK
43.\" classical solution: "rename jan jan0 jan?"
44In order to rectify this, GNU introduced the
f19a0f03 45.I \-v
fea681da
MK
46option to
47.BR ls (1),
48which is implemented using
49.BR versionsort (3),
50which again uses
e511ffb6 51.BR strverscmp ().
f47758ba 52.PP
fea681da 53Thus, the task of
e511ffb6 54.BR strverscmp ()
fea681da 55is to compare two strings and find the "right" order, while
fb186734 56.BR strcmp (3)
33a0ccb2 57finds only the lexicographic order.
c13182ef 58This function does not use
097585ed
MK
59the locale category
60.BR LC_COLLATE ,
61so is meant mostly for situations
fea681da 62where the strings are expected to be in ASCII.
f47758ba 63.PP
fea681da 64What this function does is the following.
c13182ef 65If both strings are equal, return 0.
2b9b829d 66Otherwise, find the position
fea681da
MK
67between two bytes with the property that before it both strings are equal,
68while directly after it there is a difference.
69Find the largest consecutive digit strings containing (or starting at,
c13182ef
MK
70or ending at) this position.
71If one or both of these is empty,
fea681da 72then return what
fb186734 73.BR strcmp (3)
fea681da
MK
74would have returned (numerical ordering of byte values).
75Otherwise, compare both digit strings numerically, where digit strings with
7b01461a
MK
76one or more leading zeros are interpreted as if they have a decimal point
77in front (so that in particular digit strings with more leading zeros
78come before digit strings with fewer leading zeros).
fea681da
MK
79Thus, the ordering is
80.IR 000 ", " 00 ", " 01 ", " 010 ", " 09 ", " 0 ", " 1 ", " 9 ", " 10 .
47297adb 81.SH RETURN VALUE
60a90ecd
MK
82The
83.BR strverscmp ()
84function returns an integer
c6fa0841
MK
85less than, equal to, or greater than zero if
86.I s1
fea681da 87is found, respectively, to be earlier than, equal to,
c6fa0841
MK
88or later than
89.IR s2 .
9aa3ae0b
PH
90.SH ATTRIBUTES
91For an explanation of the terms used in this section, see
92.BR attributes (7).
93.TS
94allbox;
95lb lb lb
96l l l.
97Interface Attribute Value
98T{
99.BR strverscmp ()
80189236
MK
100T} 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.\"
72ab1faa
MS
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.
47297adb 112.SH CONFORMING TO
fea681da 113This function is a GNU extension.
1f73bfaf
MK
114.SH EXAMPLE
115The program below can be used to demonstrate the behavior of
116.BR strverscmp ().
117It uses
118.BR strverscmp ()
119to compare the two strings given as its command-line arguments.
120An example of its use is the following:
f47758ba 121.PP
1f73bfaf 122.in +4n
b8302363 123.EX
0fd299bf 124$ \fB./a.out jan1 jan10\fP
1f73bfaf 125jan1 < jan10
b8302363 126.EE
1f73bfaf
MK
127.in
128.SS Program source
129\&
e7d0bb47 130.EX
1f73bfaf
MK
131#define _GNU_SOURCE
132#include <string.h>
133#include <stdio.h>
134#include <stdlib.h>
135
136int
137main(int argc, char *argv[])
138{
139 int res;
140
141 if (argc != 3) {
d1a71985 142 fprintf(stderr, "Usage: %s <string1> <string2>\en", argv[0]);
1f73bfaf
MK
143 exit(EXIT_FAILURE);
144 }
145
146 res = strverscmp(argv[1], argv[2]);
147
d1a71985 148 printf("%s %s %s\en", argv[1],
729b84f4 149 (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);
1f73bfaf
MK
150
151 exit(EXIT_SUCCESS);
152}
e7d0bb47 153.EE
47297adb 154.SH SEE ALSO
fea681da
MK
155.BR rename (1),
156.BR strcasecmp (3),
157.BR strcmp (3),
0a4f8b7b 158.BR strcoll (3)