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