1STRVERSCMP(3) Linux Programmer's Manual STRVERSCMP(3)
2
3
4
6 strverscmp - compare two version strings
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <string.h>
11
12 int strverscmp(const char *s1, const char *s2);
13
15 Often one has files jan1, jan2, ..., jan9, jan10, ... and it feels
16 wrong when ls(1) orders them jan1, jan10, ..., jan2, ..., jan9. In or‐
17 der to rectify this, GNU introduced the -v option to ls(1), which is
18 implemented using versionsort(3), which again uses strverscmp().
19
20 Thus, the task of strverscmp() is to compare two strings and find the
21 "right" order, while strcmp(3) finds only the lexicographic order.
22 This function does not use the locale category LC_COLLATE, so is meant
23 mostly for situations where the strings are expected to be in ASCII.
24
25 What this function does is the following. If both strings are equal,
26 return 0. Otherwise, find the position between two bytes with the
27 property that before it both strings are equal, while directly after it
28 there is a difference. Find the largest consecutive digit strings con‐
29 taining (or starting at, or ending at) this position. If one or both
30 of these is empty, then return what strcmp(3) would have returned (nu‐
31 merical ordering of byte values). Otherwise, compare both digit
32 strings numerically, where digit strings with one or more leading zeros
33 are interpreted as if they have a decimal point in front (so that in
34 particular digit strings with more leading zeros come before digit
35 strings with fewer leading zeros). Thus, the ordering is 000, 00, 01,
36 010, 09, 0, 1, 9, 10.
37
39 The strverscmp() function returns an integer less than, equal to, or
40 greater than zero if s1 is found, respectively, to be earlier than,
41 equal to, or later than s2.
42
44 For an explanation of the terms used in this section, see at‐
45 tributes(7).
46
47 ┌────────────────────────────────────────────┬───────────────┬─────────┐
48 │Interface │ Attribute │ Value │
49 ├────────────────────────────────────────────┼───────────────┼─────────┤
50 │strverscmp() │ Thread safety │ MT-Safe │
51 └────────────────────────────────────────────┴───────────────┴─────────┘
52
54 This function is a GNU extension.
55
57 The program below can be used to demonstrate the behavior of strver‐
58 scmp(). It uses strverscmp() to compare the two strings given as its
59 command-line arguments. An example of its use is the following:
60
61 $ ./a.out jan1 jan10
62 jan1 < jan10
63
64 Program source
65
66 #define _GNU_SOURCE
67 #include <string.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70
71 int
72 main(int argc, char *argv[])
73 {
74 int res;
75
76 if (argc != 3) {
77 fprintf(stderr, "Usage: %s <string1> <string2>\n", argv[0]);
78 exit(EXIT_FAILURE);
79 }
80
81 res = strverscmp(argv[1], argv[2]);
82
83 printf("%s %s %s\n", argv[1],
84 (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);
85
86 exit(EXIT_SUCCESS);
87 }
88
90 rename(1), strcasecmp(3), strcmp(3), strcoll(3)
91
93 This page is part of release 5.13 of the Linux man-pages project. A
94 description of the project, information about reporting bugs, and the
95 latest version of this page, can be found at
96 https://www.kernel.org/doc/man-pages/.
97
98
99
100GNU 2021-03-22 STRVERSCMP(3)