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 #define __fsetlocking __fsetlocking_internal
36 #include <stdio_ext.h>
40 #ifdef __UCLIBC_HAS_THREADS__
46 static struct ttyent tty;
49 struct ttyent * getttynam(const char *tty)
51 register struct ttyent *t;
54 while ((t = getttyent()))
55 if (!__strcmp(tty, t->ty_name))
62 /* Skip over the current field, removing quotes, and return
63 * a pointer to the next field.
66 static char * skip(register char *p)
71 for (q = 0, t = p; (c = *p) != '\0'; p++) {
73 q ^= QUOTED; /* obscure, but nice */
76 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
86 if (c == '\t' || c == ' ' || c == '\n') {
89 while ((c = *p) == '\t' || c == ' ' || c == '\n')
98 static char * value(register char *p)
101 return ((p = __strchr(p, '=')) ? ++p : NULL);
104 struct ttyent * getttyent(void)
108 static char *line = NULL;
110 if (!tf && !setttyent())
114 line = malloc(BUFSIZ);
119 __STDIO_ALWAYS_THREADLOCK(tf);
122 if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
123 __STDIO_ALWAYS_THREADUNLOCK(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);
167 /* We can release the lock only here since `zapchar' is global. */
168 __STDIO_ALWAYS_THREADUNLOCK(tf);
170 if (zapchar == '#' || *p == '#')
171 while ((c = *++p) == ' ' || c == '\t')
176 if ((p = __strchr(p, '\n')))
187 } else if ((tf = fopen(_PATH_TTYS, "r"))) {
188 /* We do the locking ourselves. */
189 #ifdef __UCLIBC_HAS_THREADS__
190 __fsetlocking (tf, FSETLOCKING_BYCALLER);
202 rval = !(fclose(tf) == EOF);