getttyent.c revision 1.9.4.1 1 /* $NetBSD: getttyent.c,v 1.9.4.1 1996/09/19 20:03:08 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: getttyent.c,v 1.9.4.1 1996/09/19 20:03:08 jtc Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45 #include <ttyent.h>
46 #include <stdio.h>
47 #include <ctype.h>
48 #include <string.h>
49
50 #ifdef __weak_alias
51 __weak_alias(endttyent,_endttyent);
52 __weak_alias(getttyent,_getttyent);
53 __weak_alias(getttynam,_getttynam);
54 __weak_alias(setttyent,_setttyent);
55 #endif
56
57 static char zapchar;
58 static FILE *tf;
59
60 struct ttyent *
61 getttynam(tty)
62 const char *tty;
63 {
64 register struct ttyent *t;
65
66 setttyent();
67 while (t = getttyent())
68 if (!strcmp(tty, t->ty_name))
69 break;
70 endttyent();
71 return (t);
72 }
73
74 struct ttyent *
75 getttyent()
76 {
77 static struct ttyent tty;
78 register int c;
79 register char *p;
80 #define MAXLINELENGTH 200
81 static char line[MAXLINELENGTH];
82 static char *skip(), *value();
83
84 if (!tf && !setttyent())
85 return (NULL);
86 for (;;) {
87 if (!fgets(p = line, sizeof(line), tf))
88 return (NULL);
89 /* skip lines that are too big */
90 if (!strchr(p, '\n')) {
91 while ((c = getc(tf)) != '\n' && c != EOF)
92 ;
93 continue;
94 }
95 while (isspace(*p))
96 ++p;
97 if (*p && *p != '#')
98 break;
99 }
100
101 zapchar = 0;
102 tty.ty_name = p;
103 p = skip(p);
104 if (!*(tty.ty_getty = p))
105 tty.ty_getty = tty.ty_type = NULL;
106 else {
107 p = skip(p);
108 if (!*(tty.ty_type = p))
109 tty.ty_type = NULL;
110 else
111 p = skip(p);
112 }
113 tty.ty_status = 0;
114 tty.ty_window = NULL;
115
116 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
117 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
118 for (; *p; p = skip(p)) {
119 if (scmp(_TTYS_OFF))
120 tty.ty_status &= ~TTY_ON;
121 else if (scmp(_TTYS_ON))
122 tty.ty_status |= TTY_ON;
123 else if (scmp(_TTYS_SECURE))
124 tty.ty_status |= TTY_SECURE;
125 else if (scmp(_TTYS_LOCAL))
126 tty.ty_status |= TTY_LOCAL;
127 else if (scmp(_TTYS_RTSCTS))
128 tty.ty_status |= TTY_RTSCTS;
129 else if (scmp(_TTYS_SOFTCAR))
130 tty.ty_status |= TTY_SOFTCAR;
131 else if (scmp(_TTYS_MDMBUF))
132 tty.ty_status |= TTY_MDMBUF;
133 else if (vcmp(_TTYS_WINDOW))
134 tty.ty_window = value(p);
135 else
136 break;
137 }
138
139 if (zapchar == '#' || *p == '#')
140 while ((c = *++p) == ' ' || c == '\t')
141 ;
142 tty.ty_comment = p;
143 if (*p == 0)
144 tty.ty_comment = 0;
145 if (p = strchr(p, '\n'))
146 *p = '\0';
147 return (&tty);
148 }
149
150 #define QUOTED 1
151
152 /*
153 * Skip over the current field, removing quotes, and return a pointer to
154 * the next field.
155 */
156 static char *
157 skip(p)
158 register char *p;
159 {
160 register char *t;
161 register int c, q;
162
163 for (q = 0, t = p; (c = *p) != '\0'; p++) {
164 if (c == '"') {
165 q ^= QUOTED; /* obscure, but nice */
166 continue;
167 }
168 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
169 p++;
170 *t++ = *p;
171 if (q == QUOTED)
172 continue;
173 if (c == '#') {
174 zapchar = c;
175 *p = 0;
176 break;
177 }
178 if (c == '\t' || c == ' ' || c == '\n') {
179 zapchar = c;
180 *p++ = 0;
181 while ((c = *p) == '\t' || c == ' ' || c == '\n')
182 p++;
183 break;
184 }
185 }
186 *--t = '\0';
187 return (p);
188 }
189
190 static char *
191 value(p)
192 register char *p;
193 {
194
195 return ((p = strchr(p, '=')) ? ++p : NULL);
196 }
197
198 int
199 setttyent()
200 {
201
202 if (tf) {
203 rewind(tf);
204 return (1);
205 } else if (tf = fopen(_PATH_TTYS, "r"))
206 return (1);
207 return (0);
208 }
209
210 int
211 endttyent()
212 {
213 int rval;
214
215 if (tf) {
216 rval = !(fclose(tf) == EOF);
217 tf = NULL;
218 return (rval);
219 }
220 return (1);
221 }
222