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
36 #ifdef __UCLIBC_HAS_THREADS__
37 # include <stdio_ext.h>
42 static struct ttyent tty;
45 /* Skip over the current field, removing quotes, and return
46 * a pointer to the next field.
49 static char * skip(register char *p)
54 for (q = 0, t = p; (c = *p) != '\0'; p++) {
56 q ^= QUOTED; /* obscure, but nice */
59 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
69 if (c == '\t' || c == ' ' || c == '\n') {
72 while ((c = *p) == '\t' || c == ' ' || c == '\n')
81 static char * value(register char *p)
84 return ((p = strchr(p, '=')) ? ++p : NULL);
93 } else if ((tf = fopen(_PATH_TTYS, "r"))) {
94 /* We do the locking ourselves. */
95 #ifdef __UCLIBC_HAS_THREADS__
96 __fsetlocking (tf, FSETLOCKING_BYCALLER);
102 libc_hidden_def(setttyent)
104 struct ttyent * getttyent(void)
108 static char *line = NULL;
109 struct ttyent *retval = NULL;
111 if (!tf && !setttyent())
115 line = malloc(BUFSIZ);
120 __STDIO_ALWAYS_THREADLOCK(tf);
123 if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
126 /* skip lines that are too big */
127 if (!strchr(p, '\n')) {
128 while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
141 if (!*(tty.ty_getty = p))
142 tty.ty_getty = tty.ty_type = NULL;
145 if (!*(tty.ty_type = p))
151 tty.ty_window = NULL;
153 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
154 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
155 for (; *p; p = skip(p)) {
157 tty.ty_status &= ~TTY_ON;
158 else if (scmp(_TTYS_ON))
159 tty.ty_status |= TTY_ON;
160 else if (scmp(_TTYS_SECURE))
161 tty.ty_status |= TTY_SECURE;
162 else if (vcmp(_TTYS_WINDOW))
163 tty.ty_window = value(p);
168 if (zapchar == '#' || *p == '#')
169 while ((c = *++p) == ' ' || c == '\t')
174 if ((p = strchr(p, '\n')))
179 __STDIO_ALWAYS_THREADUNLOCK(tf);
182 libc_hidden_def(getttyent)
189 rval = !(fclose(tf) == EOF);
195 libc_hidden_def(endttyent)
197 struct ttyent * getttynam(const char *_tty)
199 register struct ttyent *t;
202 while ((t = getttyent()))
203 if (!strcmp(_tty, t->ty_name))