xref: /freebsd/lib/libc/gen/getttyent.c (revision 61afd5bb22d787b0641523e7b9b95c964d669bd5)
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)
35 static 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 
44 static char zapchar;
45 static FILE *tf;
46 static size_t lbsize;
47 static char *line;
48 
49 #define	MALLOCCHUNK	100
50 
51 static char *skip __P((char *));
52 static char *value __P((char *));
53 
54 struct ttyent *
55 getttynam(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 
68 struct ttyent *
69 getttyent()
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 (vcmp(_TTYS_WINDOW))
125 			tty.ty_window = value(p);
126 		else if (vcmp(_TTYS_GROUP))
127 			tty.ty_group = value(p);
128 		else
129 			break;
130 	}
131 
132 	if (zapchar == '#' || *p == '#')
133 		while ((c = *++p) == ' ' || c == '\t')
134 			;
135 	tty.ty_comment = p;
136 	if (*p == 0)
137 		tty.ty_comment = 0;
138 	if ( (p = index(p, '\n')) )
139 		*p = '\0';
140 	return (&tty);
141 }
142 
143 #define	QUOTED	1
144 
145 /*
146  * Skip over the current field, removing quotes, and return a pointer to
147  * the next field.
148  */
149 static char *
150 skip(p)
151 	register char *p;
152 {
153 	register char *t;
154 	register int c, q;
155 
156 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
157 		if (c == '"') {
158 			q ^= QUOTED;	/* obscure, but nice */
159 			continue;
160 		}
161 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
162 			p++;
163 		*t++ = *p;
164 		if (q == QUOTED)
165 			continue;
166 		if (c == '#') {
167 			zapchar = c;
168 			*p = 0;
169 			break;
170 		}
171 		if (c == '\t' || c == ' ' || c == '\n') {
172 			zapchar = c;
173 			*p++ = 0;
174 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
175 				p++;
176 			break;
177 		}
178 	}
179 	*--t = '\0';
180 	return (p);
181 }
182 
183 static char *
184 value(p)
185 	register char *p;
186 {
187 
188 	return ((p = index(p, '=')) ? ++p : NULL);
189 }
190 
191 int
192 setttyent()
193 {
194 
195 	if (line == NULL) {
196 		if ((line = malloc(MALLOCCHUNK)) == NULL)
197 			return (0);
198 		lbsize = MALLOCCHUNK;
199 	}
200 	if (tf) {
201 		rewind(tf);
202 		return (1);
203 	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
204 		return (1);
205 	return (0);
206 }
207 
208 int
209 endttyent()
210 {
211 	int rval;
212 
213 	/*
214          * NB: Don't free `line' because getttynam()
215 	 * may still be referencing it
216 	 */
217 	if (tf) {
218 		rval = (fclose(tf) != EOF);
219 		tf = NULL;
220 		return (rval);
221 	}
222 	return (1);
223 }
224