]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/ns_ttl.c
Fix send to properly wait while long transmits are in progress
[mirror_edk2.git] / StdLib / BsdSocketLib / ns_ttl.c
CommitLineData
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
58#ifndef lint\r
59static char rcsid[] = "$Id: ns_ttl.c,v 1.1.1.1 2003/11/19 01:51:34 kyu3 Exp $";\r
60#endif\r
61\r
62/* Import. */\r
63\r
64#include <arpa/nameser.h>\r
65\r
66#include <ctype.h>\r
67#include <errno.h>\r
68#include <stdio.h>\r
69#include <string.h>\r
70\r
71#define SPRINTF(x) ((size_t)sprintf x)\r
72\r
73/* Forward. */\r
74\r
75static int fmt1(int t, char s, char **buf, size_t *buflen);\r
76\r
77/* Macros. */\r
78\r
79#define T(x) if ((x) < 0) return (-1); else (void)NULL\r
80\r
81/* Public. */\r
82\r
83int\r
84ns_format_ttl(u_long src, char *dst, size_t dstlen) {\r
85 char *odst = dst;\r
86 int secs, mins, hours, days, weeks, x;\r
87 char *p;\r
88\r
89 secs = (int)(src % 60); src /= 60;\r
90 mins = (int)(src % 60); src /= 60;\r
91 hours = (int)(src % 24); src /= 24;\r
92 days = (int)(src % 7); src /= 7;\r
93 weeks = (int)src; src = 0;\r
94\r
95 x = 0;\r
96 if (weeks) {\r
97 T(fmt1(weeks, 'W', &dst, &dstlen));\r
98 x++;\r
99 }\r
100 if (days) {\r
101 T(fmt1(days, 'D', &dst, &dstlen));\r
102 x++;\r
103 }\r
104 if (hours) {\r
105 T(fmt1(hours, 'H', &dst, &dstlen));\r
106 x++;\r
107 }\r
108 if (mins) {\r
109 T(fmt1(mins, 'M', &dst, &dstlen));\r
110 x++;\r
111 }\r
112 if (secs || !(weeks || days || hours || mins)) {\r
113 T(fmt1(secs, 'S', &dst, &dstlen));\r
114 x++;\r
115 }\r
116\r
117 if (x > 1) {\r
118 int ch;\r
119\r
120 for (p = odst; (ch = *p) != '\0'; p++)\r
121 if (isascii(ch) && isupper(ch))\r
122 *p = (char)( tolower(ch));\r
123 }\r
124\r
125 return ((int)(dst - odst));\r
126}\r
127\r
128int\r
129ns_parse_ttl(const char *src, u_long *dst) {\r
130 u_long ttl, tmp;\r
131 int ch, digits, dirty;\r
132\r
133 ttl = 0;\r
134 tmp = 0;\r
135 digits = 0;\r
136 dirty = 0;\r
137 while ((ch = *src++) != '\0') {\r
138 if (!isascii(ch) || !isprint(ch))\r
139 goto einval;\r
140 if (isdigit(ch)) {\r
141 tmp *= 10;\r
142 tmp += (ch - '0');\r
143 digits++;\r
144 continue;\r
145 }\r
146 if (digits == 0)\r
147 goto einval;\r
148 if (islower(ch))\r
149 ch = toupper(ch);\r
150 switch (ch) {\r
151 case 'W': tmp *= 7;\r
152 case 'D': tmp *= 24;\r
153 case 'H': tmp *= 60;\r
154 case 'M': tmp *= 60;\r
155 case 'S': break;\r
156 default: goto einval;\r
157 }\r
158 ttl += tmp;\r
159 tmp = 0;\r
160 digits = 0;\r
161 dirty = 1;\r
162 }\r
163 if (digits > 0) {\r
164 if (dirty)\r
165 goto einval;\r
166 else\r
167 ttl += tmp;\r
168 }\r
169 *dst = ttl;\r
170 return (0);\r
171\r
172 einval:\r
173 errno = EINVAL;\r
174 return (-1);\r
175}\r
176\r
177/* Private. */\r
178\r
179static int\r
180fmt1(int t, char s, char **buf, size_t *buflen) {\r
181 char tmp[50];\r
182 size_t len;\r
183\r
184 len = SPRINTF((tmp, "%d%c", t, s));\r
185 if (len + 1 > *buflen)\r
186 return (-1);\r
187 strcpy(*buf, tmp);\r
188 *buf += len;\r
189 *buflen -= len;\r
190 return (0);\r
191}\r