1 /* $NetBSD: getttyent.c,v 1.26 2013/06/30 10:07:43 martin 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.26 2013/06/30 10:07:43 martin Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
52 #include <sys/sysctl.h>
53 #include <sys/utsname.h>
56 __weak_alias(endttyent
,_endttyent
)
57 __weak_alias(getttyent
,_getttyent
)
58 __weak_alias(getttynam
,_getttynam
)
59 __weak_alias(setttyent
,_setttyent
)
60 __weak_alias(setttyentpath
,_setttyentpath
)
64 static size_t lineno
= 0;
65 static char *skip(char *, char *);
66 static char *value(char *);
69 getttynam(const char *tty
)
73 _DIAGASSERT(tty
!= NULL
);
76 while ((t
= getttyent()) != NULL
)
77 if (!strcmp(tty
, t
->ty_name
))
86 static struct ttyent tty
;
90 static char *line
= NULL
;
96 if (!tf
&& !setttyent())
101 line
= fparseln(tf
, &len
, &lineno
, NULL
, FPARSELN_UNESCALL
);
107 for (p
= line
; *p
&& isspace((unsigned char)*p
); p
++)
115 p
= skip(p
, &zapchar
);
116 if (*(tty
.ty_getty
= p
) == '\0')
117 tty
.ty_getty
= tty
.ty_type
= NULL
;
119 p
= skip(p
, &zapchar
);
120 if (*(tty
.ty_type
= p
) == '\0')
123 p
= skip(p
, &zapchar
);
126 tty
.ty_window
= NULL
;
129 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
130 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
131 for (; *p
; p
= skip(p
, &zapchar
)) {
133 tty
.ty_status
&= ~TTY_ON
;
134 else if (scmp(_TTYS_ON
))
135 tty
.ty_status
|= TTY_ON
;
136 else if (scmp(_TTYS_SECURE
))
137 tty
.ty_status
|= TTY_SECURE
;
138 else if (scmp(_TTYS_LOCAL
))
139 tty
.ty_status
|= TTY_LOCAL
;
140 else if (scmp(_TTYS_RTSCTS
))
141 tty
.ty_status
|= TTY_RTSCTS
;
142 else if (scmp(_TTYS_DTRCTS
))
143 tty
.ty_status
|= TTY_DTRCTS
;
144 else if (scmp(_TTYS_SOFTCAR
))
145 tty
.ty_status
|= TTY_SOFTCAR
;
146 else if (scmp(_TTYS_MDMBUF
))
147 tty
.ty_status
|= TTY_MDMBUF
;
148 else if (vcmp(_TTYS_WINDOW
))
149 tty
.ty_window
= value(p
);
150 else if (vcmp(_TTYS_CLASS
))
151 tty
.ty_class
= value(p
);
153 warnx("%s: %s, %lu: unknown option `%s'",
154 __func__
, _PATH_TTYS
, (unsigned long)lineno
, p
);
157 if (zapchar
== '#' || *p
== '#')
158 while ((c
= *++p
) == ' ' || c
== '\t')
162 tty
.ty_comment
= NULL
;
163 if ((p
= strchr(p
, '\n')) != NULL
)
171 * Skip over the current field, removing quotes, and return a pointer to
175 skip(char *p
, char *zapchar
)
180 _DIAGASSERT(p
!= NULL
);
183 for (q
= 0, t
= p
; (c
= *p
) != '\0'; p
++) {
185 q
^= QUOTED
; /* obscure, but nice */
188 if (q
== QUOTED
&& *p
== '\\' && *(p
+1) == '"')
199 if (c
== '\t' || c
== ' ' || c
== '\n') {
202 while ((c
= *p
) == '\t' || c
== ' ' || c
== '\n')
217 _DIAGASSERT(p
!= NULL
);
219 return (p
= strchr(p
, '=')) != NULL
? ++p
: NULL
;
223 setttyentpath(const char *path
)
232 * Try <path>.$MACHINE (e.g. etc/ttys.amd64)
235 char machine
[_SYS_NMLN
];
236 const int mib
[] = { [0] = CTL_HW
, [1] = HW_MACHINE
, };
237 size_t len
= sizeof(machine
);
239 if (sysctl(mib
, (u_int
)__arraycount(mib
), machine
, &len
,
241 char npath
[PATH_MAX
];
242 (void)snprintf(npath
, sizeof(npath
), "%s.%s", path
,
244 if ((tf
= fopen(npath
, "re")) != NULL
)
249 if ((tf
= fopen(path
, "re")) != NULL
)
257 return setttyentpath(_PATH_TTYS
);
266 rval
= !(fclose(tf
) == EOF
);