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 * 3. 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$");
36 #include <sys/types.h>
38 #include <sys/sysctl.h>
51 #define MALLOCCHUNK 100
53 static char *skip(char *);
54 static char *value(char *);
57 getttynam(const char *tty)
61 if (strncmp(tty, "/dev/", 5) == 0)
64 while ( (t = getttyent()) )
65 if (!strcmp(tty, t->ty_name))
72 auto_tty_status(const char *ty_name)
75 char *buf, *cons, *nextcons;
77 /* Check if this is an enabled kernel console line */
79 if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
80 return (0); /* Errors mean don't enable */
82 if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
85 if ((cons = strchr(buf, '/')) == NULL)
89 while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
90 if (strcmp(cons, ty_name) == 0) {
102 auto_exists_status(const char *ty_name)
110 asprintf(&dev, "%s", ty_name);
112 asprintf(&dev, "/dev/%s", ty_name);
113 if (dev != NULL && stat(dev, &sb) == 0)
122 static struct ttyent tty;
127 if (!tf && !setttyent())
130 if (!fgets(p = line, lbsize, tf))
132 /* extend buffer if line was too big, and retry */
133 while (!strchr(p, '\n') && !feof(tf)) {
135 lbsize += MALLOCCHUNK;
136 if ((p = realloc(line, lbsize)) == NULL) {
141 if (!fgets(&line[i], lbsize - i, tf))
144 while (isspace((unsigned char)*p))
150 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
151 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
156 tty.ty_window = NULL;
157 tty.ty_group = _TTYS_NOGROUP;
160 if (!*(tty.ty_getty = p))
161 tty.ty_getty = tty.ty_type = NULL;
164 if (!*(tty.ty_type = p))
167 /* compatibility kludge: handle network/dialup specially */
168 if (scmp(_TTYS_DIALUP))
169 tty.ty_status |= TTY_DIALUP;
170 else if (scmp(_TTYS_NETWORK))
171 tty.ty_status |= TTY_NETWORK;
176 for (; *p; p = skip(p)) {
178 tty.ty_status &= ~TTY_ON;
179 else if (scmp(_TTYS_ON))
180 tty.ty_status |= TTY_ON;
181 else if (scmp(_TTYS_ONIFCONSOLE))
182 tty.ty_status |= auto_tty_status(tty.ty_name);
183 else if (scmp(_TTYS_ONIFEXISTS))
184 tty.ty_status |= auto_exists_status(tty.ty_name);
185 else if (scmp(_TTYS_SECURE))
186 tty.ty_status |= TTY_SECURE;
187 else if (scmp(_TTYS_INSECURE))
188 tty.ty_status &= ~TTY_SECURE;
189 else if (scmp(_TTYS_DIALUP))
190 tty.ty_status |= TTY_DIALUP;
191 else if (scmp(_TTYS_NETWORK))
192 tty.ty_status |= TTY_NETWORK;
193 else if (vcmp(_TTYS_WINDOW))
194 tty.ty_window = value(p);
195 else if (vcmp(_TTYS_GROUP))
196 tty.ty_group = value(p);
201 if (zapchar == '#' || *p == '#')
202 while ((c = *++p) == ' ' || c == '\t')
207 if ((p = strchr(p, '\n')))
215 * Skip over the current field, removing quotes, and return a pointer to
224 for (q = 0, t = p; (c = *p) != '\0'; p++) {
226 q ^= QUOTED; /* obscure, but nice */
229 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
239 if (c == '\t' || c == ' ' || c == '\n') {
242 while ((c = *p) == '\t' || c == ' ' || c == '\n')
255 return ((p = strchr(p, '=')) ? ++p : NULL);
263 if ((line = malloc(MALLOCCHUNK)) == NULL)
265 lbsize = MALLOCCHUNK;
270 } else if ( (tf = fopen(_PATH_TTYS, "re")) )
281 * NB: Don't free `line' because getttynam()
282 * may still be referencing it
285 rval = (fclose(tf) != EOF);
293 isttystat(const char *tty, int flag)
297 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
302 isdialuptty(const char *tty)
305 return isttystat(tty, TTY_DIALUP);
309 isnettty(const char *tty)
312 return isttystat(tty, TTY_NETWORK);