1STRVERSCMP(3)              Linux Programmer's Manual             STRVERSCMP(3)
2
3
4

NAME

6       strverscmp - compare two version strings
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <string.h>
11
12       int strverscmp(const char *s1, const char *s2);
13

DESCRIPTION

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
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
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

RETURN VALUE

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

ATTRIBUTES

44       For  an  explanation  of  the  terms  used   in   this   section,   see
45       attributes(7).
46
47       ┌─────────────┬───────────────┬─────────┐
48Interface    Attribute     Value   
49       ├─────────────┼───────────────┼─────────┤
50strverscmp() │ Thread safety │ MT-Safe │
51       └─────────────┴───────────────┴─────────┘

CONFORMING TO

53       This function is a GNU extension.
54

EXAMPLE

56       The  program  below  can be used to demonstrate the behavior of strver‐
57       scmp().  It uses strverscmp() to compare the two strings given  as  its
58       command-line arguments.  An example of its use is the following:
59
60           $ ./a.out jan1 jan10
61           jan1 < jan10
62
63   Program source
64
65       #define _GNU_SOURCE
66       #include <string.h>
67       #include <stdio.h>
68       #include <stdlib.h>
69
70       int
71       main(int argc, char *argv[])
72       {
73           int res;
74
75           if (argc != 3) {
76               fprintf(stderr, "Usage: %s <string1> <string2>\n", argv[0]);
77               exit(EXIT_FAILURE);
78           }
79
80           res = strverscmp(argv[1], argv[2]);
81
82           printf("%s %s %s\n", argv[1],
83                   (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);
84
85           exit(EXIT_SUCCESS);
86       }
87

SEE ALSO

89       rename(1), strcasecmp(3), strcmp(3), strcoll(3)
90

COLOPHON

92       This  page  is  part of release 5.04 of the Linux man-pages project.  A
93       description of the project, information about reporting bugs,  and  the
94       latest     version     of     this    page,    can    be    found    at
95       https://www.kernel.org/doc/man-pages/.
96
97
98
99GNU                               2019-03-06                     STRVERSCMP(3)
Impressum