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