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
33 #include <stdio_ext.h>
37 #ifdef __UCLIBC_HAS_THREADS__
41 /* Experimentally off - libc_hidden_proto(strchr) */
42 /* Experimentally off - libc_hidden_proto(strcmp) */
43 /* Experimentally off - libc_hidden_proto(strncmp) */
44 libc_hidden_proto(__fsetlocking)
45 libc_hidden_proto(rewind)
46 libc_hidden_proto(fgets_unlocked)
47 libc_hidden_proto(getc_unlocked)
48 libc_hidden_proto(__fgetc_unlocked)
49 libc_hidden_proto(fopen)
50 libc_hidden_proto(fclose)
51 libc_hidden_proto(abort)
52 #ifdef __UCLIBC_HAS_XLOCALE__
53 libc_hidden_proto(__ctype_b_loc)
54 #elif defined __UCLIBC_HAS_CTYPE_TABLES__
55 libc_hidden_proto(__ctype_b)
60 static struct ttyent tty;
63 /* Skip over the current field, removing quotes, and return
64 * a pointer to the next field.
67 static char * skip(register char *p)
72 for (q = 0, t = p; (c = *p) != '\0'; p++) {
74 q ^= QUOTED; /* obscure, but nice */
77 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
87 if (c == '\t' || c == ' ' || c == '\n') {
90 while ((c = *p) == '\t' || c == ' ' || c == '\n')
99 static char * value(register char *p)
102 return ((p = strchr(p, '=')) ? ++p : NULL);
105 libc_hidden_proto(setttyent)
112 } else if ((tf = fopen(_PATH_TTYS, "r"))) {
113 /* We do the locking ourselves. */
114 #ifdef __UCLIBC_HAS_THREADS__
115 __fsetlocking (tf, FSETLOCKING_BYCALLER);
121 libc_hidden_def(setttyent)
123 libc_hidden_proto(getttyent)
124 struct ttyent * getttyent(void)
128 static char *line = NULL;
129 struct ttyent *retval = NULL;
131 if (!tf && !setttyent())
135 line = malloc(BUFSIZ);
140 __STDIO_ALWAYS_THREADLOCK(tf);
143 if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
146 /* skip lines that are too big */
147 if (!strchr(p, '\n')) {
148 while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
161 if (!*(tty.ty_getty = p))
162 tty.ty_getty = tty.ty_type = NULL;
165 if (!*(tty.ty_type = p))
171 tty.ty_window = NULL;
173 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
174 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
175 for (; *p; p = skip(p)) {
177 tty.ty_status &= ~TTY_ON;
178 else if (scmp(_TTYS_ON))
179 tty.ty_status |= TTY_ON;
180 else if (scmp(_TTYS_SECURE))
181 tty.ty_status |= TTY_SECURE;
182 else if (vcmp(_TTYS_WINDOW))
183 tty.ty_window = value(p);
188 if (zapchar == '#' || *p == '#')
189 while ((c = *++p) == ' ' || c == '\t')
194 if ((p = strchr(p, '\n')))
199 __STDIO_ALWAYS_THREADUNLOCK(tf);
202 libc_hidden_def(getttyent)
204 libc_hidden_proto(endttyent)
210 rval = !(fclose(tf) == EOF);
216 libc_hidden_def(endttyent)
218 struct ttyent * getttynam(const char *_tty)
220 register struct ttyent *t;
223 while ((t = getttyent()))
224 if (!strcmp(_tty, t->ty_name))