Merge from vendor branch BIND:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / nameser / ns_ttl.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
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 ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC 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
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_ttl.c,v 1.1.2.1 2004/03/09 09:17:37 marka Exp $";
20 #endif
21
22 /* Import. */
23
24 #include "port_before.h"
25
26 #include <arpa/nameser.h>
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "port_after.h"
34
35 #ifdef SPRINTF_CHAR
36 # define SPRINTF(x) strlen(sprintf/**/x)
37 #else
38 # define SPRINTF(x) ((size_t)sprintf x)
39 #endif
40
41 /* Forward. */
42
43 static int      fmt1(int t, char s, char **buf, size_t *buflen);
44
45 /* Macros. */
46
47 #define T(x) if ((x) < 0) return (-1); else (void)NULL
48
49 /* Public. */
50
51 int
52 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
53         char *odst = dst;
54         int secs, mins, hours, days, weeks, x;
55         char *p;
56
57         secs = src % 60;   src /= 60;
58         mins = src % 60;   src /= 60;
59         hours = src % 24;  src /= 24;
60         days = src % 7;    src /= 7;
61         weeks = src;       src = 0;
62
63         x = 0;
64         if (weeks) {
65                 T(fmt1(weeks, 'W', &dst, &dstlen));
66                 x++;
67         }
68         if (days) {
69                 T(fmt1(days, 'D', &dst, &dstlen));
70                 x++;
71         }
72         if (hours) {
73                 T(fmt1(hours, 'H', &dst, &dstlen));
74                 x++;
75         }
76         if (mins) {
77                 T(fmt1(mins, 'M', &dst, &dstlen));
78                 x++;
79         }
80         if (secs || !(weeks || days || hours || mins)) {
81                 T(fmt1(secs, 'S', &dst, &dstlen));
82                 x++;
83         }
84
85         if (x > 1) {
86                 int ch;
87
88                 for (p = odst; (ch = *p) != '\0'; p++)
89                         if (isascii(ch) && isupper(ch))
90                                 *p = tolower(ch);
91         }
92
93         return (dst - odst);
94 }
95
96 int
97 ns_parse_ttl(const char *src, u_long *dst) {
98         u_long ttl, tmp;
99         int ch, digits, dirty;
100
101         ttl = 0;
102         tmp = 0;
103         digits = 0;
104         dirty = 0;
105         while ((ch = *src++) != '\0') {
106                 if (!isascii(ch) || !isprint(ch))
107                         goto einval;
108                 if (isdigit(ch)) {
109                         tmp *= 10;
110                         tmp += (ch - '0');
111                         digits++;
112                         continue;
113                 }
114                 if (digits == 0)
115                         goto einval;
116                 if (islower(ch))
117                         ch = toupper(ch);
118                 switch (ch) {
119                 case 'W':  tmp *= 7;
120                 case 'D':  tmp *= 24;
121                 case 'H':  tmp *= 60;
122                 case 'M':  tmp *= 60;
123                 case 'S':  break;
124                 default:   goto einval;
125                 }
126                 ttl += tmp;
127                 tmp = 0;
128                 digits = 0;
129                 dirty = 1;
130         }
131         if (digits > 0) {
132                 if (dirty)
133                         goto einval;
134                 else
135                         ttl += tmp;
136         }
137         *dst = ttl;
138         return (0);
139
140  einval:
141         errno = EINVAL;
142         return (-1);
143 }
144
145 /* Private. */
146
147 static int
148 fmt1(int t, char s, char **buf, size_t *buflen) {
149         char tmp[50];
150         size_t len;
151
152         len = SPRINTF((tmp, "%d%c", t, s));
153         if (len + 1 > *buflen)
154                 return (-1);
155         strcpy(*buf, tmp);
156         *buf += len;
157         *buflen -= len;
158         return (0);
159 }