xref: /dragonfly/lib/libc/gen/getttyent.c (revision a4da4a90)
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. 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  * $FreeBSD: src/lib/libc/gen/getttyent.c,v 1.13 2005/07/25 17:57:15 mdodd Exp $
30  *
31  * @(#)getttyent.c	8.1 (Berkeley) 6/4/93
32  */
33 
34 #include <ttyent.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <dirent.h>
40 #include <paths.h>
41 
42 static char zapchar;
43 static FILE *tf;
44 static int maxpts = -1;
45 static int curpts = 0;
46 static size_t lbsize;
47 static char *line;
48 
49 #define PTS	"pts/"
50 #define	MALLOCCHUNK	100
51 
52 static char *skip (char *);
53 static char *value (char *);
54 
55 struct ttyent *
56 getttynam(const char *tty)
57 {
58 	struct ttyent *t;
59 
60 	if (strncmp(tty, "/dev/", 5) == 0)
61 		tty += 5;
62 	setttyent();
63 	while ( (t = getttyent()) )
64 		if (!strcmp(tty, t->ty_name))
65 			break;
66 	endttyent();
67 	return (t);
68 }
69 
70 struct ttyent *
71 getttyent(void)
72 {
73 	static struct ttyent tty;
74 	static char devpts_name[] = "pts/9999999999";
75 	char *p;
76 	int c;
77 	size_t i;
78 
79 	if (!tf && !setttyent())
80 		return (NULL);
81 	for (;;) {
82 		if (!fgets(p = line, lbsize, tf)) {
83 			if (curpts <= maxpts) {
84 				sprintf(devpts_name, "pts/%d", curpts++);
85 				tty.ty_name = devpts_name;
86 				tty.ty_getty = NULL;
87 				tty.ty_type = NULL;
88 				tty.ty_status = TTY_NETWORK;
89 				tty.ty_window = NULL;
90 				tty.ty_comment = NULL;
91 				tty.ty_group = _TTYS_NOGROUP;
92 				return (&tty);
93 			}
94 			return (NULL);
95 		}
96 		/* extend buffer if line was too big, and retry */
97 		while (!index(p, '\n') && !feof(tf)) {
98 			i = strlen(p);
99 			lbsize += MALLOCCHUNK;
100 			if ((p = realloc(line, lbsize)) == NULL) {
101 				endttyent();
102 				return (NULL);
103 			}
104 			line = p;
105 			if (!fgets(&line[i], lbsize - i, tf))
106 				return (NULL);
107 		}
108 		while (isspace((unsigned char)*p))
109 			++p;
110 		if (*p && *p != '#')
111 			break;
112 	}
113 
114 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
115 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
116 
117 	zapchar = 0;
118 	tty.ty_name = p;
119 	tty.ty_status = 0;
120 	tty.ty_window = NULL;
121 	tty.ty_group  = _TTYS_NOGROUP;
122 
123 	p = skip(p);
124 	if (!*(tty.ty_getty = p))
125 		tty.ty_getty = tty.ty_type = NULL;
126 	else {
127 		p = skip(p);
128 		if (!*(tty.ty_type = p))
129 			tty.ty_type = NULL;
130 		else {
131 			/* compatibility kludge: handle network/dialup specially */
132 			if (scmp(_TTYS_DIALUP))
133 				tty.ty_status |= TTY_DIALUP;
134 			else if (scmp(_TTYS_NETWORK))
135 				tty.ty_status |= TTY_NETWORK;
136 			p = skip(p);
137 		}
138 	}
139 
140 	for (; *p; p = skip(p)) {
141 		if (scmp(_TTYS_OFF))
142 			tty.ty_status &= ~TTY_ON;
143 		else if (scmp(_TTYS_ON))
144 			tty.ty_status |= TTY_ON;
145 		else if (scmp(_TTYS_SECURE))
146 			tty.ty_status |= TTY_SECURE;
147 		else if (scmp(_TTYS_INSECURE))
148 			tty.ty_status &= ~TTY_SECURE;
149 		else if (scmp(_TTYS_DIALUP))
150 			tty.ty_status |= TTY_DIALUP;
151 		else if (scmp(_TTYS_NETWORK))
152 			tty.ty_status |= TTY_NETWORK;
153 		else if (scmp(_TTYS_IFCONSOLE))
154 			tty.ty_status |= TTY_IFCONSOLE;
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(char *p)
182 {
183 	char *t;
184 	int c, q;
185 
186 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
187 		if (c == '"') {
188 			q ^= QUOTED;	/* obscure, but nice */
189 			continue;
190 		}
191 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
192 			p++;
193 		*t++ = *p;
194 		if (q == QUOTED)
195 			continue;
196 		if (c == '#') {
197 			zapchar = c;
198 			*p = 0;
199 			break;
200 		}
201 		if (c == '\t' || c == ' ' || c == '\n') {
202 			zapchar = c;
203 			*p++ = 0;
204 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
205 				p++;
206 			break;
207 		}
208 	}
209 	*--t = '\0';
210 	return (p);
211 }
212 
213 static char *
214 value(char *p)
215 {
216 
217 	return ((p = index(p, '=')) ? ++p : NULL);
218 }
219 
220 int
221 setttyent(void)
222 {
223 	DIR *devpts_dir;
224 	struct dirent *dp;
225 	int unit;
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 		maxpts = -1;
235 		while ((dp = readdir(devpts_dir))) {
236 			if (strcmp(dp->d_name, ".") == 0 ||
237 			    strcmp(dp->d_name, "..") == 0)
238 				continue;
239 
240 			unit = (int)strtol(dp->d_name, NULL, 10);
241 
242 			if (unit > maxpts) {
243 				maxpts = unit;
244 				curpts = 0;
245 			}
246 		}
247 		closedir(devpts_dir);
248 	}
249 	if (tf) {
250 		rewind(tf);
251 		return (1);
252 	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
253 		return (1);
254 	return (0);
255 }
256 
257 int
258 endttyent(void)
259 {
260 	int rval;
261 
262 	maxpts = -1;
263 	/*
264          * NB: Don't free `line' because getttynam()
265 	 * may still be referencing it
266 	 */
267 	if (tf) {
268 		rval = (fclose(tf) != EOF);
269 		tf = NULL;
270 		return (rval);
271 	}
272 	return (1);
273 }
274 
275 static int
276 isttystat(const char *tty, int flag)
277 {
278 	struct ttyent *t;
279 
280 	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
281 }
282 
283 
284 int
285 isdialuptty(const char *tty)
286 {
287 
288 	return isttystat(tty, TTY_DIALUP);
289 }
290 
291 int
292 isnettty(const char *tty)
293 {
294 
295 	return isttystat(tty, TTY_NETWORK);
296 }
297