1 /* $NetBSD: ns_ttl.c,v 1.7 2009/04/12 17:07:17 christos Exp $ */
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.
20 #include <sys/cdefs.h>
23 static const char rcsid
[] = "Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp";
25 __RCSID("$NetBSD: ns_ttl.c,v 1.7 2009/04/12 17:07:17 christos Exp $");
31 #include "port_before.h"
33 #include <arpa/nameser.h>
40 #include "port_after.h"
43 # define SPRINTF(x) strlen(sprintf/**/x)
45 # define SPRINTF(x) ((size_t)sprintf x)
50 static int fmt1(int t
, char s
, char **buf
, size_t *buflen
);
54 #define T(x) if ((x) < 0) return (-1)
59 ns_format_ttl(u_long src
, char *dst
, size_t dstlen
) {
61 int secs
, mins
, hours
, days
, weeks
, x
;
64 secs
= src
% 60; src
/= 60;
65 mins
= src
% 60; src
/= 60;
66 hours
= src
% 24; src
/= 24;
67 days
= src
% 7; src
/= 7;
72 T(fmt1(weeks
, 'W', &dst
, &dstlen
));
76 T(fmt1(days
, 'D', &dst
, &dstlen
));
80 T(fmt1(hours
, 'H', &dst
, &dstlen
));
84 T(fmt1(mins
, 'M', &dst
, &dstlen
));
87 if (secs
|| !(weeks
|| days
|| hours
|| mins
)) {
88 T(fmt1(secs
, 'S', &dst
, &dstlen
));
95 for (p
= odst
; (ch
= *p
) != '\0'; p
++)
96 if (isascii(ch
) && isupper(ch
))
105 ns_parse_ttl(const char *src
, u_long
*dst
) {
107 int ch
, digits
, dirty
;
113 while ((ch
= *src
++) != '\0') {
114 if (!isascii(ch
) || !isprint(ch
))
127 case 'W': tmp
*= 7; /*FALLTHROUGH*/
128 case 'D': tmp
*= 24; /*FALLTHROUGH*/
129 case 'H': tmp
*= 60; /*FALLTHROUGH*/
130 case 'M': tmp
*= 60; /*FALLTHROUGH*/
132 default: goto einval
;
158 fmt1(int t
, char s
, char **buf
, size_t *buflen
) {
162 len
= SPRINTF((tmp
, "%d%c", t
, s
));
163 if (len
+ 1 > *buflen
)