Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libc / net / ns_ttl.c
1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #ifndef lint
19 static char rcsid[] = "$FreeBSD: src/lib/libc/net/ns_ttl.c,v 1.2 1999/08/28 00:00:15 peter Exp $";
20 #endif
21
22 /* Import. */
23
24 #include <arpa/nameser.h>
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #define SPRINTF(x) ((size_t)sprintf x)
32
33 /* Forward. */
34
35 static int      fmt1(int t, char s, char **buf, size_t *buflen);
36
37 /* Macros. */
38
39 #define T(x) if ((x) < 0) return (-1); else (void)NULL
40
41 /* Public. */
42
43 int
44 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
45         char *odst = dst;
46         int secs, mins, hours, days, weeks, x;
47         char tmp[50], *p;
48
49         secs = src % 60;   src /= 60;
50         mins = src % 60;   src /= 60;
51         hours = src % 24;  src /= 24;
52         days = src % 7;    src /= 7;
53         weeks = src;       src = 0;
54
55         x = 0;
56         if (weeks) {
57                 T(fmt1(weeks, 'W', &dst, &dstlen));
58                 x++;
59         }
60         if (days) {
61                 T(fmt1(days, 'D', &dst, &dstlen));
62                 x++;
63         }
64         if (hours) {
65                 T(fmt1(hours, 'H', &dst, &dstlen));
66                 x++;
67         }
68         if (mins) {
69                 T(fmt1(mins, 'M', &dst, &dstlen));
70                 x++;
71         }
72         if (secs || !(weeks || days || hours || mins)) {
73                 T(fmt1(secs, 'S', &dst, &dstlen));
74                 x++;
75         }
76
77         if (x > 1) {
78                 int ch;
79
80                 for (p = odst; (ch = *p) != '\0'; p++)
81                         if (isascii(ch) && isupper(ch))
82                                 *p = tolower(ch);
83         }
84
85         return (dst - odst);
86 }
87
88 int
89 ns_parse_ttl(const char *src, u_long *dst) {
90         u_long ttl, tmp;
91         int ch, digits, dirty;
92
93         ttl = 0;
94         tmp = 0;
95         digits = 0;
96         dirty = 0;
97         while ((ch = *src++) != '\0') {
98                 if (!isascii(ch) || !isprint(ch))
99                         goto einval;
100                 if (isdigit(ch)) {
101                         tmp *= 10;
102                         tmp += (ch - '0');
103                         digits++;
104                         continue;
105                 }
106                 if (digits == 0)
107                         goto einval;
108                 if (islower(ch))
109                         ch = toupper(ch);
110                 switch (ch) {
111                 case 'W':  tmp *= 7;
112                 case 'D':  tmp *= 24;
113                 case 'H':  tmp *= 60;
114                 case 'M':  tmp *= 60;
115                 case 'S':  break;
116                 default:   goto einval;
117                 }
118                 ttl += tmp;
119                 tmp = 0;
120                 digits = 0;
121                 dirty = 1;
122         }
123         if (digits > 0) {
124                 if (dirty)
125                         goto einval;
126                 else
127                         ttl += tmp;
128         }
129         *dst = ttl;
130         return (0);
131
132  einval:
133         errno = EINVAL;
134         return (-1);
135 }
136
137 /* Private. */
138
139 static int
140 fmt1(int t, char s, char **buf, size_t *buflen) {
141         char tmp[50];
142         size_t len;
143
144         len = SPRINTF((tmp, "%d%c", t, s));
145         if (len + 1 > *buflen)
146                 return (-1);
147         strcpy(*buf, tmp);
148         *buf += len;
149         *buflen -= len;
150         return (0);
151 }