]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/ns_ttl.c
2 * Copyright (c) 1996 by Internet Software Consortium.
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.
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
19 * Portions copyright (c) 1999, 2000
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
37 * This product includes software developed by Intel Corporation and
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
60 #include <arpa/nameser.h>
67 #define SPRINTF(x) ((size_t)sprintf x)
71 static int fmt1(int t
, char s
, char **buf
, size_t *buflen
);
75 #define T(x) if ((x) < 0) return (-1); else (void)NULL
80 ns_format_ttl(u_long src
, char *dst
, size_t dstlen
) {
82 int secs
, mins
, hours
, days
, weeks
, x
;
85 secs
= (int)(src
% 60); src
/= 60;
86 mins
= (int)(src
% 60); src
/= 60;
87 hours
= (int)(src
% 24); src
/= 24;
88 days
= (int)(src
% 7); src
/= 7;
89 weeks
= (int)src
; src
= 0;
93 T(fmt1(weeks
, 'W', &dst
, &dstlen
));
97 T(fmt1(days
, 'D', &dst
, &dstlen
));
101 T(fmt1(hours
, 'H', &dst
, &dstlen
));
105 T(fmt1(mins
, 'M', &dst
, &dstlen
));
108 if (secs
|| !(weeks
|| days
|| hours
|| mins
)) {
109 T(fmt1(secs
, 'S', &dst
, &dstlen
));
116 for (p
= odst
; (ch
= *p
) != '\0'; p
++)
117 if (isascii(ch
) && isupper(ch
))
118 *p
= (char)( tolower(ch
));
121 return ((int)(dst
- odst
));
125 ns_parse_ttl(const char *src
, u_long
*dst
) {
127 int ch
, digits
, dirty
;
133 while ((ch
= *src
++) != '\0') {
134 if (!isascii(ch
) || !isprint(ch
))
152 default: goto einval
;
176 fmt1(int t
, char s
, char **buf
, size_t *buflen
) {
180 len
= SPRINTF((tmp
, "%d%c", t
, s
));
181 if (len
+ 1 > *buflen
)