1 /* $NetBSD: getttyent.c,v 1.23 2006/04/17 23:29:21 salo Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93";
37 __RCSID("$NetBSD: getttyent.c,v 1.23 2006/04/17 23:29:21 salo Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
53 __weak_alias(endttyent
,_endttyent
)
54 __weak_alias(getttyent
,_getttyent
)
55 __weak_alias(getttynam
,_getttynam
)
56 __weak_alias(setttyent
,_setttyent
)
57 __weak_alias(setttyentpath
,_setttyentpath
)
61 static size_t lineno
= 0;
62 static char *skip(char *, char *);
63 static char *value(char *);
66 getttynam(const char *tty
)
70 _DIAGASSERT(tty
!= NULL
);
73 while ((t
= getttyent()) != NULL
)
74 if (!strcmp(tty
, t
->ty_name
))
83 static struct ttyent tty
;
87 static char *line
= NULL
;
93 if (!tf
&& !setttyent())
98 line
= fparseln(tf
, &len
, &lineno
, NULL
, FPARSELN_UNESCALL
);
101 warn("%s", __func__
);
104 for (p
= line
; *p
&& isspace((unsigned char)*p
); p
++)
112 p
= skip(p
, &zapchar
);
113 if (*(tty
.ty_getty
= p
) == '\0')
114 tty
.ty_getty
= tty
.ty_type
= NULL
;
116 p
= skip(p
, &zapchar
);
117 if (*(tty
.ty_type
= p
) == '\0')
120 p
= skip(p
, &zapchar
);
123 tty
.ty_window
= NULL
;
126 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
127 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
128 for (; *p
; p
= skip(p
, &zapchar
)) {
130 tty
.ty_status
&= ~TTY_ON
;
131 else if (scmp(_TTYS_ON
))
132 tty
.ty_status
|= TTY_ON
;
133 else if (scmp(_TTYS_SECURE
))
134 tty
.ty_status
|= TTY_SECURE
;
135 else if (scmp(_TTYS_LOCAL
))
136 tty
.ty_status
|= TTY_LOCAL
;
137 else if (scmp(_TTYS_RTSCTS
))
138 tty
.ty_status
|= TTY_RTSCTS
;
139 else if (scmp(_TTYS_DTRCTS
))
140 tty
.ty_status
|= TTY_DTRCTS
;
141 else if (scmp(_TTYS_SOFTCAR
))
142 tty
.ty_status
|= TTY_SOFTCAR
;
143 else if (scmp(_TTYS_MDMBUF
))
144 tty
.ty_status
|= TTY_MDMBUF
;
145 else if (vcmp(_TTYS_WINDOW
))
146 tty
.ty_window
= value(p
);
147 else if (vcmp(_TTYS_CLASS
))
148 tty
.ty_class
= value(p
);
150 warnx("%s: %s, %lu: unknown option `%s'",
151 __func__
, _PATH_TTYS
, (unsigned long)lineno
, p
);
154 if (zapchar
== '#' || *p
== '#')
155 while ((c
= *++p
) == ' ' || c
== '\t')
159 tty
.ty_comment
= NULL
;
160 if ((p
= strchr(p
, '\n')) != NULL
)
168 * Skip over the current field, removing quotes, and return a pointer to
172 skip(char *p
, char *zapchar
)
177 _DIAGASSERT(p
!= NULL
);
180 for (q
= 0, t
= p
; (c
= *p
) != '\0'; p
++) {
182 q
^= QUOTED
; /* obscure, but nice */
185 if (q
== QUOTED
&& *p
== '\\' && *(p
+1) == '"')
196 if (c
== '\t' || c
== ' ' || c
== '\n') {
199 while ((c
= *p
) == '\t' || c
== ' ' || c
== '\n')
214 _DIAGASSERT(p
!= NULL
);
216 return (p
= strchr(p
, '=')) != NULL
? ++p
: NULL
;
220 setttyentpath(const char *path
)
226 } else if ((tf
= fopen(path
, "r")) != NULL
)
234 return setttyentpath(_PATH_TTYS
);
243 rval
= !(fclose(tf
) == EOF
);