2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static const char rcsid[] = "$Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp $";
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
28 #include "port_before.h"
30 #include <arpa/nameser.h>
37 #include "port_after.h"
40 # define SPRINTF(x) strlen(sprintf/**/x)
42 # define SPRINTF(x) ((size_t)sprintf x)
47 static int fmt1(int t, char s, char **buf, size_t *buflen);
51 #define T(x) if ((x) < 0) return (-1); else (void)NULL
56 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
58 int secs, mins, hours, days, weeks, x;
61 secs = src % 60; src /= 60;
62 mins = src % 60; src /= 60;
63 hours = src % 24; src /= 24;
64 days = src % 7; src /= 7;
69 T(fmt1(weeks, 'W', &dst, &dstlen));
73 T(fmt1(days, 'D', &dst, &dstlen));
77 T(fmt1(hours, 'H', &dst, &dstlen));
81 T(fmt1(mins, 'M', &dst, &dstlen));
84 if (secs || !(weeks || days || hours || mins)) {
85 T(fmt1(secs, 'S', &dst, &dstlen));
92 for (p = odst; (ch = *p) != '\0'; p++)
93 if (isascii(ch) && isupper(ch))
101 ns_parse_ttl(const char *src, u_long *dst) {
103 int ch, digits, dirty;
109 while ((ch = *src++) != '\0') {
110 if (!isascii(ch) || !isprint(ch))
128 default: goto einval;
153 fmt1(int t, char s, char **buf, size_t *buflen) {
157 len = SPRINTF((tmp, "%d%c", t, s));
158 if (len + 1 > *buflen)