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
17 order 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 prop‐
27 erty 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
31 (numerical 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 This function is a GNU extension.
45
47 rename(1), strcasecmp(3), strcmp(3), strcoll(3)
48
50 This page is part of release 3.53 of the Linux man-pages project. A
51 description of the project, and information about reporting bugs, can
52 be found at http://www.kernel.org/doc/man-pages/.
53
54
55
56GNU 2001-12-19 STRVERSCMP(3)