]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 1 | /*\r |
2 | * Copyright (c) 1996 by Internet Software Consortium.\r | |
3 | *\r | |
4 | * Permission to use, copy, modify, and distribute this software for any\r | |
5 | * purpose with or without fee is hereby granted, provided that the above\r | |
6 | * copyright notice and this permission notice appear in all copies.\r | |
7 | *\r | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r | |
9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r | |
10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r | |
11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r | |
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r | |
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r | |
14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r | |
15 | * SOFTWARE.\r | |
16 | */\r | |
17 | \r | |
18 | /*\r | |
19 | * Portions copyright (c) 1999, 2000\r | |
20 | * Intel Corporation.\r | |
21 | * All rights reserved.\r | |
22 | * \r | |
23 | * Redistribution and use in source and binary forms, with or without\r | |
24 | * modification, are permitted provided that the following conditions\r | |
25 | * are met:\r | |
26 | * \r | |
27 | * 1. Redistributions of source code must retain the above copyright\r | |
28 | * notice, this list of conditions and the following disclaimer.\r | |
29 | * \r | |
30 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
31 | * notice, this list of conditions and the following disclaimer in the\r | |
32 | * documentation and/or other materials provided with the distribution.\r | |
33 | * \r | |
34 | * 3. All advertising materials mentioning features or use of this software\r | |
35 | * must display the following acknowledgement:\r | |
36 | * \r | |
37 | * This product includes software developed by Intel Corporation and\r | |
38 | * its contributors.\r | |
39 | * \r | |
40 | * 4. Neither the name of Intel Corporation or its contributors may be\r | |
41 | * used to endorse or promote products derived from this software\r | |
42 | * without specific prior written permission.\r | |
43 | * \r | |
44 | * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r | |
45 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE\r | |
48 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r | |
49 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r | |
50 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r | |
51 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r | |
52 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r | |
53 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r | |
54 | * THE POSSIBILITY OF SUCH DAMAGE.\r | |
55 | * \r | |
56 | */\r | |
57 | \r | |
d7ce7006 | 58 | /* Import. */\r |
59 | \r | |
60 | #include <arpa/nameser.h>\r | |
61 | \r | |
62 | #include <ctype.h>\r | |
63 | #include <errno.h>\r | |
64 | #include <stdio.h>\r | |
65 | #include <string.h>\r | |
66 | \r | |
67 | #define SPRINTF(x) ((size_t)sprintf x)\r | |
68 | \r | |
69 | /* Forward. */\r | |
70 | \r | |
71 | static int fmt1(int t, char s, char **buf, size_t *buflen);\r | |
72 | \r | |
73 | /* Macros. */\r | |
74 | \r | |
75 | #define T(x) if ((x) < 0) return (-1); else (void)NULL\r | |
76 | \r | |
77 | /* Public. */\r | |
78 | \r | |
79 | int\r | |
80 | ns_format_ttl(u_long src, char *dst, size_t dstlen) {\r | |
81 | char *odst = dst;\r | |
82 | int secs, mins, hours, days, weeks, x;\r | |
83 | char *p;\r | |
84 | \r | |
85 | secs = (int)(src % 60); src /= 60;\r | |
86 | mins = (int)(src % 60); src /= 60;\r | |
87 | hours = (int)(src % 24); src /= 24;\r | |
88 | days = (int)(src % 7); src /= 7;\r | |
89 | weeks = (int)src; src = 0;\r | |
90 | \r | |
91 | x = 0;\r | |
92 | if (weeks) {\r | |
93 | T(fmt1(weeks, 'W', &dst, &dstlen));\r | |
94 | x++;\r | |
95 | }\r | |
96 | if (days) {\r | |
97 | T(fmt1(days, 'D', &dst, &dstlen));\r | |
98 | x++;\r | |
99 | }\r | |
100 | if (hours) {\r | |
101 | T(fmt1(hours, 'H', &dst, &dstlen));\r | |
102 | x++;\r | |
103 | }\r | |
104 | if (mins) {\r | |
105 | T(fmt1(mins, 'M', &dst, &dstlen));\r | |
106 | x++;\r | |
107 | }\r | |
108 | if (secs || !(weeks || days || hours || mins)) {\r | |
109 | T(fmt1(secs, 'S', &dst, &dstlen));\r | |
110 | x++;\r | |
111 | }\r | |
112 | \r | |
113 | if (x > 1) {\r | |
114 | int ch;\r | |
115 | \r | |
116 | for (p = odst; (ch = *p) != '\0'; p++)\r | |
117 | if (isascii(ch) && isupper(ch))\r | |
118 | *p = (char)( tolower(ch));\r | |
119 | }\r | |
120 | \r | |
121 | return ((int)(dst - odst));\r | |
122 | }\r | |
123 | \r | |
124 | int\r | |
125 | ns_parse_ttl(const char *src, u_long *dst) {\r | |
126 | u_long ttl, tmp;\r | |
127 | int ch, digits, dirty;\r | |
128 | \r | |
129 | ttl = 0;\r | |
130 | tmp = 0;\r | |
131 | digits = 0;\r | |
132 | dirty = 0;\r | |
133 | while ((ch = *src++) != '\0') {\r | |
134 | if (!isascii(ch) || !isprint(ch))\r | |
135 | goto einval;\r | |
136 | if (isdigit(ch)) {\r | |
137 | tmp *= 10;\r | |
138 | tmp += (ch - '0');\r | |
139 | digits++;\r | |
140 | continue;\r | |
141 | }\r | |
142 | if (digits == 0)\r | |
143 | goto einval;\r | |
144 | if (islower(ch))\r | |
145 | ch = toupper(ch);\r | |
146 | switch (ch) {\r | |
147 | case 'W': tmp *= 7;\r | |
148 | case 'D': tmp *= 24;\r | |
149 | case 'H': tmp *= 60;\r | |
150 | case 'M': tmp *= 60;\r | |
151 | case 'S': break;\r | |
152 | default: goto einval;\r | |
153 | }\r | |
154 | ttl += tmp;\r | |
155 | tmp = 0;\r | |
156 | digits = 0;\r | |
157 | dirty = 1;\r | |
158 | }\r | |
159 | if (digits > 0) {\r | |
160 | if (dirty)\r | |
161 | goto einval;\r | |
162 | else\r | |
163 | ttl += tmp;\r | |
164 | }\r | |
165 | *dst = ttl;\r | |
166 | return (0);\r | |
167 | \r | |
168 | einval:\r | |
169 | errno = EINVAL;\r | |
170 | return (-1);\r | |
171 | }\r | |
172 | \r | |
173 | /* Private. */\r | |
174 | \r | |
175 | static int\r | |
176 | fmt1(int t, char s, char **buf, size_t *buflen) {\r | |
177 | char tmp[50];\r | |
178 | size_t len;\r | |
179 | \r | |
180 | len = SPRINTF((tmp, "%d%c", t, s));\r | |
181 | if (len + 1 > *buflen)\r | |
182 | return (-1);\r | |
183 | strcpy(*buf, tmp);\r | |
184 | *buf += len;\r | |
185 | *buflen -= len;\r | |
186 | return (0);\r | |
187 | }\r |