getttyent.c revision 1.10 1 /* $NetBSD: getttyent.c,v 1.10 1997/07/13 19:14:35 christos 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 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: getttyent.c,v 1.10 1997/07/13 19:14:35 christos Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include <ttyent.h>
46 #include <stdio.h>
47 #include <ctype.h>
48 #include <string.h>
49
50 static char zapchar;
51 static FILE *tf;
52 static char *skip __P((char *));
53 static char *value __P((char *));
54
55 struct ttyent *
56 getttynam(tty)
57 const char *tty;
58 {
59 register struct ttyent *t;
60
61 setttyent();
62 while ((t = getttyent()) != NULL)
63 if (!strcmp(tty, t->ty_name))
64 break;
65 endttyent();
66 return (t);
67 }
68
69 struct ttyent *
70 getttyent()
71 {
72 static struct ttyent tty;
73 register int c;
74 register char *p;
75 #define MAXLINELENGTH 200
76 static char line[MAXLINELENGTH];
77
78 if (!tf && !setttyent())
79 return (NULL);
80 for (;;) {
81 if (!fgets(p = line, sizeof(line), tf))
82 return (NULL);
83 /* skip lines that are too big */
84 if (!strchr(p, '\n')) {
85 while ((c = getc(tf)) != '\n' && c != EOF)
86 ;
87 continue;
88 }
89 while (isspace(*p))
90 ++p;
91 if (*p && *p != '#')
92 break;
93 }
94
95 zapchar = 0;
96 tty.ty_name = p;
97 p = skip(p);
98 if (!*(tty.ty_getty = p))
99 tty.ty_getty = tty.ty_type = NULL;
100 else {
101 p = skip(p);
102 if (!*(tty.ty_type = p))
103 tty.ty_type = NULL;
104 else
105 p = skip(p);
106 }
107 tty.ty_status = 0;
108 tty.ty_window = NULL;
109
110 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
111 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
112 for (; *p; p = skip(p)) {
113 if (scmp(_TTYS_OFF))
114 tty.ty_status &= ~TTY_ON;
115 else if (scmp(_TTYS_ON))
116 tty.ty_status |= TTY_ON;
117 else if (scmp(_TTYS_SECURE))
118 tty.ty_status |= TTY_SECURE;
119 else if (scmp(_TTYS_LOCAL))
120 tty.ty_status |= TTY_LOCAL;
121 else if (scmp(_TTYS_RTSCTS))
122 tty.ty_status |= TTY_RTSCTS;
123 else if (scmp(_TTYS_SOFTCAR))
124 tty.ty_status |= TTY_SOFTCAR;
125 else if (scmp(_TTYS_MDMBUF))
126 tty.ty_status |= TTY_MDMBUF;
127 else if (vcmp(_TTYS_WINDOW))
128 tty.ty_window = value(p);
129 else
130 break;
131 }
132
133 if (zapchar == '#' || *p == '#')
134 while ((c = *++p) == ' ' || c == '\t')
135 ;
136 tty.ty_comment = p;
137 if (*p == 0)
138 tty.ty_comment = 0;
139 if ((p = strchr(p, '\n')) != NULL)
140 *p = '\0';
141 return (&tty);
142 }
143
144 #define QUOTED 1
145
146 /*
147 * Skip over the current field, removing quotes, and return a pointer to
148 * the next field.
149 */
150 static char *
151 skip(p)
152 register char *p;
153 {
154 register char *t;
155 register int c, q;
156
157 for (q = 0, t = p; (c = *p) != '\0'; p++) {
158 if (c == '"') {
159 q ^= QUOTED; /* obscure, but nice */
160 continue;
161 }
162 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
163 p++;
164 *t++ = *p;
165 if (q == QUOTED)
166 continue;
167 if (c == '#') {
168 zapchar = c;
169 *p = 0;
170 break;
171 }
172 if (c == '\t' || c == ' ' || c == '\n') {
173 zapchar = c;
174 *p++ = 0;
175 while ((c = *p) == '\t' || c == ' ' || c == '\n')
176 p++;
177 break;
178 }
179 }
180 *--t = '\0';
181 return (p);
182 }
183
184 static char *
185 value(p)
186 register char *p;
187 {
188
189 return ((p = strchr(p, '=')) ? ++p : NULL);
190 }
191
192 int
193 setttyent()
194 {
195
196 if (tf) {
197 rewind(tf);
198 return (1);
199 } else if ((tf = fopen(_PATH_TTYS, "r")) != NULL)
200 return (1);
201 return (0);
202 }
203
204 int
205 endttyent()
206 {
207 int rval;
208
209 if (tf) {
210 rval = !(fclose(tf) == EOF);
211 tf = NULL;
212 return (rval);
213 }
214 return (1);
215 }
216