Sync psm/evdev/atkbd with FreeBSD
[dragonfly.git] / lib / libc / gen / getttyent.c
CommitLineData
984263bc
MD
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.
0f0f9bbd 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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 *
2bc1b80e 29 * $FreeBSD: src/lib/libc/gen/getttyent.c,v 1.13 2005/07/25 17:57:15 mdodd Exp $
1de703da
MD
30 *
31 * @(#)getttyent.c 8.1 (Berkeley) 6/4/93
984263bc
MD
32 */
33
984263bc
MD
34#include <ttyent.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <ctype.h>
38#include <string.h>
f4f08bf8
AH
39#include <dirent.h>
40#include <paths.h>
984263bc
MD
41
42static char zapchar;
43static FILE *tf;
f4f08bf8
AH
44static int maxpts = -1;
45static int curpts = 0;
984263bc
MD
46static size_t lbsize;
47static char *line;
48
f4f08bf8 49#define PTS "pts/"
984263bc
MD
50#define MALLOCCHUNK 100
51
064e1fb3
EN
52static char *skip (char *);
53static char *value (char *);
984263bc
MD
54
55struct ttyent *
0f52d283 56getttynam(const char *tty)
984263bc 57{
e27dad4e 58 struct ttyent *t;
984263bc
MD
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
70struct ttyent *
0f52d283 71getttyent(void)
984263bc
MD
72{
73 static struct ttyent tty;
f4f08bf8 74 static char devpts_name[] = "pts/9999999999";
e27dad4e
HP
75 char *p;
76 int c;
984263bc
MD
77 size_t i;
78
79 if (!tf && !setttyent())
80 return (NULL);
81 for (;;) {
f4f08bf8
AH
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 }
984263bc 94 return (NULL);
f4f08bf8 95 }
984263bc 96 /* extend buffer if line was too big, and retry */
2bc1b80e 97 while (!index(p, '\n') && !feof(tf)) {
984263bc
MD
98 i = strlen(p);
99 lbsize += MALLOCCHUNK;
100 if ((p = realloc(line, lbsize)) == NULL) {
0f52d283 101 endttyent();
984263bc
MD
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;
2bc1b80e
PA
119 tty.ty_status = 0;
120 tty.ty_window = NULL;
121 tty.ty_group = _TTYS_NOGROUP;
122
984263bc
MD
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 }
984263bc
MD
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;
fda9d792
MD
153 else if (scmp(_TTYS_IFCONSOLE))
154 tty.ty_status |= TTY_IFCONSOLE;
f0e61bb7
AL
155 else if (scmp(_TTYS_IFEXISTS))
156 tty.ty_status |= TTY_IFEXISTS;
984263bc
MD
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 */
182static char *
0f52d283 183skip(char *p)
984263bc 184{
e27dad4e
HP
185 char *t;
186 int c, q;
984263bc
MD
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
215static char *
0f52d283 216value(char *p)
984263bc
MD
217{
218
219 return ((p = index(p, '=')) ? ++p : NULL);
220}
221
222int
0f52d283 223setttyent(void)
984263bc 224{
f4f08bf8
AH
225 DIR *devpts_dir;
226 struct dirent *dp;
227 int unit;
984263bc
MD
228
229 if (line == NULL) {
230 if ((line = malloc(MALLOCCHUNK)) == NULL)
231 return (0);
232 lbsize = MALLOCCHUNK;
233 }
f4f08bf8
AH
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 }
984263bc
MD
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
259int
0f52d283 260endttyent(void)
984263bc
MD
261{
262 int rval;
263
f4f08bf8 264 maxpts = -1;
984263bc
MD
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
277static int
0f52d283 278isttystat(const char *tty, int flag)
984263bc 279{
e27dad4e 280 struct ttyent *t;
984263bc
MD
281
282 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
283}
284
285
286int
0f52d283 287isdialuptty(const char *tty)
984263bc
MD
288{
289
290 return isttystat(tty, TTY_DIALUP);
291}
292
0f52d283
SW
293int
294isnettty(const char *tty)
984263bc
MD
295{
296
297 return isttystat(tty, TTY_NETWORK);
298}