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