2 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>
3 .\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH strverscmp 3 (date) "Linux man-pages (unreleased)"
9 strverscmp \- compare two version strings
12 .RI ( libc ", " \-lc )
15 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .B #include <string.h>
18 .BI "int strverscmp(const char *" s1 ", const char *" s2 );
22 .IR jan1 ", " jan2 ", ..., " jan9 ", " jan10 ", ..."
23 and it feels wrong when
26 .IR jan1 ", " jan10 ", ..., " jan2 ", ..., " jan9 .
27 .\" classical solution: "rename jan jan0 jan?"
28 In order to rectify this, GNU introduced the
32 which is implemented using
39 is to compare two strings and find the "right" order, while
41 finds only the lexicographic order.
42 This function does not use
45 so is meant mostly for situations
46 where the strings are expected to be in ASCII.
48 What this function does is the following.
49 If both strings are equal, return 0.
50 Otherwise, find the position
51 between two bytes with the property that before it both strings are equal,
52 while directly after it there is a difference.
53 Find the largest consecutive digit strings containing (or starting at,
54 or ending at) this position.
55 If one or both of these is empty,
58 would have returned (numerical ordering of byte values).
59 Otherwise, compare both digit strings numerically, where digit strings with
60 one or more leading zeros are interpreted as if they have a decimal point
61 in front (so that in particular digit strings with more leading zeros
62 come before digit strings with fewer leading zeros).
64 .IR 000 ", " 00 ", " 01 ", " 010 ", " 09 ", " 0 ", " 1 ", " 9 ", " 10 .
68 function returns an integer
69 less than, equal to, or greater than zero if
71 is found, respectively, to be earlier than, equal to,
75 For an explanation of the terms used in this section, see
81 Interface Attribute Value
86 T} Thread safety MT-Safe
88 .\" FIXME: The marking is different from that in the glibc manual,
91 .\" strverscmp: MT-Safe locale
93 .\" glibc manual says strverscmp should have marking locale because it calls
94 .\" isdigit() multiple times and isdigit() uses locale variable.
95 .\" But isdigit() has two implementations. With different compiling conditions,
96 .\" we may call isdigit() in macro, then strverscmp() should not have locale
101 The program below can be used to demonstrate the behavior of
105 to compare the two strings given as its command-line arguments.
106 An example of its use is the following:
110 $ \fB./a.out jan1 jan10\fP
116 .\" SRC BEGIN (strverscmp.c)
124 main(int argc, char *argv[])
129 fprintf(stderr, "Usage: %s <string1> <string2>\en", argv[0]);
133 res = strverscmp(argv[1], argv[2]);
135 printf("%s %s %s\en", argv[1],
136 (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);