source: trunk/libs/newlib/src/newlib/libc/sys/linux/net/ns_ttl.c

Last change on this file was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.2 KB
Line 
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)
19static 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#include "libc-symbols.h"
32
33#ifdef SPRINTF_CHAR
34# define SPRINTF(x) strlen(sprintf/**/x)
35#else
36# define SPRINTF(x) ((size_t)sprintf x)
37#endif
38
39/* Forward. */
40
41static int      fmt1(int t, char s, char **buf, size_t *buflen);
42
43/* Macros. */
44
45#define T(x) if ((x) < 0) return (-1); else (void)NULL
46
47/* Public. */
48
49int
50ns_format_ttl(u_long src, char *dst, size_t dstlen) {
51        char *odst = dst;
52        int secs, mins, hours, days, weeks, x;
53        char *p;
54
55        secs = src % 60;   src /= 60;
56        mins = src % 60;   src /= 60;
57        hours = src % 24;  src /= 24;
58        days = src % 7;    src /= 7;
59        weeks = src;       src = 0;
60
61        x = 0;
62        if (weeks) {
63                T(fmt1(weeks, 'W', &dst, &dstlen));
64                x++;
65        }
66        if (days) {
67                T(fmt1(days, 'D', &dst, &dstlen));
68                x++;
69        }
70        if (hours) {
71                T(fmt1(hours, 'H', &dst, &dstlen));
72                x++;
73        }
74        if (mins) {
75                T(fmt1(mins, 'M', &dst, &dstlen));
76                x++;
77        }
78        if (secs || !(weeks || days || hours || mins)) {
79                T(fmt1(secs, 'S', &dst, &dstlen));
80                x++;
81        }
82
83        if (x > 1) {
84                int ch;
85
86                for (p = odst; (ch = *p) != '\0'; p++)
87                        if (isascii(ch) && isupper(ch))
88                                *p = tolower(ch);
89        }
90
91        return (dst - odst);
92}
93
94#ifndef SHARED
95// Seems not to be needed.  It's not exported from the DSO.  Some libresolv.a
96// might depend on it so we let it in.
97int
98ns_parse_ttl(const char *src, u_long *dst) {
99        u_long ttl, tmp;
100        int ch, digits, dirty;
101
102        ttl = 0;
103        tmp = 0;
104        digits = 0;
105        dirty = 0;
106        while ((ch = *src++) != '\0') {
107                if (!isascii(ch) || !isprint(ch))
108                        goto einval;
109                if (isdigit(ch)) {
110                        tmp *= 10;
111                        tmp += (ch - '0');
112                        digits++;
113                        continue;
114                }
115                if (digits == 0)
116                        goto einval;
117                if (islower(ch))
118                        ch = toupper(ch);
119                switch (ch) {
120                case 'W':  tmp *= 7;
121                case 'D':  tmp *= 24;
122                case 'H':  tmp *= 60;
123                case 'M':  tmp *= 60;
124                case 'S':  break;
125                default:   goto einval;
126                }
127                ttl += tmp;
128                tmp = 0;
129                digits = 0;
130                dirty = 1;
131        }
132        if (digits > 0) {
133                if (dirty)
134                        goto einval;
135                else
136                        ttl += tmp;
137        }
138        *dst = ttl;
139        return (0);
140
141 einval:
142        __set_errno (EINVAL);
143        return (-1);
144}
145#endif
146
147/* Private. */
148
149static int
150fmt1(int t, char s, char **buf, size_t *buflen) {
151        char tmp[50];
152        size_t len;
153
154        len = SPRINTF((tmp, "%d%c", t, s));
155        if (len + 1 > *buflen)
156                return (-1);
157        strcpy(*buf, tmp);
158        *buf += len;
159        *buflen -= len;
160        return (0);
161}
Note: See TracBrowser for help on using the repository browser.