chiark / gitweb /
Merge branch 'running' of login.chiark.greenend.org.uk:public-git/inn-innduct
[innduct.git] / lib / sequence.c
1 /*  $Id: sequence.c 4871 2001-07-09 08:09:58Z alexk $
2 **
3 **  Sequence space arithmetic routines.
4 **
5 **  This is a set of routines for implementing so called sequence
6 **  space arithmetic (typically used for DNS serial numbers). The
7 **  implementation here is taken from RFC 1982.
8 */
9
10 #include "config.h"
11 #include "clibrary.h"
12 #include <limits.h>
13 #include "inn/sequence.h"
14
15
16 /*
17 **  compare two unsigned long numbers using sequence space arithmetic
18 **
19 **    returns:
20 **     0 - i1 = i2
21 **    -1 - i1 < i2
22 **     1 - i1 > i2
23 **     INT_MAX - undefined
24 */
25 int
26 seq_lcompare(unsigned long i1, unsigned long i2)
27 {
28     if (i1 == i2)
29         return 0;
30     else if ((i1 < i2 && i2 - i1 < (1 + ULONG_MAX / 2)) ||
31              (i1 > i2 && i1 - i2 > (1 + ULONG_MAX / 2)))
32         return -1;
33     else if ((i1 < i2 && i2 - i1 > (1 + ULONG_MAX / 2)) ||
34              (i1 > i2 && i1 - i2 < (1 + ULONG_MAX / 2)))
35         return 1;
36     return INT_MAX;
37 }