Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libc / gen / getttyent.c
blob9a5093cddff95b3f1cc493a93ce1dc94844d3897
1 /* $NetBSD: getttyent.c,v 1.22 2005/05/14 15:43:47 christos Exp $ */
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: getttyent.c,v 1.22 2005/05/14 15:43:47 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
43 #include <assert.h>
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <ttyent.h>
48 #include <errno.h>
49 #include <err.h>
50 #include <stdlib.h>
52 #ifdef __weak_alias
53 __weak_alias(endttyent,_endttyent)
54 __weak_alias(getttyent,_getttyent)
55 __weak_alias(getttynam,_getttynam)
56 __weak_alias(setttyent,_setttyent)
57 __weak_alias(setttyentpath,_setttyentpath)
58 #endif
60 static FILE *tf;
61 static size_t lineno = 0;
62 static char *skip(char *, char *);
63 static char *value(char *);
65 struct ttyent *
66 getttynam(const char *tty)
68 struct ttyent *t;
70 _DIAGASSERT(tty != NULL);
72 setttyent();
73 while ((t = getttyent()) != NULL)
74 if (!strcmp(tty, t->ty_name))
75 break;
76 endttyent();
77 return (t);
80 struct ttyent *
81 getttyent(void)
83 static struct ttyent tty;
84 int c;
85 char *p;
86 size_t len;
87 static char *line = NULL;
88 char zapchar;
90 if (line)
91 free(line);
93 if (!tf && !setttyent())
94 return NULL;
96 for (;;) {
97 errno = 0;
98 line = fparseln(tf, &len, &lineno, NULL, FPARSELN_UNESCALL);
99 if (line == NULL) {
100 if (errno != 0)
101 warn(__func__);
102 return NULL;
104 for (p = line; *p && isspace((unsigned char)*p); p++)
105 continue;
106 if (*p && *p != '#')
107 break;
108 free(line);
111 tty.ty_name = p;
112 p = skip(p, &zapchar);
113 if (*(tty.ty_getty = p) == '\0')
114 tty.ty_getty = tty.ty_type = NULL;
115 else {
116 p = skip(p, &zapchar);
117 if (*(tty.ty_type = p) == '\0')
118 tty.ty_type = NULL;
119 else
120 p = skip(p, &zapchar);
122 tty.ty_status = 0;
123 tty.ty_window = NULL;
124 tty.ty_class = NULL;
126 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
127 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
128 for (; *p; p = skip(p, &zapchar)) {
129 if (scmp(_TTYS_OFF))
130 tty.ty_status &= ~TTY_ON;
131 else if (scmp(_TTYS_ON))
132 tty.ty_status |= TTY_ON;
133 else if (scmp(_TTYS_SECURE))
134 tty.ty_status |= TTY_SECURE;
135 else if (scmp(_TTYS_LOCAL))
136 tty.ty_status |= TTY_LOCAL;
137 else if (scmp(_TTYS_RTSCTS))
138 tty.ty_status |= TTY_RTSCTS;
139 else if (scmp(_TTYS_DTRCTS))
140 tty.ty_status |= TTY_DTRCTS;
141 else if (scmp(_TTYS_SOFTCAR))
142 tty.ty_status |= TTY_SOFTCAR;
143 else if (scmp(_TTYS_MDMBUF))
144 tty.ty_status |= TTY_MDMBUF;
145 else if (vcmp(_TTYS_WINDOW))
146 tty.ty_window = value(p);
147 else if (vcmp(_TTYS_CLASS))
148 tty.ty_class = value(p);
149 else
150 warnx("%s: %s, %lu: unknown option `%s'",
151 __func__, _PATH_TTYS, (unsigned long)lineno, p);
154 if (zapchar == '#' || *p == '#')
155 while ((c = *++p) == ' ' || c == '\t')
156 continue;
157 tty.ty_comment = p;
158 if (*p == '\0')
159 tty.ty_comment = NULL;
160 if ((p = strchr(p, '\n')) != NULL)
161 *p = '\0';
162 return &tty;
165 #define QUOTED 1
168 * Skip over the current field, removing quotes, and return a pointer to
169 * the next field.
171 static char *
172 skip(char *p, char *zapchar)
174 char *t;
175 int c, q;
177 _DIAGASSERT(p != NULL);
178 *zapchar = '\0';
180 for (q = 0, t = p; (c = *p) != '\0'; p++) {
181 if (c == '"') {
182 q ^= QUOTED; /* obscure, but nice */
183 continue;
185 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
186 p++;
187 *t++ = *p;
188 if (q == QUOTED)
189 continue;
190 if (c == '#') {
191 *zapchar = c;
192 *p = '\0';
193 *--t = '\0';
194 return p;
196 if (c == '\t' || c == ' ' || c == '\n') {
197 *zapchar = c;
198 *p++ = '\0';
199 while ((c = *p) == '\t' || c == ' ' || c == '\n')
200 p++;
201 *--t = '\0';
202 return p;
205 if (t != p)
206 *t = '\0';
207 return p;
210 static char *
211 value(char *p)
214 _DIAGASSERT(p != NULL);
216 return (p = strchr(p, '=')) != NULL ? ++p : NULL;
220 setttyentpath(const char *path)
222 lineno = 0;
223 if (tf) {
224 rewind(tf);
225 return 1;
226 } else if ((tf = fopen(path, "r")) != NULL)
227 return 1;
228 return 0;
232 setttyent(void)
234 return setttyentpath(_PATH_TTYS);
238 endttyent(void)
240 int rval;
242 if (tf) {
243 rval = !(fclose(tf) == EOF);
244 tf = NULL;
245 return rval;
247 return 1;