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 *
getttynam(const char * tty)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 *
getttyent(void)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 (scmp(_TTYS_IFEXISTS))
156 tty.ty_status |= TTY_IFEXISTS;
157 else if (vcmp(_TTYS_WINDOW))
158 tty.ty_window = value(p);
159 else if (vcmp(_TTYS_GROUP))
160 tty.ty_group = value(p);
161 else
162 break;
163 }
164
165 if (zapchar == '#' || *p == '#')
166 while ((c = *++p) == ' ' || c == '\t')
167 ;
168 tty.ty_comment = p;
169 if (*p == 0)
170 tty.ty_comment = 0;
171 if ( (p = index(p, '\n')) )
172 *p = '\0';
173 return (&tty);
174 }
175
176 #define QUOTED 1
177
178 /*
179 * Skip over the current field, removing quotes, and return a pointer to
180 * the next field.
181 */
182 static char *
skip(char * p)183 skip(char *p)
184 {
185 char *t;
186 int c, q;
187
188 for (q = 0, t = p; (c = *p) != '\0'; p++) {
189 if (c == '"') {
190 q ^= QUOTED; /* obscure, but nice */
191 continue;
192 }
193 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
194 p++;
195 *t++ = *p;
196 if (q == QUOTED)
197 continue;
198 if (c == '#') {
199 zapchar = c;
200 *p = 0;
201 break;
202 }
203 if (c == '\t' || c == ' ' || c == '\n') {
204 zapchar = c;
205 *p++ = 0;
206 while ((c = *p) == '\t' || c == ' ' || c == '\n')
207 p++;
208 break;
209 }
210 }
211 *--t = '\0';
212 return (p);
213 }
214
215 static char *
value(char * p)216 value(char *p)
217 {
218
219 return ((p = index(p, '=')) ? ++p : NULL);
220 }
221
222 int
setttyent(void)223 setttyent(void)
224 {
225 DIR *devpts_dir;
226 struct dirent *dp;
227 int unit;
228
229 if (line == NULL) {
230 if ((line = malloc(MALLOCCHUNK)) == NULL)
231 return (0);
232 lbsize = MALLOCCHUNK;
233 }
234 devpts_dir = opendir(_PATH_DEV PTS);
235 if (devpts_dir) {
236 maxpts = -1;
237 while ((dp = readdir(devpts_dir))) {
238 if (strcmp(dp->d_name, ".") == 0 ||
239 strcmp(dp->d_name, "..") == 0)
240 continue;
241
242 unit = (int)strtol(dp->d_name, NULL, 10);
243
244 if (unit > maxpts) {
245 maxpts = unit;
246 curpts = 0;
247 }
248 }
249 closedir(devpts_dir);
250 }
251 if (tf) {
252 rewind(tf);
253 return (1);
254 } else if ( (tf = fopen(_PATH_TTYS, "r")) )
255 return (1);
256 return (0);
257 }
258
259 int
endttyent(void)260 endttyent(void)
261 {
262 int rval;
263
264 maxpts = -1;
265 /*
266 * NB: Don't free `line' because getttynam()
267 * may still be referencing it
268 */
269 if (tf) {
270 rval = (fclose(tf) != EOF);
271 tf = NULL;
272 return (rval);
273 }
274 return (1);
275 }
276
277 static int
isttystat(const char * tty,int flag)278 isttystat(const char *tty, int flag)
279 {
280 struct ttyent *t;
281
282 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
283 }
284
285
286 int
isdialuptty(const char * tty)287 isdialuptty(const char *tty)
288 {
289
290 return isttystat(tty, TTY_DIALUP);
291 }
292
293 int
isnettty(const char * tty)294 isnettty(const char *tty)
295 {
296
297 return isttystat(tty, TTY_NETWORK);
298 }
299