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