No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / nameser / ns_ttl.c
blob1d76d7d1c16d2681df42695905c4f5a450702482
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
20 #ifndef lint
21 static const char rcsid[] = "Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp";
22 #endif
24 /* Import. */
26 #include "port_before.h"
28 #include <arpa/nameser.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
35 #include "port_after.h"
37 #ifdef SPRINTF_CHAR
38 # define SPRINTF(x) strlen(sprintf/**/x)
39 #else
40 # define SPRINTF(x) ((size_t)sprintf x)
41 #endif
43 /* Forward. */
45 static int fmt1(int t, char s, char **buf, size_t *buflen);
47 /* Macros. */
49 #define T(x) if ((x) < 0) return (-1); else (void)NULL
51 /* Public. */
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;
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;
65 x = 0;
66 if (weeks) {
67 T(fmt1(weeks, 'W', &dst, &dstlen));
68 x++;
70 if (days) {
71 T(fmt1(days, 'D', &dst, &dstlen));
72 x++;
74 if (hours) {
75 T(fmt1(hours, 'H', &dst, &dstlen));
76 x++;
78 if (mins) {
79 T(fmt1(mins, 'M', &dst, &dstlen));
80 x++;
82 if (secs || !(weeks || days || hours || mins)) {
83 T(fmt1(secs, 'S', &dst, &dstlen));
84 x++;
87 if (x > 1) {
88 int ch;
90 for (p = odst; (ch = *p) != '\0'; p++)
91 if (isascii(ch) && isupper(ch))
92 *p = tolower(ch);
95 return (dst - odst);
98 int
99 ns_parse_ttl(const char *src, u_long *dst) {
100 u_long ttl, tmp;
101 int ch, digits, dirty;
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;
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;
128 ttl += tmp;
129 tmp = 0;
130 digits = 0;
131 dirty = 1;
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);
143 einval:
144 errno = EINVAL;
145 return (-1);
148 /* Private. */
150 static int
151 fmt1(int t, char s, char **buf, size_t *buflen) {
152 char tmp[50];
153 size_t len;
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);
164 /*! \file */