2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
40 #include <sys/sysctl.h>
53 #define MALLOCCHUNK 100
55 static char *skip(char *);
56 static char *value(char *);
59 getttynam(const char *tty)
63 if (strncmp(tty, "/dev/", 5) == 0)
66 while ( (t = getttyent()) )
67 if (!strcmp(tty, t->ty_name))
74 auto_tty_status(const char *ty_name)
77 char *buf, *cons, *nextcons;
82 /* Check if this is an enabled kernel console line */
84 if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
85 return (rv); /* Errors mean don't enable */
87 if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
90 if ((cons = strchr(buf, '/')) == NULL)
94 while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
95 if (strcmp(cons, ty_name) == 0) {
107 auto_exists_status(const char *ty_name)
115 asprintf(&dev, "%s", ty_name);
117 asprintf(&dev, "/dev/%s", ty_name);
118 if (dev != NULL && stat(dev, &sb) == 0)
127 static struct ttyent tty;
132 if (!tf && !setttyent())
135 if (!fgets(p = line, lbsize, tf))
137 /* extend buffer if line was too big, and retry */
138 while (!strchr(p, '\n') && !feof(tf)) {
140 lbsize += MALLOCCHUNK;
141 if ((p = realloc(line, lbsize)) == NULL) {
146 if (!fgets(&line[i], lbsize - i, tf))
149 while (isspace((unsigned char)*p))
155 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
156 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
161 tty.ty_window = NULL;
162 tty.ty_group = _TTYS_NOGROUP;
165 if (!*(tty.ty_getty = p))
166 tty.ty_getty = tty.ty_type = NULL;
169 if (!*(tty.ty_type = p))
172 /* compatibility kludge: handle network/dialup specially */
173 if (scmp(_TTYS_DIALUP))
174 tty.ty_status |= TTY_DIALUP;
175 else if (scmp(_TTYS_NETWORK))
176 tty.ty_status |= TTY_NETWORK;
181 for (; *p; p = skip(p)) {
183 tty.ty_status &= ~TTY_ON;
184 else if (scmp(_TTYS_ON))
185 tty.ty_status |= TTY_ON;
186 else if (scmp(_TTYS_ONIFCONSOLE))
187 tty.ty_status |= auto_tty_status(tty.ty_name);
188 else if (scmp(_TTYS_ONIFEXISTS))
189 tty.ty_status |= auto_exists_status(tty.ty_name);
190 else if (scmp(_TTYS_SECURE))
191 tty.ty_status |= TTY_SECURE;
192 else if (scmp(_TTYS_INSECURE))
193 tty.ty_status &= ~TTY_SECURE;
194 else if (scmp(_TTYS_DIALUP))
195 tty.ty_status |= TTY_DIALUP;
196 else if (scmp(_TTYS_NETWORK))
197 tty.ty_status |= TTY_NETWORK;
198 else if (vcmp(_TTYS_WINDOW))
199 tty.ty_window = value(p);
200 else if (vcmp(_TTYS_GROUP))
201 tty.ty_group = value(p);
206 if (zapchar == '#' || *p == '#')
207 while ((c = *++p) == ' ' || c == '\t')
212 if ((p = strchr(p, '\n')))
220 * Skip over the current field, removing quotes, and return a pointer to
229 for (q = 0, t = p; (c = *p) != '\0'; p++) {
231 q ^= QUOTED; /* obscure, but nice */
234 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
244 if (c == '\t' || c == ' ' || c == '\n') {
247 while ((c = *p) == '\t' || c == ' ' || c == '\n')
260 return ((p = strchr(p, '=')) ? ++p : NULL);
268 if ((line = malloc(MALLOCCHUNK)) == NULL)
270 lbsize = MALLOCCHUNK;
275 } else if ( (tf = fopen(_PATH_TTYS, "re")) )
286 * NB: Don't free `line' because getttynam()
287 * may still be referencing it
290 rval = (fclose(tf) != EOF);
298 isttystat(const char *tty, int flag)
302 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
307 isdialuptty(const char *tty)
310 return isttystat(tty, TTY_DIALUP);
314 isnettty(const char *tty)
317 return isttystat(tty, TTY_NETWORK);