Prevent check-local-headers.sh hang.
[glibc.git] / resolv / ns_ttl.c
blobd4c98bcf3ae2757d84230d7a8f3631865cd83bfe
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
19 #if !defined(_LIBC) && !defined(lint)
20 static const char rcsid[] = "$BINDId: ns_ttl.c,v 8.8 1999/10/13 16:39:36 vixie Exp $";
21 #endif
23 /* Import. */
25 #include <arpa/nameser.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
32 #ifdef SPRINTF_CHAR
33 # define SPRINTF(x) strlen(sprintf/**/x)
34 #else
35 # define SPRINTF(x) ((size_t)sprintf x)
36 #endif
38 /* Forward. */
40 static int fmt1(int t, char s, char **buf, size_t *buflen);
42 /* Macros. */
44 #define T(x) if ((x) < 0) return (-1); else (void)NULL
46 /* Public. */
48 int
49 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
50 char *odst = dst;
51 int secs, mins, hours, days, weeks, x;
52 char *p;
54 secs = src % 60; src /= 60;
55 mins = src % 60; src /= 60;
56 hours = src % 24; src /= 24;
57 days = src % 7; src /= 7;
58 weeks = src; src = 0;
60 x = 0;
61 if (weeks) {
62 T(fmt1(weeks, 'W', &dst, &dstlen));
63 x++;
65 if (days) {
66 T(fmt1(days, 'D', &dst, &dstlen));
67 x++;
69 if (hours) {
70 T(fmt1(hours, 'H', &dst, &dstlen));
71 x++;
73 if (mins) {
74 T(fmt1(mins, 'M', &dst, &dstlen));
75 x++;
77 if (secs || !(weeks || days || hours || mins)) {
78 T(fmt1(secs, 'S', &dst, &dstlen));
79 x++;
82 if (x > 1) {
83 int ch;
85 for (p = odst; (ch = *p) != '\0'; p++)
86 if (isascii(ch) && isupper(ch))
87 *p = tolower(ch);
90 return (dst - odst);
92 libresolv_hidden_def (ns_format_ttl)
94 // Seems not to be needed. It's not exported from the DSO. Some libresolv.a
95 // might depend on it so we let it in.
96 int
97 ns_parse_ttl(const char *src, u_long *dst) {
98 u_long ttl, tmp;
99 int ch, digits, dirty;
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;
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;
126 ttl += tmp;
127 tmp = 0;
128 digits = 0;
129 dirty = 1;
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);
141 einval:
142 __set_errno (EINVAL);
143 return (-1);
146 /* Private. */
148 static int
149 fmt1(int t, char s, char **buf, size_t *buflen) {
150 char tmp[50];
151 size_t len;
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);
162 /*! \file */