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
34 #include <stdio_ext.h>
38 #ifdef __UCLIBC_HAS_THREADS__
42 libc_hidden_proto(strchr)
43 libc_hidden_proto(strcmp)
44 libc_hidden_proto(strncmp)
45 libc_hidden_proto(__fsetlocking)
46 libc_hidden_proto(rewind)
47 libc_hidden_proto(fgets_unlocked)
48 libc_hidden_proto(getc_unlocked)
49 libc_hidden_proto(__fgetc_unlocked)
50 libc_hidden_proto(fopen)
51 libc_hidden_proto(fclose)
52 libc_hidden_proto(abort)
56 static struct ttyent tty;
59 /* Skip over the current field, removing quotes, and return
60 * a pointer to the next field.
63 static char * skip(register char *p)
68 for (q = 0, t = p; (c = *p) != '\0'; p++) {
70 q ^= QUOTED; /* obscure, but nice */
73 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
83 if (c == '\t' || c == ' ' || c == '\n') {
86 while ((c = *p) == '\t' || c == ' ' || c == '\n')
95 static char * value(register char *p)
98 return ((p = strchr(p, '=')) ? ++p : NULL);
101 int attribute_hidden __setttyent(void)
107 } else if ((tf = fopen(_PATH_TTYS, "r"))) {
108 /* We do the locking ourselves. */
109 #ifdef __UCLIBC_HAS_THREADS__
110 __fsetlocking (tf, FSETLOCKING_BYCALLER);
116 strong_alias(__setttyent,setttyent)
118 struct ttyent attribute_hidden * __getttyent(void)
122 static char *line = NULL;
124 if (!tf && !__setttyent())
128 line = malloc(BUFSIZ);
133 __STDIO_ALWAYS_THREADLOCK(tf);
136 if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
137 __STDIO_ALWAYS_THREADUNLOCK(tf);
140 /* skip lines that are too big */
141 if (!strchr(p, '\n')) {
142 while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
155 if (!*(tty.ty_getty = p))
156 tty.ty_getty = tty.ty_type = NULL;
159 if (!*(tty.ty_type = p))
165 tty.ty_window = NULL;
167 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
168 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
169 for (; *p; p = skip(p)) {
171 tty.ty_status &= ~TTY_ON;
172 else if (scmp(_TTYS_ON))
173 tty.ty_status |= TTY_ON;
174 else if (scmp(_TTYS_SECURE))
175 tty.ty_status |= TTY_SECURE;
176 else if (vcmp(_TTYS_WINDOW))
177 tty.ty_window = value(p);
181 /* We can release the lock only here since `zapchar' is global. */
182 __STDIO_ALWAYS_THREADUNLOCK(tf);
184 if (zapchar == '#' || *p == '#')
185 while ((c = *++p) == ' ' || c == '\t')
190 if ((p = strchr(p, '\n')))
194 strong_alias(__getttyent,getttyent)
196 int attribute_hidden __endttyent(void)
201 rval = !(fclose(tf) == EOF);
207 strong_alias(__endttyent,endttyent)
209 struct ttyent * getttynam(const char *tty)
211 register struct ttyent *t;
214 while ((t = __getttyent()))
215 if (!strcmp(tty, t->ty_name))