]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/misc/ttyent/getttyent.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / misc / ttyent / getttyent.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <features.h>
31 #include <ttyent.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #ifdef __UCLIBC_HAS_THREADS__
37 # include <stdio_ext.h>
38 #endif
39
40 static char zapchar;
41 static FILE *tf;
42 static struct ttyent tty;
43
44
45 /* Skip over the current field, removing quotes, and return
46  * a pointer to the next field.
47  */
48 #define QUOTED  1
49 static char * skip(register char *p)
50 {
51     register char *t;
52     register int c, q;
53
54     for (q = 0, t = p; (c = *p) != '\0'; p++) {
55         if (c == '"') {
56             q ^= QUOTED;        /* obscure, but nice */
57             continue;
58         }
59         if (q == QUOTED && *p == '\\' && *(p+1) == '"')
60             p++;
61         *t++ = *p;
62         if (q == QUOTED)
63             continue;
64         if (c == '#') {
65             zapchar = c;
66             *p = 0;
67             break;
68         }
69         if (c == '\t' || c == ' ' || c == '\n') {
70             zapchar = c;
71             *p++ = 0;
72             while ((c = *p) == '\t' || c == ' ' || c == '\n')
73                 p++;
74             break;
75         }
76     }
77     *--t = '\0';
78     return (p);
79 }
80
81 static char * value(register char *p)
82 {
83
84     return ((p = strchr(p, '=')) ? ++p : NULL);
85 }
86
87 int setttyent(void)
88 {
89
90     if (tf) {
91         rewind(tf);
92         return (1);
93     } else if ((tf = fopen(_PATH_TTYS, "r"))) {
94         /* We do the locking ourselves.  */
95 #ifdef __UCLIBC_HAS_THREADS__
96         __fsetlocking (tf, FSETLOCKING_BYCALLER);
97 #endif
98         return (1);
99     }
100     return (0);
101 }
102 libc_hidden_def(setttyent)
103
104 struct ttyent * getttyent(void)
105 {
106     register int c;
107     register char *p;
108     static char *line = NULL;
109     struct ttyent *retval = NULL;
110
111     if (!tf && !setttyent())
112         return (NULL);
113
114     if (!line) {
115             line = malloc(BUFSIZ);
116                 if (!line)
117                     abort();
118     }
119
120         __STDIO_ALWAYS_THREADLOCK(tf);
121
122     for (;;) {
123         if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
124             goto DONE;
125         }
126         /* skip lines that are too big */
127         if (!strchr(p, '\n')) {
128             while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
129                 ;
130             continue;
131         }
132         while (isspace(*p))
133             ++p;
134         if (*p && *p != '#')
135             break;
136     }
137
138     zapchar = 0;
139     tty.ty_name = p;
140     p = skip(p);
141     if (!*(tty.ty_getty = p))
142         tty.ty_getty = tty.ty_type = NULL;
143     else {
144         p = skip(p);
145         if (!*(tty.ty_type = p))
146             tty.ty_type = NULL;
147         else
148             p = skip(p);
149     }
150     tty.ty_status = 0;
151     tty.ty_window = NULL;
152
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)) {
156         if (scmp(_TTYS_OFF))
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);
164         else
165             break;
166     }
167
168     if (zapchar == '#' || *p == '#')
169         while ((c = *++p) == ' ' || c == '\t')
170             ;
171     tty.ty_comment = p;
172     if (*p == 0)
173         tty.ty_comment = 0;
174     if ((p = strchr(p, '\n')))
175         *p = '\0';
176     retval = &tty;
177
178  DONE:
179     __STDIO_ALWAYS_THREADUNLOCK(tf);
180     return retval;
181 }
182 libc_hidden_def(getttyent)
183
184 int endttyent(void)
185 {
186     int rval;
187
188     if (tf) {
189         rval = !(fclose(tf) == EOF);
190         tf = NULL;
191         return (rval);
192     }
193     return (1);
194 }
195 libc_hidden_def(endttyent)
196
197 struct ttyent * getttynam(const char *_tty)
198 {
199     register struct ttyent *t;
200
201     setttyent();
202     while ((t = getttyent()))
203         if (!strcmp(_tty, t->ty_name))
204             break;
205     endttyent();
206     return (t);
207 }