Update.
[glibc.git] / resolv / ns_ttl.c
blob6be2b0d7a75be4f25c05fecd04d2b0a6a93903c0
1 /*
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #ifndef lint
19 static const char rcsid[] = "$Id$";
20 #endif
22 /* Import. */
23 #include <arpa/nameser.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
30 #ifdef SPRINTF_CHAR
31 # define SPRINTF(x) strlen(sprintf/**/x)
32 #else
33 # define SPRINTF(x) ((size_t)sprintf x)
34 #endif
36 /* Forward. */
38 static int fmt1(int t, char s, char **buf, size_t *buflen);
40 /* Macros. */
42 #define T(x) if ((x) < 0) return (-1); else (void)NULL
44 /* Public. */
46 int
47 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
48 char *odst = dst;
49 int secs, mins, hours, days, weeks, x;
50 char *p;
52 secs = src % 60; src /= 60;
53 mins = src % 60; src /= 60;
54 hours = src % 24; src /= 24;
55 days = src % 7; src /= 7;
56 weeks = src; src = 0;
58 x = 0;
59 if (weeks) {
60 T(fmt1(weeks, 'W', &dst, &dstlen));
61 x++;
63 if (days) {
64 T(fmt1(days, 'D', &dst, &dstlen));
65 x++;
67 if (hours) {
68 T(fmt1(hours, 'H', &dst, &dstlen));
69 x++;
71 if (mins) {
72 T(fmt1(mins, 'M', &dst, &dstlen));
73 x++;
75 if (secs || !(weeks || days || hours || mins)) {
76 T(fmt1(secs, 'S', &dst, &dstlen));
77 x++;
80 if (x > 1) {
81 int ch;
83 for (p = odst; (ch = *p) != '\0'; p++)
84 if (isascii(ch) && isupper(ch))
85 *p = tolower(ch);
88 return (dst - odst);
91 int
92 ns_parse_ttl(const char *src, u_long *dst) {
93 u_long ttl, tmp;
94 int ch, digits, dirty;
96 ttl = 0;
97 tmp = 0;
98 digits = 0;
99 dirty = 0;
100 while ((ch = *src++) != '\0') {
101 if (!isascii(ch) || !isprint(ch))
102 goto einval;
103 if (isdigit(ch)) {
104 tmp *= 10;
105 tmp += (ch - '0');
106 digits++;
107 continue;
109 if (digits == 0)
110 goto einval;
111 if (islower(ch))
112 ch = toupper(ch);
113 switch (ch) {
114 case 'W': tmp *= 7;
115 case 'D': tmp *= 24;
116 case 'H': tmp *= 60;
117 case 'M': tmp *= 60;
118 case 'S': break;
119 default: goto einval;
121 ttl += tmp;
122 tmp = 0;
123 digits = 0;
124 dirty = 1;
126 if (digits > 0) {
127 if (dirty)
128 goto einval;
129 else
130 ttl += tmp;
132 *dst = ttl;
133 return (0);
135 einval:
136 __set_errno (EINVAL);
137 return (-1);
140 /* Private. */
142 static int
143 fmt1(int t, char s, char **buf, size_t *buflen) {
144 char tmp[50];
145 size_t len;
147 len = SPRINTF((tmp, "%d%c", t, s));
148 if (len + 1 > *buflen)
149 return (-1);
150 strcpy(*buf, tmp);
151 *buf += len;
152 *buflen -= len;
153 return (0);