1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
3*7c478bd9Sstevel@tonic-gate  * All rights reserved.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*7c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*7c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate #ifndef lint
26*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: ns_ttl.c,v 8.8 1999/10/13 16:39:36 vixie Exp $";
27*7c478bd9Sstevel@tonic-gate #endif
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /* Import. */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include "port_before.h"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <ctype.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include "port_after.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
43*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
44*7c478bd9Sstevel@tonic-gate #else
45*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
46*7c478bd9Sstevel@tonic-gate #endif
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* Forward. */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static int	fmt1(int t, char s, char **buf, size_t *buflen);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /* Macros. */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #define T(x) if ((x) < 0) return (-1); else (void)NULL
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /* Public. */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate int
59*7c478bd9Sstevel@tonic-gate ns_format_ttl(u_long src, char *dst, size_t dstlen) {
60*7c478bd9Sstevel@tonic-gate 	char *odst = dst;
61*7c478bd9Sstevel@tonic-gate 	int secs, mins, hours, days, weeks, x;
62*7c478bd9Sstevel@tonic-gate 	char *p;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	secs = src % 60;   src /= 60;
65*7c478bd9Sstevel@tonic-gate 	mins = src % 60;   src /= 60;
66*7c478bd9Sstevel@tonic-gate 	hours = src % 24;  src /= 24;
67*7c478bd9Sstevel@tonic-gate 	days = src % 7;    src /= 7;
68*7c478bd9Sstevel@tonic-gate 	weeks = src;       src = 0;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	x = 0;
71*7c478bd9Sstevel@tonic-gate 	if (weeks) {
72*7c478bd9Sstevel@tonic-gate 		T(fmt1(weeks, 'W', &dst, &dstlen));
73*7c478bd9Sstevel@tonic-gate 		x++;
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	if (days) {
76*7c478bd9Sstevel@tonic-gate 		T(fmt1(days, 'D', &dst, &dstlen));
77*7c478bd9Sstevel@tonic-gate 		x++;
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	if (hours) {
80*7c478bd9Sstevel@tonic-gate 		T(fmt1(hours, 'H', &dst, &dstlen));
81*7c478bd9Sstevel@tonic-gate 		x++;
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	if (mins) {
84*7c478bd9Sstevel@tonic-gate 		T(fmt1(mins, 'M', &dst, &dstlen));
85*7c478bd9Sstevel@tonic-gate 		x++;
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 	if (secs || !(weeks || days || hours || mins)) {
88*7c478bd9Sstevel@tonic-gate 		T(fmt1(secs, 'S', &dst, &dstlen));
89*7c478bd9Sstevel@tonic-gate 		x++;
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (x > 1) {
93*7c478bd9Sstevel@tonic-gate 		int ch;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 		for (p = odst; (ch = *p) != '\0'; p++)
96*7c478bd9Sstevel@tonic-gate 			if (isascii(ch) && isupper(ch))
97*7c478bd9Sstevel@tonic-gate 				*p = tolower(ch);
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	return (dst - odst);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate int
104*7c478bd9Sstevel@tonic-gate ns_parse_ttl(const char *src, u_long *dst) {
105*7c478bd9Sstevel@tonic-gate 	u_long ttl, tmp;
106*7c478bd9Sstevel@tonic-gate 	int ch, digits, dirty;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	ttl = 0;
109*7c478bd9Sstevel@tonic-gate 	tmp = 0;
110*7c478bd9Sstevel@tonic-gate 	digits = 0;
111*7c478bd9Sstevel@tonic-gate 	dirty = 0;
112*7c478bd9Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
113*7c478bd9Sstevel@tonic-gate 		if (!isascii(ch) || !isprint(ch))
114*7c478bd9Sstevel@tonic-gate 			goto einval;
115*7c478bd9Sstevel@tonic-gate 		if (isdigit(ch)) {
116*7c478bd9Sstevel@tonic-gate 			tmp *= 10;
117*7c478bd9Sstevel@tonic-gate 			tmp += (ch - '0');
118*7c478bd9Sstevel@tonic-gate 			digits++;
119*7c478bd9Sstevel@tonic-gate 			continue;
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 		if (digits == 0)
122*7c478bd9Sstevel@tonic-gate 			goto einval;
123*7c478bd9Sstevel@tonic-gate 		if (islower(ch))
124*7c478bd9Sstevel@tonic-gate 			ch = toupper(ch);
125*7c478bd9Sstevel@tonic-gate 		switch (ch) {
126*7c478bd9Sstevel@tonic-gate 		case 'W':  tmp *= 7;
127*7c478bd9Sstevel@tonic-gate 		case 'D':  tmp *= 24;
128*7c478bd9Sstevel@tonic-gate 		case 'H':  tmp *= 60;
129*7c478bd9Sstevel@tonic-gate 		case 'M':  tmp *= 60;
130*7c478bd9Sstevel@tonic-gate 		case 'S':  break;
131*7c478bd9Sstevel@tonic-gate 		default:   goto einval;
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 		ttl += tmp;
134*7c478bd9Sstevel@tonic-gate 		tmp = 0;
135*7c478bd9Sstevel@tonic-gate 		digits = 0;
136*7c478bd9Sstevel@tonic-gate 		dirty = 1;
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 	if (digits > 0) {
139*7c478bd9Sstevel@tonic-gate 		if (dirty)
140*7c478bd9Sstevel@tonic-gate 			goto einval;
141*7c478bd9Sstevel@tonic-gate 		else
142*7c478bd9Sstevel@tonic-gate 			ttl += tmp;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 	*dst = ttl;
145*7c478bd9Sstevel@tonic-gate 	return (0);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate  einval:
148*7c478bd9Sstevel@tonic-gate 	errno = EINVAL;
149*7c478bd9Sstevel@tonic-gate 	return (-1);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate /* Private. */
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate static int
155*7c478bd9Sstevel@tonic-gate fmt1(int t, char s, char **buf, size_t *buflen) {
156*7c478bd9Sstevel@tonic-gate 	char tmp[50];
157*7c478bd9Sstevel@tonic-gate 	size_t len;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	len = SPRINTF((tmp, "%d%c", t, s));
160*7c478bd9Sstevel@tonic-gate 	if (len + 1 > *buflen)
161*7c478bd9Sstevel@tonic-gate 		return (-1);
162*7c478bd9Sstevel@tonic-gate 	strcpy(*buf, tmp);
163*7c478bd9Sstevel@tonic-gate 	*buf += len;
164*7c478bd9Sstevel@tonic-gate 	*buflen -= len;
165*7c478bd9Sstevel@tonic-gate 	return (0);
166*7c478bd9Sstevel@tonic-gate }
167