libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / nameser / ns_ttl.c
blob2545ffda1366f0c8d75c9de2d75fd059b72d556a
1 /* $NetBSD: ns_ttl.c,v 1.7 2009/04/12 17:07:17 christos Exp $ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 #ifndef lint
22 #ifdef notdef
23 static const char rcsid[] = "Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp";
24 #else
25 __RCSID("$NetBSD: ns_ttl.c,v 1.7 2009/04/12 17:07:17 christos Exp $");
26 #endif
27 #endif
29 /* Import. */
31 #include "port_before.h"
33 #include <arpa/nameser.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
40 #include "port_after.h"
42 #ifdef SPRINTF_CHAR
43 # define SPRINTF(x) strlen(sprintf/**/x)
44 #else
45 # define SPRINTF(x) ((size_t)sprintf x)
46 #endif
48 /* Forward. */
50 static int fmt1(int t, char s, char **buf, size_t *buflen);
52 /* Macros. */
54 #define T(x) if ((x) < 0) return (-1)
56 /* Public. */
58 int
59 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
60 char *odst = dst;
61 int secs, mins, hours, days, weeks, x;
62 char *p;
64 secs = src % 60; src /= 60;
65 mins = src % 60; src /= 60;
66 hours = src % 24; src /= 24;
67 days = src % 7; src /= 7;
68 weeks = src; src = 0;
70 x = 0;
71 if (weeks) {
72 T(fmt1(weeks, 'W', &dst, &dstlen));
73 x++;
75 if (days) {
76 T(fmt1(days, 'D', &dst, &dstlen));
77 x++;
79 if (hours) {
80 T(fmt1(hours, 'H', &dst, &dstlen));
81 x++;
83 if (mins) {
84 T(fmt1(mins, 'M', &dst, &dstlen));
85 x++;
87 if (secs || !(weeks || days || hours || mins)) {
88 T(fmt1(secs, 'S', &dst, &dstlen));
89 x++;
92 if (x > 1) {
93 int ch;
95 for (p = odst; (ch = *p) != '\0'; p++)
96 if (isascii(ch) && isupper(ch))
97 *p = tolower(ch);
100 return (dst - odst);
103 #ifndef _LIBC
105 ns_parse_ttl(const char *src, u_long *dst) {
106 u_long ttl, tmp;
107 int ch, digits, dirty;
109 ttl = 0;
110 tmp = 0;
111 digits = 0;
112 dirty = 0;
113 while ((ch = *src++) != '\0') {
114 if (!isascii(ch) || !isprint(ch))
115 goto einval;
116 if (isdigit(ch)) {
117 tmp *= 10;
118 tmp += (ch - '0');
119 digits++;
120 continue;
122 if (digits == 0)
123 goto einval;
124 if (islower(ch))
125 ch = toupper(ch);
126 switch (ch) {
127 case 'W': tmp *= 7; /*FALLTHROUGH*/
128 case 'D': tmp *= 24; /*FALLTHROUGH*/
129 case 'H': tmp *= 60; /*FALLTHROUGH*/
130 case 'M': tmp *= 60; /*FALLTHROUGH*/
131 case 'S': break;
132 default: goto einval;
134 ttl += tmp;
135 tmp = 0;
136 digits = 0;
137 dirty = 1;
139 if (digits > 0) {
140 if (dirty)
141 goto einval;
142 else
143 ttl += tmp;
144 } else if (!dirty)
145 goto einval;
146 *dst = ttl;
147 return (0);
149 einval:
150 errno = EINVAL;
151 return (-1);
153 #endif
155 /* Private. */
157 static int
158 fmt1(int t, char s, char **buf, size_t *buflen) {
159 char tmp[50];
160 size_t len;
162 len = SPRINTF((tmp, "%d%c", t, s));
163 if (len + 1 > *buflen)
164 return (-1);
165 strcpy(*buf, tmp);
166 *buf += len;
167 *buflen -= len;
168 return (0);
171 /*! \file */