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