2 * Copyright (c) 1996 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
19 static char rcsid[] = "$FreeBSD: src/lib/libc/net/ns_ttl.c,v 1.2 1999/08/28 00:00:15 peter Exp $";
24 #include <arpa/nameser.h>
31 #define SPRINTF(x) ((size_t)sprintf x)
35 static int fmt1(int t, char s, char **buf, size_t *buflen);
39 #define T(x) if ((x) < 0) return (-1); else (void)NULL
44 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
46 int secs, mins, hours, days, weeks, x;
49 secs = src % 60; src /= 60;
50 mins = src % 60; src /= 60;
51 hours = src % 24; src /= 24;
52 days = src % 7; src /= 7;
57 T(fmt1(weeks, 'W', &dst, &dstlen));
61 T(fmt1(days, 'D', &dst, &dstlen));
65 T(fmt1(hours, 'H', &dst, &dstlen));
69 T(fmt1(mins, 'M', &dst, &dstlen));
72 if (secs || !(weeks || days || hours || mins)) {
73 T(fmt1(secs, 'S', &dst, &dstlen));
80 for (p = odst; (ch = *p) != '\0'; p++)
81 if (isascii(ch) && isupper(ch))
89 ns_parse_ttl(const char *src, u_long *dst) {
91 int ch, digits, dirty;
97 while ((ch = *src++) != '\0') {
98 if (!isascii(ch) || !isprint(ch))
116 default: goto einval;
140 fmt1(int t, char s, char **buf, size_t *buflen) {
144 len = SPRINTF((tmp, "%d%c", t, s));
145 if (len + 1 > *buflen)