getttyent.c revision 22734
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	tty.ty_group  = _TTYS_NOGROUP;
114
115#define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
116#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
117	for (; *p; p = skip(p)) {
118		if (scmp(_TTYS_OFF))
119			tty.ty_status &= ~TTY_ON;
120		else if (scmp(_TTYS_ON))
121			tty.ty_status |= TTY_ON;
122		else if (scmp(_TTYS_SECURE))
123			tty.ty_status |= TTY_SECURE;
124		else if (scmp(_TTYS_INSECURE))
125			tty.ty_status &= ~TTY_SECURE;
126		else if (vcmp(_TTYS_WINDOW))
127			tty.ty_window = value(p);
128		else if (vcmp(_TTYS_GROUP))
129			tty.ty_group = value(p);
130		else
131			break;
132	}
133
134	if (zapchar == '#' || *p == '#')
135		while ((c = *++p) == ' ' || c == '\t')
136			;
137	tty.ty_comment = p;
138	if (*p == 0)
139		tty.ty_comment = 0;
140	if ( (p = index(p, '\n')) )
141		*p = '\0';
142	return (&tty);
143}
144
145#define	QUOTED	1
146
147/*
148 * Skip over the current field, removing quotes, and return a pointer to
149 * the next field.
150 */
151static char *
152skip(p)
153	register char *p;
154{
155	register char *t;
156	register int c, q;
157
158	for (q = 0, t = p; (c = *p) != '\0'; p++) {
159		if (c == '"') {
160			q ^= QUOTED;	/* obscure, but nice */
161			continue;
162		}
163		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
164			p++;
165		*t++ = *p;
166		if (q == QUOTED)
167			continue;
168		if (c == '#') {
169			zapchar = c;
170			*p = 0;
171			break;
172		}
173		if (c == '\t' || c == ' ' || c == '\n') {
174			zapchar = c;
175			*p++ = 0;
176			while ((c = *p) == '\t' || c == ' ' || c == '\n')
177				p++;
178			break;
179		}
180	}
181	*--t = '\0';
182	return (p);
183}
184
185static char *
186value(p)
187	register char *p;
188{
189
190	return ((p = index(p, '=')) ? ++p : NULL);
191}
192
193int
194setttyent()
195{
196
197	if (line == NULL) {
198		if ((line = malloc(MALLOCCHUNK)) == NULL)
199			return (0);
200		lbsize = MALLOCCHUNK;
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
210int
211endttyent()
212{
213	int rval;
214
215	/*
216         * NB: Don't free `line' because getttynam()
217	 * may still be referencing it
218	 */
219	if (tf) {
220		rval = (fclose(tf) != EOF);
221		tf = NULL;
222		return (rval);
223	}
224	return (1);
225}
226