Debian Codegraph @

upstream/fedora-44/man3/strverscmp.3

raw
1 '\" t
2 .\" Copyright (C) 2001 Andries Brouwer <[email protected]>
3 .\" and Copyright (C) 2016 Michael Kerrisk <[email protected]>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH strverscmp 3 2024-12-16 "Linux man-pages 6.13"
8 .SH NAME
9 strverscmp \- compare two version strings
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .B #include <string.h>
17 .P
18 .BI "int strverscmp(const char *" s1 ", const char *" s2 );
19 .fi
20 .SH DESCRIPTION
21 For a dataset like
22 .IR jan1 ", " jan2 ", ..., " jan9 ", " jan10 ", ..."
23 sorting it lexicographically yields
24 .IR jan1 ", " jan10 ", ..., " jan2 ", ..., " jan9 .
25 .\" classical solution: "rename jan jan0 jan?"
26 The task of
27 .BR strverscmp ()
28 is to compare two strings yielding the former order, while
29 .BR strcmp (3)
30 finds only the lexicographic order.
31 This function does not use
32 the locale category
33 .BR LC_COLLATE ,
34 so is meant mostly for situations
35 where the strings are expected to be in ASCII.
36 This is different from the ordering produced by
37 .BR sort (1)
38 .BR -V .
39 .\" sort -V sorts a-1.0a < a-1.0.1a; strverscmp() does not
40 .P
41 What this function does is the following.
42 If both strings are equal, return 0.
43 Otherwise, find the position
44 between two bytes with the property that before it both strings are equal,
45 while directly after it there is a difference.
46 Find the largest consecutive digit strings containing (or starting at,
47 or ending at) this position.
48 If one or both of these is empty,
49 then return what
50 .BR strcmp (3)
51 would have returned (numerical ordering of byte values).
52 Otherwise, compare both digit strings numerically, where digit strings with
53 one or more leading zeros are interpreted as if they have a decimal point
54 in front (so that in particular digit strings with more leading zeros
55 come before digit strings with fewer leading zeros).
56 Thus, the ordering is
57 .IR 000 ", " 00 ", " 01 ", " 010 ", " 09 ", " 0 ", " 1 ", " 9 ", " 10 .
58 .SH RETURN VALUE
59 The
60 .BR strverscmp ()
61 function returns an integer
62 less than, equal to, or greater than zero if
63 .I s1
64 is found, respectively, to be earlier than, equal to,
65 or later than
66 .IR s2 .
67 .SH ATTRIBUTES
68 For an explanation of the terms used in this section, see
69 .BR attributes (7).
70 .TS
71 allbox;
72 lbx lb lb
73 l l l.
74 Interface Attribute Value
75 T{
76 .na
77 .nh
78 .BR strverscmp ()
79 T} Thread safety MT-Safe
80 .TE
81 .\" FIXME: The marking is different from that in the glibc manual,
82 .\" which has:
83 .\"
84 .\" strverscmp: MT-Safe locale
85 .\"
86 .\" glibc manual says strverscmp should have marking locale because it calls
87 .\" isdigit() multiple times and isdigit() uses locale variable.
88 .\" But isdigit() has two implementations. With different compiling conditions,
89 .\" we may call isdigit() in macro, then strverscmp() should not have locale
90 .\" problem.
91 .SH STANDARDS
92 GNU.
93 .SH EXAMPLES
94 The program below can be used to demonstrate the behavior of
95 .BR strverscmp ().
96 It uses
97 .BR strverscmp ()
98 to compare the two strings given as its command-line arguments.
99 An example of its use is the following:
100 .P
101 .in +4n
102 .EX
103 $ \fB./a.out jan1 jan10\fP
104 jan1 < jan10
105 .EE
106 .in
107 .SS Program source
108 \&
109 .\" SRC BEGIN (strverscmp.c)
110 .EX
111 #define _GNU_SOURCE
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 \&
116 int
117 main(int argc, char *argv[])
118 {
119 int res;
120 \&
121 if (argc != 3) {
122 fprintf(stderr, "Usage: %s <string1> <string2>\[rs]n", argv[0]);
123 exit(EXIT_FAILURE);
124 }
125 \&
126 res = strverscmp(argv[1], argv[2]);
127 \&
128 printf("%s %s %s\[rs]n", argv[1],
129 (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);
130 \&
131 exit(EXIT_SUCCESS);
132 }
133 .EE
134 .\" SRC END
135 .SH SEE ALSO
136 .BR rename (1),
137 .BR strcasecmp (3),
138 .BR strcmp (3),
139 .BR strcoll (3)