]>
git.saurik.com Git - apple/libresolv.git/blob - ns_ttl.c
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
20 static const char rcsid
[] = "$Id: ns_ttl.c,v 1.1 2006/03/01 19:01:37 majka Exp $";
27 #include "port_before.h"
30 #include <arpa/nameser.h>
38 #include "port_after.h"
42 # define SPRINTF(x) strlen(sprintf/**/x)
44 # define SPRINTF(x) ((size_t)sprintf x)
49 static int fmt1(int t
, char s
, char **buf
, size_t *buflen
);
53 #define T(x) if ((x) < 0) return (-1); else (void)NULL
58 ns_format_ttl(u_long src
, char *dst
, size_t dstlen
) {
60 int secs
, mins
, hours
, days
, weeks
, x
;
63 secs
= src
% 60; src
/= 60;
64 mins
= src
% 60; src
/= 60;
65 hours
= src
% 24; src
/= 24;
66 days
= src
% 7; src
/= 7;
71 T(fmt1(weeks
, 'W', &dst
, &dstlen
));
75 T(fmt1(days
, 'D', &dst
, &dstlen
));
79 T(fmt1(hours
, 'H', &dst
, &dstlen
));
83 T(fmt1(mins
, 'M', &dst
, &dstlen
));
86 if (secs
|| !(weeks
|| days
|| hours
|| mins
)) {
87 T(fmt1(secs
, 'S', &dst
, &dstlen
));
94 for (p
= odst
; (ch
= *p
) != '\0'; p
++)
95 if (isascii(ch
) && isupper(ch
))
103 ns_parse_ttl(const char *src
, u_long
*dst
) {
105 int ch
, digits
, dirty
;
111 while ((ch
= *src
++) != '\0') {
112 if (!isascii(ch
) || !isprint(ch
))
130 default: goto einval
;
154 fmt1(int t
, char s
, char **buf
, size_t *buflen
) {
158 len
= SPRINTF((tmp
, "%d%c", t
, s
));
159 if (len
+ 1 > *buflen
)