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