BIND - Update BIND to 9.5.2
[dragonfly.git] / contrib / bind-9.5.2 / lib / bind / nameser / ns_ttl.c
blob69c2f83f57756bf1158bdee6f67af655f3f79699
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp $";
20 #endif
22 /* Import. */
24 #include "port_before.h"
26 #include <arpa/nameser.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
33 #include "port_after.h"
35 #ifdef SPRINTF_CHAR
36 # define SPRINTF(x) strlen(sprintf/**/x)
37 #else
38 # define SPRINTF(x) ((size_t)sprintf x)
39 #endif
41 /* Forward. */
43 static int fmt1(int t, char s, char **buf, size_t *buflen);
45 /* Macros. */
47 #define T(x) if ((x) < 0) return (-1); else (void)NULL
49 /* Public. */
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;
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;
63 x = 0;
64 if (weeks) {
65 T(fmt1(weeks, 'W', &dst, &dstlen));
66 x++;
68 if (days) {
69 T(fmt1(days, 'D', &dst, &dstlen));
70 x++;
72 if (hours) {
73 T(fmt1(hours, 'H', &dst, &dstlen));
74 x++;
76 if (mins) {
77 T(fmt1(mins, 'M', &dst, &dstlen));
78 x++;
80 if (secs || !(weeks || days || hours || mins)) {
81 T(fmt1(secs, 'S', &dst, &dstlen));
82 x++;
85 if (x > 1) {
86 int ch;
88 for (p = odst; (ch = *p) != '\0'; p++)
89 if (isascii(ch) && isupper(ch))
90 *p = tolower(ch);
93 return (dst - odst);
96 int
97 ns_parse_ttl(const char *src, u_long *dst) {
98 u_long ttl, tmp;
99 int ch, digits, dirty;
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;
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;
126 ttl += tmp;
127 tmp = 0;
128 digits = 0;
129 dirty = 1;
131 if (digits > 0) {
132 if (dirty)
133 goto einval;
134 else
135 ttl += tmp;
136 } else if (!dirty)
137 goto einval;
138 *dst = ttl;
139 return (0);
141 einval:
142 errno = EINVAL;
143 return (-1);
146 /* Private. */
148 static int
149 fmt1(int t, char s, char **buf, size_t *buflen) {
150 char tmp[50];
151 size_t len;
153 len = SPRINTF((tmp, "%d%c", t, s));
154 if (len + 1 > *buflen)
155 return (-1);
156 strcpy(*buf, tmp);
157 *buf += len;
158 *buflen -= len;
159 return (0);
162 /*! \file */