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
18 #if !defined(_LIBC) && !defined(lint)
19 static const char rcsid
[] = "$BINDId: ns_ttl.c,v 8.8 1999/10/13 16:39:36 vixie Exp $";
24 #include <arpa/nameser.h>
32 # define SPRINTF(x) strlen(sprintf/**/x)
34 # define SPRINTF(x) ((size_t)sprintf x)
39 static int fmt1(int t
, char s
, char **buf
, size_t *buflen
);
43 #define T(x) if ((x) < 0) return (-1); else (void)NULL
48 ns_format_ttl(u_long src
, char *dst
, size_t dstlen
) {
50 int secs
, mins
, hours
, days
, weeks
, x
;
53 secs
= src
% 60; src
/= 60;
54 mins
= src
% 60; src
/= 60;
55 hours
= src
% 24; src
/= 24;
56 days
= src
% 7; src
/= 7;
61 T(fmt1(weeks
, 'W', &dst
, &dstlen
));
65 T(fmt1(days
, 'D', &dst
, &dstlen
));
69 T(fmt1(hours
, 'H', &dst
, &dstlen
));
73 T(fmt1(mins
, 'M', &dst
, &dstlen
));
76 if (secs
|| !(weeks
|| days
|| hours
|| mins
)) {
77 T(fmt1(secs
, 'S', &dst
, &dstlen
));
84 for (p
= odst
; (ch
= *p
) != '\0'; p
++)
85 if (isascii(ch
) && isupper(ch
))
93 ns_parse_ttl(const char *src
, u_long
*dst
) {
95 int ch
, digits
, dirty
;
101 while ((ch
= *src
++) != '\0') {
102 if (!isascii(ch
) || !isprint(ch
))
120 default: goto einval
;
137 __set_errno (EINVAL
);
144 fmt1(int t
, char s
, char **buf
, size_t *buflen
) {
148 len
= SPRINTF((tmp
, "%d%c", t
, s
));
149 if (len
+ 1 > *buflen
)