1strverscmp(3)              Library Functions Manual              strverscmp(3)
2
3
4

NAME

6       strverscmp - compare two version strings
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

18       Often  one  has  files  jan1, jan2, ..., jan9, jan10, ...  and it feels
19       wrong when ls(1) orders them jan1, jan10, ..., jan2, ..., jan9.  In or‐
20       der  to  rectify  this, GNU introduced the -v option to ls(1), which is
21       implemented using versionsort(3), which again uses strverscmp().
22
23       Thus, the task of strverscmp() is to compare two strings and  find  the
24       "right"  order,  while  strcmp(3)  finds  only the lexicographic order.
25       This function does not use the locale category LC_COLLATE, so is  meant
26       mostly for situations where the strings are expected to be in ASCII.
27
28       What  this  function does is the following.  If both strings are equal,
29       return 0.  Otherwise, find the position  between  two  bytes  with  the
30       property that before it both strings are equal, while directly after it
31       there is a difference.  Find the largest consecutive digit strings con‐
32       taining  (or  starting at, or ending at) this position.  If one or both
33       of these is empty, then return what strcmp(3) would have returned  (nu‐
34       merical  ordering  of  byte  values).   Otherwise,  compare  both digit
35       strings numerically, where digit strings with one or more leading zeros
36       are  interpreted  as  if they have a decimal point in front (so that in
37       particular digit strings with more  leading  zeros  come  before  digit
38       strings  with fewer leading zeros).  Thus, the ordering is 000, 00, 01,
39       010, 09, 0, 1, 9, 10.
40

RETURN VALUE

42       The strverscmp() function returns an integer less than,  equal  to,  or
43       greater  than  zero  if  s1 is found, respectively, to be earlier than,
44       equal to, or later than s2.
45

ATTRIBUTES

47       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
48       tributes(7).
49
50       ┌────────────────────────────────────────────┬───────────────┬─────────┐
51Interface                                   Attribute     Value   
52       ├────────────────────────────────────────────┼───────────────┼─────────┤
53strverscmp()                                │ Thread safety │ MT-Safe │
54       └────────────────────────────────────────────┴───────────────┴─────────┘
55

STANDARDS

57       GNU.
58

EXAMPLES

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

SEE ALSO

93       rename(1), strcasecmp(3), strcmp(3), strcoll(3)
94
95
96
97Linux man-pages 6.05              2023-07-20                     strverscmp(3)
Impressum