]> git.proxmox.com Git - mirror_frr.git/blame - lib/defaults.c
lib: add frr_version_cmp()
[mirror_frr.git] / lib / defaults.c
CommitLineData
96673e06
DL
1/*
2 * FRR switchable defaults.
3 * Copyright (c) 2017-2019 David Lamparter, for NetDEF, Inc.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <zebra.h>
19
20#include "defaults.h"
21#include "libfrr.h"
22#include "version.h"
23
24static int version_value(int ch)
25{
26 /* non-ASCII shouldn't happen */
27 if (ch < 0 || ch >= 128)
28 return 2;
29
30 /* ~foo sorts older than nothing */
31 if (ch == '~')
32 return 0;
33 if (ch == '\0')
34 return 1;
35 if (isalpha(ch))
36 return 0x100 + tolower(ch);
37
38 /* punctuation and digits (and everything else) */
39 return 0x200 + ch;
40}
41
42int frr_version_cmp(const char *aa, const char *bb)
43{
44 const char *apos = aa, *bpos = bb;
45
46 /* || is correct, we won't scan past the end of a string since that
47 * doesn't compare equal to anything else */
48 while (apos[0] || bpos[0]) {
49 if (isdigit((unsigned char)apos[0]) &&
50 isdigit((unsigned char)bpos[0])) {
51 unsigned long av, bv;
52 char *aend = NULL, *bend = NULL;
53
54 av = strtoul(apos, &aend, 10);
55 bv = strtoul(bpos, &bend, 10);
56 if (av < bv)
57 return -1;
58 if (av > bv)
59 return 1;
60
61 apos = aend;
62 bpos = bend;
63 continue;
64 }
65
66 int a = version_value(*apos++);
67 int b = version_value(*bpos++);
68
69 if (a < b)
70 return -1;
71 if (a > b)
72 return 1;
73 }
74 return 0;
75}