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$");
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
50 #define MALLOCCHUNK 100
52 static char *skip(char *);
53 static char *value(char *);
56 getttynam(const char *tty)
60 if (strncmp(tty, "/dev/", 5) == 0)
63 while ( (t = getttyent()) )
64 if (!strcmp(tty, t->ty_name))
71 auto_tty_status(const char *ty_name)
74 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 (0); /* 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) {
103 static struct ttyent tty;
108 if (!tf && !setttyent())
111 if (!fgets(p = line, lbsize, tf))
113 /* extend buffer if line was too big, and retry */
114 while (!index(p, '\n') && !feof(tf)) {
116 lbsize += MALLOCCHUNK;
117 if ((p = realloc(line, lbsize)) == NULL) {
122 if (!fgets(&line[i], lbsize - i, tf))
125 while (isspace((unsigned char)*p))
131 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
132 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
137 tty.ty_window = NULL;
138 tty.ty_group = _TTYS_NOGROUP;
141 if (!*(tty.ty_getty = p))
142 tty.ty_getty = tty.ty_type = NULL;
145 if (!*(tty.ty_type = p))
148 /* compatibility kludge: handle network/dialup specially */
149 if (scmp(_TTYS_DIALUP))
150 tty.ty_status |= TTY_DIALUP;
151 else if (scmp(_TTYS_NETWORK))
152 tty.ty_status |= TTY_NETWORK;
157 for (; *p; p = skip(p)) {
159 tty.ty_status &= ~TTY_ON;
160 else if (scmp(_TTYS_ON))
161 tty.ty_status |= TTY_ON;
162 else if (scmp(_TTYS_ONIFCONSOLE))
163 tty.ty_status |= auto_tty_status(tty.ty_name);
164 else if (scmp(_TTYS_SECURE))
165 tty.ty_status |= TTY_SECURE;
166 else if (scmp(_TTYS_INSECURE))
167 tty.ty_status &= ~TTY_SECURE;
168 else if (scmp(_TTYS_DIALUP))
169 tty.ty_status |= TTY_DIALUP;
170 else if (scmp(_TTYS_NETWORK))
171 tty.ty_status |= TTY_NETWORK;
172 else if (vcmp(_TTYS_WINDOW))
173 tty.ty_window = value(p);
174 else if (vcmp(_TTYS_GROUP))
175 tty.ty_group = value(p);
180 if (zapchar == '#' || *p == '#')
181 while ((c = *++p) == ' ' || c == '\t')
186 if ( (p = index(p, '\n')) )
194 * Skip over the current field, removing quotes, and return a pointer to
203 for (q = 0, t = p; (c = *p) != '\0'; p++) {
205 q ^= QUOTED; /* obscure, but nice */
208 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
218 if (c == '\t' || c == ' ' || c == '\n') {
221 while ((c = *p) == '\t' || c == ' ' || c == '\n')
234 return ((p = index(p, '=')) ? ++p : NULL);
242 if ((line = malloc(MALLOCCHUNK)) == NULL)
244 lbsize = MALLOCCHUNK;
249 } else if ( (tf = fopen(_PATH_TTYS, "r")) )
260 * NB: Don't free `line' because getttynam()
261 * may still be referencing it
264 rval = (fclose(tf) != EOF);
272 isttystat(const char *tty, int flag)
276 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
281 isdialuptty(const char *tty)
284 return isttystat(tty, TTY_DIALUP);
288 isnettty(const char *tty)
291 return isttystat(tty, TTY_NETWORK);