add opendir alias
[minix.git] / lib / libc / nameser / ns_ttl.c
blobe895e5a5baf075cbc532ed82d73ea7c02cb2f67a
1 /* $NetBSD: ns_ttl.c,v 1.8 2012/03/13 21:13:39 christos Exp $ */
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 #include <sys/cdefs.h>
21 #ifndef lint
22 #ifdef notdef
23 static 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.8 2012/03/13 21:13:39 christos Exp $");
26 #endif
27 #endif
29 /* Import. */
31 #include "port_before.h"
33 #include <arpa/nameser.h>
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <string.h>
41 #include "port_after.h"
43 #ifdef SPRINTF_CHAR
44 # define SPRINTF(x) strlen(sprintf/**/x)
45 #else
46 # define SPRINTF(x) ((size_t)sprintf x)
47 #endif
49 /* Forward. */
51 static int fmt1(int t, char s, char **buf, size_t *buflen);
53 /* Macros. */
55 #define T(x) if ((x) < 0) return (-1)
57 /* Public. */
59 int
60 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
61 char *odst = dst;
62 int secs, mins, hours, days, weeks, x;
63 char *p;
65 secs = (int)(src % 60); src /= 60;
66 mins = (int)(src % 60); src /= 60;
67 hours = (int)(src % 24); src /= 24;
68 days = (int)(src % 7); src /= 7;
69 weeks = (int)src; src = 0;
71 x = 0;
72 if (weeks) {
73 T(fmt1(weeks, 'W', &dst, &dstlen));
74 x++;
76 if (days) {
77 T(fmt1(days, 'D', &dst, &dstlen));
78 x++;
80 if (hours) {
81 T(fmt1(hours, 'H', &dst, &dstlen));
82 x++;
84 if (mins) {
85 T(fmt1(mins, 'M', &dst, &dstlen));
86 x++;
88 if (secs || !(weeks || days || hours || mins)) {
89 T(fmt1(secs, 'S', &dst, &dstlen));
90 x++;
93 if (x > 1) {
94 int ch;
96 for (p = odst; (ch = *p) != '\0'; p++)
97 if (isascii(ch) && isupper(ch))
98 *p = tolower(ch);
101 _DIAGASSERT(__type_fit(int, dst - odst));
102 return (int)(dst - odst);
105 #ifndef _LIBC
107 ns_parse_ttl(const char *src, u_long *dst) {
108 u_long ttl, tmp;
109 int ch, digits, dirty;
111 ttl = 0;
112 tmp = 0;
113 digits = 0;
114 dirty = 0;
115 while ((ch = *src++) != '\0') {
116 if (!isascii(ch) || !isprint(ch))
117 goto einval;
118 if (isdigit(ch)) {
119 tmp *= 10;
120 tmp += (ch - '0');
121 digits++;
122 continue;
124 if (digits == 0)
125 goto einval;
126 if (islower(ch))
127 ch = toupper(ch);
128 switch (ch) {
129 case 'W': tmp *= 7; /*FALLTHROUGH*/
130 case 'D': tmp *= 24; /*FALLTHROUGH*/
131 case 'H': tmp *= 60; /*FALLTHROUGH*/
132 case 'M': tmp *= 60; /*FALLTHROUGH*/
133 case 'S': break;
134 default: goto einval;
136 ttl += tmp;
137 tmp = 0;
138 digits = 0;
139 dirty = 1;
141 if (digits > 0) {
142 if (dirty)
143 goto einval;
144 else
145 ttl += tmp;
146 } else if (!dirty)
147 goto einval;
148 *dst = ttl;
149 return (0);
151 einval:
152 errno = EINVAL;
153 return (-1);
155 #endif
157 /* Private. */
159 static int
160 fmt1(int t, char s, char **buf, size_t *buflen) {
161 char tmp[50];
162 size_t len;
164 len = SPRINTF((tmp, "%d%c", t, s));
165 if (len + 1 > *buflen)
166 return (-1);
167 strcpy(*buf, tmp);
168 *buf += len;
169 *buflen -= len;
170 return (0);
173 /*! \file */