Merge from vendor branch GCC:
[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  * $FreeBSD: src/lib/libc/net/ns_ttl.c,v 1.2 1999/08/28 00:00:15 peter Exp $
18  * $DragonFly: src/lib/libc/net/ns_ttl.c,v 1.3 2005/11/13 02:04:47 swildner Exp $
19  */
20
21 /* Import. */
22
23 #include <arpa/nameser.h>
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #define SPRINTF(x) ((size_t)sprintf x)
31
32 /* Forward. */
33
34 static int      fmt1(int t, char s, char **buf, size_t *buflen);
35
36 /* Macros. */
37
38 #define T(x) if ((x) < 0) return (-1); else (void)NULL
39
40 /* Public. */
41
42 int
43 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
44         char *odst = dst;
45         int secs, mins, hours, days, weeks, x;
46         char *p;
47
48         secs = src % 60;   src /= 60;
49         mins = src % 60;   src /= 60;
50         hours = src % 24;  src /= 24;
51         days = src % 7;    src /= 7;
52         weeks = src;       src = 0;
53
54         x = 0;
55         if (weeks) {
56                 T(fmt1(weeks, 'W', &dst, &dstlen));
57                 x++;
58         }
59         if (days) {
60                 T(fmt1(days, 'D', &dst, &dstlen));
61                 x++;
62         }
63         if (hours) {
64                 T(fmt1(hours, 'H', &dst, &dstlen));
65                 x++;
66         }
67         if (mins) {
68                 T(fmt1(mins, 'M', &dst, &dstlen));
69                 x++;
70         }
71         if (secs || !(weeks || days || hours || mins)) {
72                 T(fmt1(secs, 'S', &dst, &dstlen));
73                 x++;
74         }
75
76         if (x > 1) {
77                 int ch;
78
79                 for (p = odst; (ch = *p) != '\0'; p++)
80                         if (isascii(ch) && isupper(ch))
81                                 *p = tolower(ch);
82         }
83
84         return (dst - odst);
85 }
86
87 int
88 ns_parse_ttl(const char *src, u_long *dst) {
89         u_long ttl, tmp;
90         int ch, digits, dirty;
91
92         ttl = 0;
93         tmp = 0;
94         digits = 0;
95         dirty = 0;
96         while ((ch = *src++) != '\0') {
97                 if (!isascii(ch) || !isprint(ch))
98                         goto einval;
99                 if (isdigit(ch)) {
100                         tmp *= 10;
101                         tmp += (ch - '0');
102                         digits++;
103                         continue;
104                 }
105                 if (digits == 0)
106                         goto einval;
107                 if (islower(ch))
108                         ch = toupper(ch);
109                 switch (ch) {
110                 case 'W':  tmp *= 7;
111                 case 'D':  tmp *= 24;
112                 case 'H':  tmp *= 60;
113                 case 'M':  tmp *= 60;
114                 case 'S':  break;
115                 default:   goto einval;
116                 }
117                 ttl += tmp;
118                 tmp = 0;
119                 digits = 0;
120                 dirty = 1;
121         }
122         if (digits > 0) {
123                 if (dirty)
124                         goto einval;
125                 else
126                         ttl += tmp;
127         }
128         *dst = ttl;
129         return (0);
130
131  einval:
132         errno = EINVAL;
133         return (-1);
134 }
135
136 /* Private. */
137
138 static int
139 fmt1(int t, char s, char **buf, size_t *buflen) {
140         char tmp[50];
141         size_t len;
142
143         len = SPRINTF((tmp, "%d%c", t, s));
144         if (len + 1 > *buflen)
145                 return (-1);
146         strcpy(*buf, tmp);
147         *buf += len;
148         *buflen -= len;
149         return (0);
150 }