xref: /freebsd/lib/libc/nameser/ns_ttl.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 /* Import. */
25 
26 #include "port_before.h"
27 
28 #include <arpa/nameser.h>
29 
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include "port_after.h"
36 
37 #ifdef SPRINTF_CHAR
38 # define SPRINTF(x) strlen(sprintf/**/x)
39 #else
40 # define SPRINTF(x) ((size_t)sprintf x)
41 #endif
42 
43 /* Forward. */
44 
45 static int	fmt1(int t, char s, char **buf, size_t *buflen);
46 
47 /* Macros. */
48 
49 #define T(x) if ((x) < 0) return (-1); else (void)NULL
50 
51 /* Public. */
52 
53 int
54 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
55 	char *odst = dst;
56 	int secs, mins, hours, days, weeks, x;
57 	char *p;
58 
59 	secs = src % 60;   src /= 60;
60 	mins = src % 60;   src /= 60;
61 	hours = src % 24;  src /= 24;
62 	days = src % 7;    src /= 7;
63 	weeks = src;       src = 0;
64 
65 	x = 0;
66 	if (weeks) {
67 		T(fmt1(weeks, 'W', &dst, &dstlen));
68 		x++;
69 	}
70 	if (days) {
71 		T(fmt1(days, 'D', &dst, &dstlen));
72 		x++;
73 	}
74 	if (hours) {
75 		T(fmt1(hours, 'H', &dst, &dstlen));
76 		x++;
77 	}
78 	if (mins) {
79 		T(fmt1(mins, 'M', &dst, &dstlen));
80 		x++;
81 	}
82 	if (secs || !(weeks || days || hours || mins)) {
83 		T(fmt1(secs, 'S', &dst, &dstlen));
84 		x++;
85 	}
86 
87 	if (x > 1) {
88 		int ch;
89 
90 		for (p = odst; (ch = *p) != '\0'; p++)
91 			if (isascii(ch) && isupper(ch))
92 				*p = tolower(ch);
93 	}
94 
95 	return (dst - odst);
96 }
97 
98 int
99 ns_parse_ttl(const char *src, u_long *dst) {
100 	u_long ttl, tmp;
101 	int ch, digits, dirty;
102 
103 	ttl = 0;
104 	tmp = 0;
105 	digits = 0;
106 	dirty = 0;
107 	while ((ch = *src++) != '\0') {
108 		if (!isascii(ch) || !isprint(ch))
109 			goto einval;
110 		if (isdigit(ch)) {
111 			tmp *= 10;
112 			tmp += (ch - '0');
113 			digits++;
114 			continue;
115 		}
116 		if (digits == 0)
117 			goto einval;
118 		if (islower(ch))
119 			ch = toupper(ch);
120 		switch (ch) {
121 		case 'W':  tmp *= 7;
122 		case 'D':  tmp *= 24;
123 		case 'H':  tmp *= 60;
124 		case 'M':  tmp *= 60;
125 		case 'S':  break;
126 		default:   goto einval;
127 		}
128 		ttl += tmp;
129 		tmp = 0;
130 		digits = 0;
131 		dirty = 1;
132 	}
133 	if (digits > 0) {
134 		if (dirty)
135 			goto einval;
136 		else
137 			ttl += tmp;
138 	} else if (!dirty)
139 		goto einval;
140 	*dst = ttl;
141 	return (0);
142 
143  einval:
144 	errno = EINVAL;
145 	return (-1);
146 }
147 
148 /* Private. */
149 
150 static int
151 fmt1(int t, char s, char **buf, size_t *buflen) {
152 	char tmp[50];
153 	size_t len;
154 
155 	len = SPRINTF((tmp, "%d%c", t, s));
156 	if (len + 1 > *buflen)
157 		return (-1);
158 	strcpy(*buf, tmp);
159 	*buf += len;
160 	*buflen -= len;
161 	return (0);
162 }
163 
164 /*! \file */
165