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 #include <sys/types.h>
34 #include <sys/sysctl.h>
47 #define MALLOCCHUNK 100
49 static char *skip(char *);
50 static char *value(char *);
53 getttynam(const char *tty)
57 if (strncmp(tty, "/dev/", 5) == 0)
60 while ( (t = getttyent()) )
61 if (!strcmp(tty, t->ty_name))
68 auto_tty_status(const char *ty_name)
71 char *buf, *cons, *nextcons;
76 /* Check if this is an enabled kernel console line */
78 if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
79 return (rv); /* Errors mean don't enable */
81 if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
84 if ((cons = strchr(buf, '/')) == NULL)
88 while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
89 if (strcmp(cons, ty_name) == 0) {
101 auto_exists_status(const char *ty_name)
109 asprintf(&dev, "%s", ty_name);
111 asprintf(&dev, "/dev/%s", ty_name);
112 if (dev != NULL && stat(dev, &sb) == 0)
121 static struct ttyent tty;
126 if (!tf && !setttyent())
129 if (!fgets(p = line, lbsize, tf))
131 /* extend buffer if line was too big, and retry */
132 while (!strchr(p, '\n') && !feof(tf)) {
134 lbsize += MALLOCCHUNK;
135 if ((p = realloc(line, lbsize)) == NULL) {
140 if (!fgets(&line[i], lbsize - i, tf))
143 while (isspace((unsigned char)*p))
149 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
150 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
155 tty.ty_window = NULL;
156 tty.ty_group = _TTYS_NOGROUP;
159 if (!*(tty.ty_getty = p))
160 tty.ty_getty = tty.ty_type = NULL;
163 if (!*(tty.ty_type = p))
166 /* compatibility kludge: handle network/dialup specially */
167 if (scmp(_TTYS_DIALUP))
168 tty.ty_status |= TTY_DIALUP;
169 else if (scmp(_TTYS_NETWORK))
170 tty.ty_status |= TTY_NETWORK;
175 for (; *p; p = skip(p)) {
177 tty.ty_status &= ~TTY_ON;
178 else if (scmp(_TTYS_ON))
179 tty.ty_status |= TTY_ON;
180 else if (scmp(_TTYS_ONIFCONSOLE))
181 tty.ty_status |= auto_tty_status(tty.ty_name);
182 else if (scmp(_TTYS_ONIFEXISTS))
183 tty.ty_status |= auto_exists_status(tty.ty_name);
184 else if (scmp(_TTYS_SECURE))
185 tty.ty_status |= TTY_SECURE;
186 else if (scmp(_TTYS_INSECURE))
187 tty.ty_status &= ~TTY_SECURE;
188 else if (scmp(_TTYS_DIALUP))
189 tty.ty_status |= TTY_DIALUP;
190 else if (scmp(_TTYS_NETWORK))
191 tty.ty_status |= TTY_NETWORK;
192 else if (vcmp(_TTYS_WINDOW))
193 tty.ty_window = value(p);
194 else if (vcmp(_TTYS_GROUP))
195 tty.ty_group = value(p);
200 if (zapchar == '#' || *p == '#')
201 while ((c = *++p) == ' ' || c == '\t')
206 if ((p = strchr(p, '\n')))
214 * Skip over the current field, removing quotes, and return a pointer to
223 for (q = 0, t = p; (c = *p) != '\0'; p++) {
225 q ^= QUOTED; /* obscure, but nice */
228 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
238 if (c == '\t' || c == ' ' || c == '\n') {
241 while ((c = *p) == '\t' || c == ' ' || c == '\n')
254 return ((p = strchr(p, '=')) ? ++p : NULL);
262 if ((line = malloc(MALLOCCHUNK)) == NULL)
264 lbsize = MALLOCCHUNK;
269 } else if ( (tf = fopen(_PATH_TTYS, "re")) )
280 * NB: Don't free `line' because getttynam()
281 * may still be referencing it
284 rval = (fclose(tf) != EOF);
292 isttystat(const char *tty, int flag)
296 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
301 isdialuptty(const char *tty)
304 return isttystat(tty, TTY_DIALUP);
308 isnettty(const char *tty)
311 return isttystat(tty, TTY_NETWORK);