OSDN Git Service

923c45ab06e1fd3aeb70c412c5f3d7b37fd30277
[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 __fsetlocking __fsetlocking_internal
31
32 #define _GNU_SOURCE
33 #include <features.h>
34 #include <ttyent.h>
35 #include <stdio.h>
36 #include <stdio_ext.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #ifdef __UCLIBC_HAS_THREADS__
41 #include <pthread.h>
42 #endif
43
44 static char zapchar;
45 static FILE *tf;
46 static struct ttyent tty;
47
48
49 struct ttyent * getttynam(const char *tty)
50 {
51     register struct ttyent *t;
52
53     setttyent();
54     while ((t = getttyent()))
55         if (!__strcmp(tty, t->ty_name))
56             break;
57     endttyent();
58     return (t);
59 }
60
61
62 /* Skip over the current field, removing quotes, and return 
63  * a pointer to the next field.
64  */
65 #define QUOTED  1
66 static char * skip(register char *p)
67 {
68     register char *t;
69     register int c, q;
70
71     for (q = 0, t = p; (c = *p) != '\0'; p++) {
72         if (c == '"') {
73             q ^= QUOTED;        /* obscure, but nice */
74             continue;
75         }
76         if (q == QUOTED && *p == '\\' && *(p+1) == '"')
77             p++;
78         *t++ = *p;
79         if (q == QUOTED)
80             continue;
81         if (c == '#') {
82             zapchar = c;
83             *p = 0;
84             break;
85         }
86         if (c == '\t' || c == ' ' || c == '\n') {
87             zapchar = c;
88             *p++ = 0;
89             while ((c = *p) == '\t' || c == ' ' || c == '\n')
90                 p++;
91             break;
92         }
93     }
94     *--t = '\0';
95     return (p);
96 }
97
98 static char * value(register char *p)
99 {
100
101     return ((p = __strchr(p, '=')) ? ++p : NULL);
102 }
103
104 struct ttyent * getttyent(void)
105 {
106     register int c;
107     register char *p;
108     static char *line = NULL;
109
110     if (!tf && !setttyent())
111         return (NULL);
112
113     if (!line) {
114             line = malloc(BUFSIZ);
115                 if (!line)
116                     abort();
117     }
118
119         __STDIO_ALWAYS_THREADLOCK(tf);
120
121     for (;;) {
122         if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
123                 __STDIO_ALWAYS_THREADUNLOCK(tf);
124             return (NULL);
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     /* We can release the lock only here since `zapchar' is global.  */
168         __STDIO_ALWAYS_THREADUNLOCK(tf);
169
170     if (zapchar == '#' || *p == '#')
171         while ((c = *++p) == ' ' || c == '\t')
172             ;
173     tty.ty_comment = p;
174     if (*p == 0)
175         tty.ty_comment = 0;
176     if ((p = __strchr(p, '\n')))
177         *p = '\0';
178     return (&tty);
179 }
180
181 int setttyent(void)
182 {
183
184     if (tf) {
185         rewind(tf);
186         return (1);
187     } else if ((tf = fopen(_PATH_TTYS, "r"))) {
188         /* We do the locking ourselves.  */
189 #ifdef __UCLIBC_HAS_THREADS__
190         __fsetlocking (tf, FSETLOCKING_BYCALLER);
191 #endif
192         return (1);
193     }
194     return (0);
195 }
196
197 int endttyent(void)
198 {
199     int rval;
200
201     if (tf) {
202         rval = !(fclose(tf) == EOF);
203         tf = NULL;
204         return (rval);
205     }
206     return (1);
207 }