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 = -1;
47 static int curpts = 0;
52 #define MALLOCCHUNK 100
54 static char *skip(char *);
55 static char *value(char *);
63 if (strncmp(tty, "/dev/", 5) == 0)
66 while ( (t = getttyent()) )
67 if (!strcmp(tty, t->ty_name))
76 static struct ttyent tty;
77 static char devpts_name[] = "pts/4294967295";
82 if (!tf && !setttyent())
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;
92 tty.ty_comment = NULL;
93 tty.ty_group = _TTYS_NOGROUP;
98 /* extend buffer if line was too big, and retry */
99 while (!index(p, '\n') && !feof(tf)) {
101 lbsize += MALLOCCHUNK;
102 if ((p = realloc(line, lbsize)) == NULL) {
107 if (!fgets(&line[i], lbsize - i, tf))
110 while (isspace((unsigned char)*p))
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] == '='
122 tty.ty_window = NULL;
123 tty.ty_group = _TTYS_NOGROUP;
126 if (!*(tty.ty_getty = p))
127 tty.ty_getty = tty.ty_type = NULL;
130 if (!*(tty.ty_type = p))
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;
142 for (; *p; p = skip(p)) {
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);
163 if (zapchar == '#' || *p == '#')
164 while ((c = *++p) == ' ' || c == '\t')
169 if ( (p = index(p, '\n')) )
177 * Skip over the current field, removing quotes, and return a pointer to
187 for (q = 0, t = p; (c = *p) != '\0'; p++) {
189 q ^= QUOTED; /* obscure, but nice */
192 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
202 if (c == '\t' || c == ' ' || c == '\n') {
205 while ((c = *p) == '\t' || c == ' ' || c == '\n')
219 return ((p = index(p, '=')) ? ++p : NULL);
228 if ((line = malloc(MALLOCCHUNK)) == NULL)
230 lbsize = MALLOCCHUNK;
232 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);
246 closedir(devpts_dir);
251 } else if ( (tf = fopen(_PATH_TTYS, "r")) )
263 * NB: Don't free `line' because getttynam()
264 * may still be referencing it
267 rval = (fclose(tf) != EOF);
281 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
290 return isttystat(tty, TTY_DIALUP);
297 return isttystat(tty, TTY_NETWORK);