2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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$");
46 static int maxpts = 0;
47 static int curpts = 0;
48 static int pts_valid = 0;
53 #define MALLOCCHUNK 100
55 static char *skip(char *);
56 static char *value(char *);
64 if (strncmp(tty, "/dev/", 5) == 0)
67 while ( (t = getttyent()) )
68 if (!strcmp(tty, t->ty_name))
77 static struct ttyent tty;
78 static char devpts_name[] = "pts/4294967295";
83 if (!tf && !setttyent())
86 if (!fgets(p = line, lbsize, tf)) {
87 if (pts_valid == 1 && curpts <= maxpts) {
88 sprintf(devpts_name, "pts/%d", curpts++);
89 tty.ty_name = devpts_name;
90 tty.ty_getty = tty.ty_type = NULL;
91 tty.ty_status = TTY_NETWORK;
93 tty.ty_comment = NULL;
94 tty.ty_group = _TTYS_NOGROUP;
99 /* extend buffer if line was too big, and retry */
100 while (!index(p, '\n')) {
102 lbsize += MALLOCCHUNK;
103 if ((p = realloc(line, lbsize)) == NULL) {
108 if (!fgets(&line[i], lbsize - i, tf))
111 while (isspace((unsigned char)*p))
117 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
118 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
123 tty.ty_window = NULL;
124 tty.ty_group = _TTYS_NOGROUP;
127 if (!*(tty.ty_getty = p))
128 tty.ty_getty = tty.ty_type = NULL;
131 if (!*(tty.ty_type = p))
134 /* compatibility kludge: handle network/dialup specially */
135 if (scmp(_TTYS_DIALUP))
136 tty.ty_status |= TTY_DIALUP;
137 else if (scmp(_TTYS_NETWORK))
138 tty.ty_status |= TTY_NETWORK;
143 for (; *p; p = skip(p)) {
145 tty.ty_status &= ~TTY_ON;
146 else if (scmp(_TTYS_ON))
147 tty.ty_status |= TTY_ON;
148 else if (scmp(_TTYS_SECURE))
149 tty.ty_status |= TTY_SECURE;
150 else if (scmp(_TTYS_INSECURE))
151 tty.ty_status &= ~TTY_SECURE;
152 else if (scmp(_TTYS_DIALUP))
153 tty.ty_status |= TTY_DIALUP;
154 else if (scmp(_TTYS_NETWORK))
155 tty.ty_status |= TTY_NETWORK;
156 else if (vcmp(_TTYS_WINDOW))
157 tty.ty_window = value(p);
158 else if (vcmp(_TTYS_GROUP))
159 tty.ty_group = value(p);
164 if (zapchar == '#' || *p == '#')
165 while ((c = *++p) == ' ' || c == '\t')
170 if ( (p = index(p, '\n')) )
178 * Skip over the current field, removing quotes, and return a pointer to
188 for (q = 0, t = p; (c = *p) != '\0'; p++) {
190 q ^= QUOTED; /* obscure, but nice */
193 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
203 if (c == '\t' || c == ' ' || c == '\n') {
206 while ((c = *p) == '\t' || c == ' ' || c == '\n')
220 return ((p = index(p, '=')) ? ++p : NULL);
229 if ((line = malloc(MALLOCCHUNK)) == NULL)
231 lbsize = MALLOCCHUNK;
233 devpts_dir = opendir(_PATH_DEV PTS);
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);
247 closedir(devpts_dir);
252 } else if ( (tf = fopen(_PATH_TTYS, "r")) )
264 * NB: Don't free `line' because getttynam()
265 * may still be referencing it
268 rval = (fclose(tf) != EOF);
282 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
291 return isttystat(tty, TTY_DIALUP);
298 return isttystat(tty, TTY_NETWORK);