OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / 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 #define _GNU_SOURCE
31 #include <features.h>
32 #include <ttyent.h>
33 #include <stdio.h>
34 #include <stdio_ext.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #ifdef __UCLIBC_HAS_THREADS__
39 #include <pthread.h>
40 #endif
41
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)
53
54 static char zapchar;
55 static FILE *tf;
56 static struct ttyent tty;
57
58
59 /* Skip over the current field, removing quotes, and return 
60  * a pointer to the next field.
61  */
62 #define QUOTED  1
63 static char * skip(register char *p)
64 {
65     register char *t;
66     register int c, q;
67
68     for (q = 0, t = p; (c = *p) != '\0'; p++) {
69         if (c == '"') {
70             q ^= QUOTED;        /* obscure, but nice */
71             continue;
72         }
73         if (q == QUOTED && *p == '\\' && *(p+1) == '"')
74             p++;
75         *t++ = *p;
76         if (q == QUOTED)
77             continue;
78         if (c == '#') {
79             zapchar = c;
80             *p = 0;
81             break;
82         }
83         if (c == '\t' || c == ' ' || c == '\n') {
84             zapchar = c;
85             *p++ = 0;
86             while ((c = *p) == '\t' || c == ' ' || c == '\n')
87                 p++;
88             break;
89         }
90     }
91     *--t = '\0';
92     return (p);
93 }
94
95 static char * value(register char *p)
96 {
97
98     return ((p = strchr(p, '=')) ? ++p : NULL);
99 }
100
101 int attribute_hidden __setttyent(void)
102 {
103
104     if (tf) {
105         rewind(tf);
106         return (1);
107     } else if ((tf = fopen(_PATH_TTYS, "r"))) {
108         /* We do the locking ourselves.  */
109 #ifdef __UCLIBC_HAS_THREADS__
110         __fsetlocking (tf, FSETLOCKING_BYCALLER);
111 #endif
112         return (1);
113     }
114     return (0);
115 }
116 strong_alias(__setttyent,setttyent)
117
118 struct ttyent attribute_hidden * __getttyent(void)
119 {
120     register int c;
121     register char *p;
122     static char *line = NULL;
123
124     if (!tf && !__setttyent())
125         return (NULL);
126
127     if (!line) {
128             line = malloc(BUFSIZ);
129                 if (!line)
130                     abort();
131     }
132
133         __STDIO_ALWAYS_THREADLOCK(tf);
134
135     for (;;) {
136         if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
137                 __STDIO_ALWAYS_THREADUNLOCK(tf);
138             return (NULL);
139         }
140         /* skip lines that are too big */
141         if (!strchr(p, '\n')) {
142             while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
143                 ;
144             continue;
145         }
146         while (isspace(*p))
147             ++p;
148         if (*p && *p != '#')
149             break;
150     }
151
152     zapchar = 0;
153     tty.ty_name = p;
154     p = skip(p);
155     if (!*(tty.ty_getty = p))
156         tty.ty_getty = tty.ty_type = NULL;
157     else {
158         p = skip(p);
159         if (!*(tty.ty_type = p))
160             tty.ty_type = NULL;
161         else
162             p = skip(p);
163     }
164     tty.ty_status = 0;
165     tty.ty_window = NULL;
166
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)) {
170         if (scmp(_TTYS_OFF))
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);
178         else
179             break;
180     }
181     /* We can release the lock only here since `zapchar' is global.  */
182         __STDIO_ALWAYS_THREADUNLOCK(tf);
183
184     if (zapchar == '#' || *p == '#')
185         while ((c = *++p) == ' ' || c == '\t')
186             ;
187     tty.ty_comment = p;
188     if (*p == 0)
189         tty.ty_comment = 0;
190     if ((p = strchr(p, '\n')))
191         *p = '\0';
192     return (&tty);
193 }
194 strong_alias(__getttyent,getttyent)
195
196 int attribute_hidden __endttyent(void)
197 {
198     int rval;
199
200     if (tf) {
201         rval = !(fclose(tf) == EOF);
202         tf = NULL;
203         return (rval);
204     }
205     return (1);
206 }
207 strong_alias(__endttyent,endttyent)
208
209 struct ttyent * getttynam(const char *tty)
210 {
211     register struct ttyent *t;
212
213     __setttyent();
214     while ((t = __getttyent()))
215         if (!strcmp(tty, t->ty_name))
216             break;
217     __endttyent();
218     return (t);
219 }