]> git.saurik.com Git - apple/libc.git/blob - gen/getttyent.c
723077bb79323d0c025ea57d5ba146d3902678d6
[apple/libc.git] / gen / getttyent.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56
57 #include <ttyent.h>
58 #include <stdio.h>
59 #include <ctype.h>
60 #include <string.h>
61 #include <stdlib.h>
62
63 static char zapchar;
64 static FILE *tf;
65
66 struct ttyent *
67 getttynam(tty)
68 const char *tty;
69 {
70 register struct ttyent *t;
71
72 setttyent();
73 while (t = getttyent())
74 if (!strcmp(tty, t->ty_name))
75 break;
76 endttyent();
77 return (t);
78 }
79
80 struct ttyent *
81 getttyent()
82 {
83 static struct ttyent tty;
84 register int c;
85 register char *p;
86 #define MAXLINELENGTH 1024
87 static char *line = NULL;
88 static char *skip(), *value();
89
90 if ( line == NULL ) {
91 line = malloc(MAXLINELENGTH);
92 if( line == NULL )
93 return NULL;
94 }
95
96 if (!tf && !setttyent())
97 return (NULL);
98 for (;;) {
99 if (!fgets(p = line, MAXLINELENGTH, tf))
100 return (NULL);
101 /* skip lines that are too big */
102 if (!index(p, '\n')) {
103 while ((c = getc(tf)) != '\n' && c != EOF)
104 ;
105 continue;
106 }
107 while (isspace(*p))
108 ++p;
109 if (*p && *p != '#')
110 break;
111 }
112
113 zapchar = 0;
114 tty.ty_name = p;
115 p = skip(p);
116 if (!*(tty.ty_getty = p))
117 tty.ty_getty = tty.ty_type = NULL;
118 else {
119 p = skip(p);
120 if (!*(tty.ty_type = p))
121 tty.ty_type = NULL;
122 else
123 p = skip(p);
124 }
125 tty.ty_status = 0;
126 tty.ty_window = NULL;
127 tty.ty_onerror = NULL;
128 tty.ty_onoption = NULL;
129
130 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
131 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
132 for (; *p; p = skip(p)) {
133 if (scmp(_TTYS_OFF))
134 tty.ty_status &= ~TTY_ON;
135 else if (scmp(_TTYS_ON))
136 tty.ty_status |= TTY_ON;
137 else if (scmp(_TTYS_SECURE))
138 tty.ty_status |= TTY_SECURE;
139 else if (vcmp(_TTYS_WINDOW))
140 tty.ty_window = value(p);
141 else if (vcmp(_TTYS_ONERROR))
142 tty.ty_onerror = value(p);
143 else if (vcmp(_TTYS_ONOPTION))
144 tty.ty_onoption = value(p);
145 else
146 break;
147 }
148
149 if (zapchar == '#' || *p == '#')
150 while ((c = *++p) == ' ' || c == '\t')
151 ;
152 tty.ty_comment = p;
153 if (*p == 0)
154 tty.ty_comment = 0;
155 if (p = index(p, '\n'))
156 *p = '\0';
157 return (&tty);
158 }
159
160 #define QUOTED 1
161
162 /*
163 * Skip over the current field, removing quotes, and return a pointer to
164 * the next field.
165 */
166 static char *
167 skip(p)
168 register char *p;
169 {
170 register char *t;
171 register int c, q;
172
173 for (q = 0, t = p; (c = *p) != '\0'; p++) {
174 if (c == '"') {
175 q ^= QUOTED; /* obscure, but nice */
176 continue;
177 }
178 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
179 p++;
180 *t++ = *p;
181 if (q == QUOTED)
182 continue;
183 if (c == '#') {
184 zapchar = c;
185 *p = 0;
186 break;
187 }
188 if (c == '\t' || c == ' ' || c == '\n') {
189 zapchar = c;
190 *p++ = 0;
191 while ((c = *p) == '\t' || c == ' ' || c == '\n')
192 p++;
193 break;
194 }
195 }
196 *--t = '\0';
197 return (p);
198 }
199
200 static char *
201 value(p)
202 register char *p;
203 {
204
205 return ((p = index(p, '=')) ? ++p : NULL);
206 }
207
208 int
209 setttyent()
210 {
211
212 if (tf) {
213 (void)rewind(tf);
214 return (1);
215 } else if (tf = fopen(_PATH_TTYS, "r"))
216 return (1);
217 return (0);
218 }
219
220 int
221 endttyent()
222 {
223 int rval;
224
225 if (tf) {
226 rval = !(fclose(tf) == EOF);
227 tf = NULL;
228 return (rval);
229 }
230 return (1);
231 }