OSDN Git Service

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