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
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 $
23 #include <arpa/nameser.h>
30 #define SPRINTF(x) ((size_t)sprintf x)
34 static int fmt1(int t, char s, char **buf, size_t *buflen);
38 #define T(x) if ((x) < 0) return (-1); else (void)NULL
43 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
45 int secs, mins, hours, days, weeks, x;
48 secs = src % 60; src /= 60;
49 mins = src % 60; src /= 60;
50 hours = src % 24; src /= 24;
51 days = src % 7; src /= 7;
56 T(fmt1(weeks, 'W', &dst, &dstlen));
60 T(fmt1(days, 'D', &dst, &dstlen));
64 T(fmt1(hours, 'H', &dst, &dstlen));
68 T(fmt1(mins, 'M', &dst, &dstlen));
71 if (secs || !(weeks || days || hours || mins)) {
72 T(fmt1(secs, 'S', &dst, &dstlen));
79 for (p = odst; (ch = *p) != '\0'; p++)
80 if (isascii(ch) && isupper(ch))
88 ns_parse_ttl(const char *src, u_long *dst) {
90 int ch, digits, dirty;
96 while ((ch = *src++) != '\0') {
97 if (!isascii(ch) || !isprint(ch))
115 default: goto einval;
139 fmt1(int t, char s, char **buf, size_t *buflen) {
143 len = SPRINTF((tmp, "%d%c", t, s));
144 if (len + 1 > *buflen)