xref: /freebsd/lib/libc/gen/getttyent.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <ttyent.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <dirent.h>
42 #include <paths.h>
43 
44 static char zapchar;
45 static FILE *tf;
46 static int maxpts = -1;
47 static int curpts = 0;
48 static size_t lbsize;
49 static char *line;
50 
51 #define PTS "pts/"
52 #define	MALLOCCHUNK	100
53 
54 static char *skip(char *);
55 static char *value(char *);
56 
57 struct ttyent *
58 getttynam(tty)
59 	const char *tty;
60 {
61 	struct ttyent *t;
62 
63 	if (strncmp(tty, "/dev/", 5) == 0)
64 		tty += 5;
65 	setttyent();
66 	while ( (t = getttyent()) )
67 		if (!strcmp(tty, t->ty_name))
68 			break;
69 	endttyent();
70 	return (t);
71 }
72 
73 struct ttyent *
74 getttyent()
75 {
76 	static struct ttyent tty;
77 	static char devpts_name[] = "pts/4294967295";
78 	char *p;
79 	int c;
80 	size_t i;
81 
82 	if (!tf && !setttyent())
83 		return (NULL);
84 	for (;;) {
85 		if (!fgets(p = line, lbsize, tf)) {
86 			if (curpts <= maxpts) {
87 				sprintf(devpts_name, "pts/%d", curpts++);
88 				tty.ty_name = devpts_name;
89 				tty.ty_getty = tty.ty_type = NULL;
90 				tty.ty_status = TTY_NETWORK;
91 				tty.ty_window = NULL;
92 				tty.ty_comment = NULL;
93 				tty.ty_group  = _TTYS_NOGROUP;
94 				return (&tty);
95 			}
96 			return (NULL);
97 		}
98 		/* extend buffer if line was too big, and retry */
99 		while (!index(p, '\n') && !feof(tf)) {
100 			i = strlen(p);
101 			lbsize += MALLOCCHUNK;
102 			if ((p = realloc(line, lbsize)) == NULL) {
103 				(void)endttyent();
104 				return (NULL);
105 			}
106 			line = p;
107 			if (!fgets(&line[i], lbsize - i, tf))
108 				return (NULL);
109 		}
110 		while (isspace((unsigned char)*p))
111 			++p;
112 		if (*p && *p != '#')
113 			break;
114 	}
115 
116 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
117 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
118 
119 	zapchar = 0;
120 	tty.ty_name = p;
121 	tty.ty_status = 0;
122 	tty.ty_window = NULL;
123 	tty.ty_group  = _TTYS_NOGROUP;
124 
125 	p = skip(p);
126 	if (!*(tty.ty_getty = p))
127 		tty.ty_getty = tty.ty_type = NULL;
128 	else {
129 		p = skip(p);
130 		if (!*(tty.ty_type = p))
131 			tty.ty_type = NULL;
132 		else {
133 			/* compatibility kludge: handle network/dialup specially */
134 			if (scmp(_TTYS_DIALUP))
135 				tty.ty_status |= TTY_DIALUP;
136 			else if (scmp(_TTYS_NETWORK))
137 				tty.ty_status |= TTY_NETWORK;
138 			p = skip(p);
139 		}
140 	}
141 
142 	for (; *p; p = skip(p)) {
143 		if (scmp(_TTYS_OFF))
144 			tty.ty_status &= ~TTY_ON;
145 		else if (scmp(_TTYS_ON))
146 			tty.ty_status |= TTY_ON;
147 		else if (scmp(_TTYS_SECURE))
148 			tty.ty_status |= TTY_SECURE;
149 		else if (scmp(_TTYS_INSECURE))
150 			tty.ty_status &= ~TTY_SECURE;
151 		else if (scmp(_TTYS_DIALUP))
152 			tty.ty_status |= TTY_DIALUP;
153 		else if (scmp(_TTYS_NETWORK))
154 			tty.ty_status |= TTY_NETWORK;
155 		else if (vcmp(_TTYS_WINDOW))
156 			tty.ty_window = value(p);
157 		else if (vcmp(_TTYS_GROUP))
158 			tty.ty_group = value(p);
159 		else
160 			break;
161 	}
162 
163 	if (zapchar == '#' || *p == '#')
164 		while ((c = *++p) == ' ' || c == '\t')
165 			;
166 	tty.ty_comment = p;
167 	if (*p == 0)
168 		tty.ty_comment = 0;
169 	if ( (p = index(p, '\n')) )
170 		*p = '\0';
171 	return (&tty);
172 }
173 
174 #define	QUOTED	1
175 
176 /*
177  * Skip over the current field, removing quotes, and return a pointer to
178  * the next field.
179  */
180 static char *
181 skip(p)
182 	char *p;
183 {
184 	char *t;
185 	int c, q;
186 
187 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
188 		if (c == '"') {
189 			q ^= QUOTED;	/* obscure, but nice */
190 			continue;
191 		}
192 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
193 			p++;
194 		*t++ = *p;
195 		if (q == QUOTED)
196 			continue;
197 		if (c == '#') {
198 			zapchar = c;
199 			*p = 0;
200 			break;
201 		}
202 		if (c == '\t' || c == ' ' || c == '\n') {
203 			zapchar = c;
204 			*p++ = 0;
205 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
206 				p++;
207 			break;
208 		}
209 	}
210 	*--t = '\0';
211 	return (p);
212 }
213 
214 static char *
215 value(p)
216 	char *p;
217 {
218 
219 	return ((p = index(p, '=')) ? ++p : NULL);
220 }
221 
222 int
223 setttyent()
224 {
225 	DIR *devpts_dir;
226 
227 	if (line == NULL) {
228 		if ((line = malloc(MALLOCCHUNK)) == NULL)
229 			return (0);
230 		lbsize = MALLOCCHUNK;
231 	}
232 	devpts_dir = opendir(_PATH_DEV PTS);
233 	if (devpts_dir) {
234 		struct dirent *dp;
235 
236 		maxpts = -1;
237 		while ((dp = readdir(devpts_dir))) {
238 			if (strcmp(dp->d_name, ".") != 0 &&
239 			    strcmp(dp->d_name, "..") != 0) {
240 				if (atoi(dp->d_name) > maxpts) {
241 					maxpts = atoi(dp->d_name);
242 					curpts = 0;
243 				}
244 			}
245 		}
246 		closedir(devpts_dir);
247 	}
248 	if (tf) {
249 		rewind(tf);
250 		return (1);
251 	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
252 		return (1);
253 	return (0);
254 }
255 
256 int
257 endttyent()
258 {
259 	int rval;
260 
261 	maxpts = -1;
262 	/*
263          * NB: Don't free `line' because getttynam()
264 	 * may still be referencing it
265 	 */
266 	if (tf) {
267 		rval = (fclose(tf) != EOF);
268 		tf = NULL;
269 		return (rval);
270 	}
271 	return (1);
272 }
273 
274 static int
275 isttystat(tty, flag)
276 	const char *tty;
277 	int flag;
278 {
279 	struct ttyent *t;
280 
281 	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
282 }
283 
284 
285 int
286 isdialuptty(tty)
287 	const char *tty;
288 {
289 
290 	return isttystat(tty, TTY_DIALUP);
291 }
292 
293 int isnettty(tty)
294 	const char *tty;
295 {
296 
297 	return isttystat(tty, TTY_NETWORK);
298 }
299