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
29 * $FreeBSD: src/lib/libc/gen/getttyent.c,v 1.13 2005/07/25 17:57:15 mdodd Exp $
31 * @(#)getttyent.c 8.1 (Berkeley) 6/4/93
44 static int maxpts = -1;
45 static int curpts = 0;
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))
73 static struct ttyent tty;
74 static char devpts_name[] = "pts/9999999999";
79 if (!tf && !setttyent())
82 if (!fgets(p = line, lbsize, tf)) {
83 if (curpts <= maxpts) {
84 sprintf(devpts_name, "pts/%d", curpts++);
85 tty.ty_name = devpts_name;
88 tty.ty_status = TTY_NETWORK;
90 tty.ty_comment = NULL;
91 tty.ty_group = _TTYS_NOGROUP;
96 /* extend buffer if line was too big, and retry */
97 while (!index(p, '\n') && !feof(tf)) {
99 lbsize += MALLOCCHUNK;
100 if ((p = realloc(line, lbsize)) == NULL) {
105 if (!fgets(&line[i], lbsize - i, tf))
108 while (isspace((unsigned char)*p))
114 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
115 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
120 tty.ty_window = NULL;
121 tty.ty_group = _TTYS_NOGROUP;
124 if (!*(tty.ty_getty = p))
125 tty.ty_getty = tty.ty_type = NULL;
128 if (!*(tty.ty_type = p))
131 /* compatibility kludge: handle network/dialup specially */
132 if (scmp(_TTYS_DIALUP))
133 tty.ty_status |= TTY_DIALUP;
134 else if (scmp(_TTYS_NETWORK))
135 tty.ty_status |= TTY_NETWORK;
140 for (; *p; p = skip(p)) {
142 tty.ty_status &= ~TTY_ON;
143 else if (scmp(_TTYS_ON))
144 tty.ty_status |= TTY_ON;
145 else if (scmp(_TTYS_SECURE))
146 tty.ty_status |= TTY_SECURE;
147 else if (scmp(_TTYS_INSECURE))
148 tty.ty_status &= ~TTY_SECURE;
149 else if (scmp(_TTYS_DIALUP))
150 tty.ty_status |= TTY_DIALUP;
151 else if (scmp(_TTYS_NETWORK))
152 tty.ty_status |= TTY_NETWORK;
153 else if (scmp(_TTYS_IFCONSOLE))
154 tty.ty_status |= TTY_IFCONSOLE;
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
186 for (q = 0, t = p; (c = *p) != '\0'; p++) {
188 q ^= QUOTED; /* obscure, but nice */
191 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
201 if (c == '\t' || c == ' ' || c == '\n') {
204 while ((c = *p) == '\t' || c == ' ' || c == '\n')
217 return ((p = index(p, '=')) ? ++p : NULL);
228 if ((line = malloc(MALLOCCHUNK)) == NULL)
230 lbsize = MALLOCCHUNK;
232 devpts_dir = opendir(_PATH_DEV PTS);
235 while ((dp = readdir(devpts_dir))) {
236 if (strcmp(dp->d_name, ".") == 0 ||
237 strcmp(dp->d_name, "..") == 0)
240 unit = (int)strtol(dp->d_name, NULL, 10);
247 closedir(devpts_dir);
252 } else if ( (tf = fopen(_PATH_TTYS, "r")) )
264 * NB: Don't free `line' because getttynam()
265 * may still be referencing it
268 rval = (fclose(tf) != EOF);
276 isttystat(const char *tty, int flag)
280 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
285 isdialuptty(const char *tty)
288 return isttystat(tty, TTY_DIALUP);
292 isnettty(const char *tty)
295 return isttystat(tty, TTY_NETWORK);