1 /*****************************************************************************
2 * RRDtool 1.4.2 Copyright by Tobi Oetiker, 1997-2009
3 * This code is stolen from rateup (mrtg-2.x) by Dave Rand
4 *****************************************************************************
5 * diff calculate the difference between two very long integers available as
7 *****************************************************************************
10 * Revision 1.4 2003/03/10 00:30:34 oetiker
11 * handle cases with two negative numbers
12 * -- Sasha Mikheev <sasha@avalon-net.co.il>
14 * Revision 1.3 2002/04/01 18:31:22 oetiker
15 * "!" takes a higher preference than "||" this means rrd_update N:: would
16 * segfault -- Oliver Cook <ollie@uk.clara.net>
18 * Revision 1.2 2002/02/01 20:34:49 oetiker
19 * fixed version number and date/time
21 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
24 * Revision 1.1 1998/10/08 18:21:45 oetiker
27 * Revision 1.3 1998/02/06 21:10:52 oetiker
28 * removed max define .. it is now in rrd_tool.h
30 * Revision 1.2 1997/12/07 20:38:03 oetiker
33 * Revision 1.1 1997/11/28 23:31:59 oetiker
36 *****************************************************************************/
45 char res[LAST_DS_LEN + 1], *a1, *b1, *r1, *fix;
47 char a_neg = 0, b_neg = 0;
50 while (!(isdigit((int) *a) || *a == 0)) {
56 while (isdigit((int) *fix))
58 *fix = 0; /* maybe there is some non digit data in the string */
59 while (!(isdigit((int) *b) || *b == 0)) {
65 while (isdigit((int) *fix))
67 *fix = 0; /* maybe there is some non digit data in the string */
68 if (!isdigit((int) *a) || !isdigit((int) *b))
70 if (a_neg + b_neg == 1) /* can not handle numbers with different signs yet */
72 a1 = &a[strlen(a) - 1];
73 m = max(strlen(a), strlen(b));
75 return DNAN; /* result string too short */
78 for (b1 = res; b1 <= r1; b1++)
80 b1 = &b[strlen(b) - 1];
81 r1[1] = 0; /* Null terminate result */
83 for (x = 0; x < m; x++) {
84 if (a1 >= a && b1 >= b) {
85 *r1 = ((*a1 - c) - *b1) + '0';
89 *r1 = ('0' - *b1 - c) + '0';
94 } else if (*r1 > '9') { /* 0 - 10 */
106 for (x = 0; isdigit((int) *r1) && x < m; x++, r1--) {
107 *r1 = ('9' - *r1 + c) + '0';
119 if (a_neg + b_neg == 2) /* both are negatives, reverse sign */