1 /* -*- mode:C; c-file-style: "bsd" -*- */
3 * Copyright (c) 2012-2013 Yubico AB
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <ykpers-version.h>
37 /* From http://article.gmane.org/gmane.os.freebsd.devel.hackers/23606 */
39 my_strverscmp(const char *s1, const char *s2)
41 static const char *digits = "0123456789";
45 p1 = strcspn(s1, digits);
46 p2 = strcspn(s2, digits);
47 while (p1 == p2 && s1[p1] != '\0' && s2[p2] != '\0') {
48 /* Different prefix */
49 if ((ret = strncmp(s1, s2, p1)) != 0)
67 * If the common prefix for s1 and s2 consists only of zeros, then the
68 * "longer" number has to compare less. Otherwise the comparison needs
69 * to be numerical (just fallthrough). See
70 * http://refspecs.freestandards.org/LSB_2.0.1/LSB-generic/
71 * LSB-generic/baselib-strverscmp.html
73 while (*s1 == '0' && *s2 == '0') {
78 p1 = strspn(s1, digits);
79 p2 = strspn(s2, digits);
81 /* Catch empty strings */
82 if (p1 == 0 && p2 > 0)
84 else if (p2 == 0 && p1 > 0)
87 /* Prefixes are not same */
88 if (*s1 != *s2 && *s1 != '0' && *s2 != '0') {
95 ret = strncmp(s1, s2, p1);
97 ret = strncmp(s1, s2, p2);
103 p1 = strspn(s1, digits);
104 p2 = strspn(s2, digits);
110 else if ((ret = strncmp(s1, s2, p1)) != 0)
113 /* Numbers are equal or not present, try with next ones. */
116 p1 = strcspn(s1, digits);
117 p2 = strcspn(s2, digits);
120 return strcmp(s1, s2);
124 ykpers_check_version (const char *req_version)
126 if (!req_version || my_strverscmp (req_version, YKPERS_VERSION_STRING) <= 0)
127 return YKPERS_VERSION_STRING;