source: rtems/cpukit/libnetworking/libc/ns_ttl.c @ cb2f320

4.104.114.84.95
Last change on this file since cb2f320 was cb2f320, checked in by Joel Sherrill <joel.sherrill@…>, on 03/05/04 at 18:02:41

2004-03-05 Joel Sherrill <joel@…>

  • libblock/src/bdbuf.c, libblock/src/ramdisk.c, libcsupport/src/newlibc.c, libcsupport/src/sync.c, libmisc/cpuuse/cpuuse.c, libmisc/monitor/mon-symbols.c, libmisc/shell/cmds.c, libmisc/shell/shell.c, libnetworking/kern/kern_sysctl.c, libnetworking/lib/ftpfs.c, libnetworking/lib/tftpDriver.c, libnetworking/libc/gethostbydns.c, libnetworking/libc/gethostbyht.c, libnetworking/libc/gethostnamadr.c, libnetworking/libc/getnetbyht.c, libnetworking/libc/getnetnamadr.c, libnetworking/libc/inet_addr.c, libnetworking/libc/linkaddr.c, libnetworking/libc/map_v4v6.c, libnetworking/libc/ns_print.c, libnetworking/libc/ns_ttl.c, libnetworking/libc/nsap_addr.c, libnetworking/libc/rcmd.c, libnetworking/libc/res_debug.c, libnetworking/libc/res_mkupdate.c, libnetworking/libc/res_query.c, libnetworking/libc/res_send.c, libnetworking/libc/res_update.c, libnetworking/net/radix.c, libnetworking/rtems/mkrootfs.c, librpc/src/rpc/clnt_perror.c, librpc/src/rpc/svc.c, score/macros/rtems/score/chain.inl, score/src/objectidtoname.c: Too much was accidentally committed -- revert.
  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[39e6e65a]1/*
2 * Copyright (c) 1996 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
[ff0f694d]18#if !defined(__rtems__)
19#if !defined(LINT) && !defined(CODECENTER)
[39e6e65a]20static char rcsid[] = "$Id$";
[ff0f694d]21#endif /* not lint */
22#endif /* not rtems */
[39e6e65a]23
24/* Import. */
25
26#include <arpa/nameser.h>
27
28#include <ctype.h>
29#include <errno.h>
30#include <stdio.h>
31#include <string.h>
32
33#define SPRINTF(x) ((size_t)sprintf x)
34
35/* Forward. */
36
37static int      fmt1(int t, char s, char **buf, size_t *buflen);
38
39/* Macros. */
40
41#define T(x) if ((x) < 0) return (-1); else (void)NULL
42
43/* Public. */
44
45int
46ns_format_ttl(u_long src, char *dst, size_t dstlen) {
47        char *odst = dst;
48        int secs, mins, hours, days, weeks, x;
[ff0f694d]49        char *p;
[39e6e65a]50
51        secs = src % 60;   src /= 60;
52        mins = src % 60;   src /= 60;
53        hours = src % 24;  src /= 24;
54        days = src % 7;    src /= 7;
55        weeks = src;       src = 0;
56
57        x = 0;
58        if (weeks) {
59                T(fmt1(weeks, 'W', &dst, &dstlen));
60                x++;
61        }
62        if (days) {
63                T(fmt1(days, 'D', &dst, &dstlen));
64                x++;
65        }
66        if (hours) {
67                T(fmt1(hours, 'H', &dst, &dstlen));
68                x++;
69        }
70        if (mins) {
71                T(fmt1(mins, 'M', &dst, &dstlen));
72                x++;
73        }
74        if (secs || !(weeks || days || hours || mins)) {
75                T(fmt1(secs, 'S', &dst, &dstlen));
76                x++;
77        }
78
79        if (x > 1) {
80                int ch;
81
82                for (p = odst; (ch = *p) != '\0'; p++)
83                        if (isascii(ch) && isupper(ch))
84                                *p = tolower(ch);
85        }
86
87        return (dst - odst);
88}
89
90int
91ns_parse_ttl(const char *src, u_long *dst) {
92        u_long ttl, tmp;
93        int ch, digits, dirty;
94
95        ttl = 0;
96        tmp = 0;
97        digits = 0;
98        dirty = 0;
99        while ((ch = *src++) != '\0') {
100                if (!isascii(ch) || !isprint(ch))
101                        goto einval;
102                if (isdigit(ch)) {
103                        tmp *= 10;
104                        tmp += (ch - '0');
105                        digits++;
106                        continue;
107                }
108                if (digits == 0)
109                        goto einval;
110                if (islower(ch))
111                        ch = toupper(ch);
112                switch (ch) {
113                case 'W':  tmp *= 7;
114                case 'D':  tmp *= 24;
115                case 'H':  tmp *= 60;
116                case 'M':  tmp *= 60;
117                case 'S':  break;
118                default:   goto einval;
119                }
120                ttl += tmp;
121                tmp = 0;
122                digits = 0;
123                dirty = 1;
124        }
125        if (digits > 0) {
126                if (dirty)
127                        goto einval;
128                else
129                        ttl += tmp;
130        }
131        *dst = ttl;
132        return (0);
133
134 einval:
135        errno = EINVAL;
136        return (-1);
137}
138
139/* Private. */
140
141static int
142fmt1(int t, char s, char **buf, size_t *buflen) {
143        char tmp[50];
144        size_t len;
145
146        len = SPRINTF((tmp, "%d%c", t, s));
147        if (len + 1 > *buflen)
148                return (-1);
149        strcpy(*buf, tmp);
150        *buf += len;
151        *buflen -= len;
152        return (0);
153}
Note: See TracBrowser for help on using the repository browser.