1*84d9c625SLionel Sambuc /* $NetBSD: getttyent.c,v 1.26 2013/06/30 10:07:43 martin Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1989, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93";
362fe8fb19SBen Gras #else
37*84d9c625SLionel Sambuc __RCSID("$NetBSD: getttyent.c,v 1.26 2013/06/30 10:07:43 martin Exp $");
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
402fe8fb19SBen Gras
412fe8fb19SBen Gras #include "namespace.h"
422fe8fb19SBen Gras
432fe8fb19SBen Gras #include <assert.h>
442fe8fb19SBen Gras #include <ctype.h>
452fe8fb19SBen Gras #include <stdio.h>
462fe8fb19SBen Gras #include <string.h>
472fe8fb19SBen Gras #include <ttyent.h>
482fe8fb19SBen Gras #include <errno.h>
492fe8fb19SBen Gras #include <err.h>
502fe8fb19SBen Gras #include <stdlib.h>
512fe8fb19SBen Gras
52*84d9c625SLionel Sambuc #include <sys/sysctl.h>
53*84d9c625SLionel Sambuc #include <sys/utsname.h>
54*84d9c625SLionel Sambuc
552fe8fb19SBen Gras #ifdef __weak_alias
562fe8fb19SBen Gras __weak_alias(endttyent,_endttyent)
572fe8fb19SBen Gras __weak_alias(getttyent,_getttyent)
582fe8fb19SBen Gras __weak_alias(getttynam,_getttynam)
592fe8fb19SBen Gras __weak_alias(setttyent,_setttyent)
602fe8fb19SBen Gras __weak_alias(setttyentpath,_setttyentpath)
612fe8fb19SBen Gras #endif
622fe8fb19SBen Gras
632fe8fb19SBen Gras static FILE *tf;
642fe8fb19SBen Gras static size_t lineno = 0;
652fe8fb19SBen Gras static char *skip(char *, char *);
662fe8fb19SBen Gras static char *value(char *);
672fe8fb19SBen Gras
682fe8fb19SBen Gras struct ttyent *
getttynam(const char * tty)692fe8fb19SBen Gras getttynam(const char *tty)
702fe8fb19SBen Gras {
712fe8fb19SBen Gras struct ttyent *t;
722fe8fb19SBen Gras
732fe8fb19SBen Gras _DIAGASSERT(tty != NULL);
742fe8fb19SBen Gras
752fe8fb19SBen Gras setttyent();
762fe8fb19SBen Gras while ((t = getttyent()) != NULL)
772fe8fb19SBen Gras if (!strcmp(tty, t->ty_name))
782fe8fb19SBen Gras break;
792fe8fb19SBen Gras endttyent();
802fe8fb19SBen Gras return (t);
812fe8fb19SBen Gras }
822fe8fb19SBen Gras
832fe8fb19SBen Gras struct ttyent *
getttyent(void)842fe8fb19SBen Gras getttyent(void)
852fe8fb19SBen Gras {
862fe8fb19SBen Gras static struct ttyent tty;
872fe8fb19SBen Gras int c;
882fe8fb19SBen Gras char *p;
892fe8fb19SBen Gras size_t len;
902fe8fb19SBen Gras static char *line = NULL;
912fe8fb19SBen Gras char zapchar;
922fe8fb19SBen Gras
932fe8fb19SBen Gras if (line)
942fe8fb19SBen Gras free(line);
952fe8fb19SBen Gras
962fe8fb19SBen Gras if (!tf && !setttyent())
972fe8fb19SBen Gras return NULL;
982fe8fb19SBen Gras
992fe8fb19SBen Gras for (;;) {
1002fe8fb19SBen Gras errno = 0;
1012fe8fb19SBen Gras line = fparseln(tf, &len, &lineno, NULL, FPARSELN_UNESCALL);
1022fe8fb19SBen Gras if (line == NULL) {
1032fe8fb19SBen Gras if (errno != 0)
104*84d9c625SLionel Sambuc warn(__func__);
1052fe8fb19SBen Gras return NULL;
1062fe8fb19SBen Gras }
1072fe8fb19SBen Gras for (p = line; *p && isspace((unsigned char)*p); p++)
1082fe8fb19SBen Gras continue;
1092fe8fb19SBen Gras if (*p && *p != '#')
1102fe8fb19SBen Gras break;
1112fe8fb19SBen Gras free(line);
1122fe8fb19SBen Gras }
1132fe8fb19SBen Gras
1142fe8fb19SBen Gras tty.ty_name = p;
1152fe8fb19SBen Gras p = skip(p, &zapchar);
1162fe8fb19SBen Gras if (*(tty.ty_getty = p) == '\0')
1172fe8fb19SBen Gras tty.ty_getty = tty.ty_type = NULL;
1182fe8fb19SBen Gras else {
1192fe8fb19SBen Gras p = skip(p, &zapchar);
1202fe8fb19SBen Gras if (*(tty.ty_type = p) == '\0')
1212fe8fb19SBen Gras tty.ty_type = NULL;
1222fe8fb19SBen Gras else
1232fe8fb19SBen Gras p = skip(p, &zapchar);
1242fe8fb19SBen Gras }
1252fe8fb19SBen Gras tty.ty_status = 0;
1262fe8fb19SBen Gras tty.ty_window = NULL;
1272fe8fb19SBen Gras tty.ty_class = NULL;
1282fe8fb19SBen Gras
1292fe8fb19SBen Gras #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
1302fe8fb19SBen Gras #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
1312fe8fb19SBen Gras for (; *p; p = skip(p, &zapchar)) {
1322fe8fb19SBen Gras if (scmp(_TTYS_OFF))
1332fe8fb19SBen Gras tty.ty_status &= ~TTY_ON;
1342fe8fb19SBen Gras else if (scmp(_TTYS_ON))
1352fe8fb19SBen Gras tty.ty_status |= TTY_ON;
1362fe8fb19SBen Gras else if (scmp(_TTYS_SECURE))
1372fe8fb19SBen Gras tty.ty_status |= TTY_SECURE;
1382fe8fb19SBen Gras else if (scmp(_TTYS_LOCAL))
1392fe8fb19SBen Gras tty.ty_status |= TTY_LOCAL;
1402fe8fb19SBen Gras else if (scmp(_TTYS_RTSCTS))
1412fe8fb19SBen Gras tty.ty_status |= TTY_RTSCTS;
1422fe8fb19SBen Gras else if (scmp(_TTYS_DTRCTS))
1432fe8fb19SBen Gras tty.ty_status |= TTY_DTRCTS;
1442fe8fb19SBen Gras else if (scmp(_TTYS_SOFTCAR))
1452fe8fb19SBen Gras tty.ty_status |= TTY_SOFTCAR;
1462fe8fb19SBen Gras else if (scmp(_TTYS_MDMBUF))
1472fe8fb19SBen Gras tty.ty_status |= TTY_MDMBUF;
1482fe8fb19SBen Gras else if (vcmp(_TTYS_WINDOW))
1492fe8fb19SBen Gras tty.ty_window = value(p);
1502fe8fb19SBen Gras else if (vcmp(_TTYS_CLASS))
1512fe8fb19SBen Gras tty.ty_class = value(p);
1522fe8fb19SBen Gras else
1532fe8fb19SBen Gras warnx("%s: %s, %lu: unknown option `%s'",
1542fe8fb19SBen Gras __func__, _PATH_TTYS, (unsigned long)lineno, p);
1552fe8fb19SBen Gras }
1562fe8fb19SBen Gras
1572fe8fb19SBen Gras if (zapchar == '#' || *p == '#')
1582fe8fb19SBen Gras while ((c = *++p) == ' ' || c == '\t')
1592fe8fb19SBen Gras continue;
1602fe8fb19SBen Gras tty.ty_comment = p;
1612fe8fb19SBen Gras if (*p == '\0')
1622fe8fb19SBen Gras tty.ty_comment = NULL;
1632fe8fb19SBen Gras if ((p = strchr(p, '\n')) != NULL)
1642fe8fb19SBen Gras *p = '\0';
1652fe8fb19SBen Gras return &tty;
1662fe8fb19SBen Gras }
1672fe8fb19SBen Gras
1682fe8fb19SBen Gras #define QUOTED 1
1692fe8fb19SBen Gras
1702fe8fb19SBen Gras /*
1712fe8fb19SBen Gras * Skip over the current field, removing quotes, and return a pointer to
1722fe8fb19SBen Gras * the next field.
1732fe8fb19SBen Gras */
1742fe8fb19SBen Gras static char *
skip(char * p,char * zapchar)1752fe8fb19SBen Gras skip(char *p, char *zapchar)
1762fe8fb19SBen Gras {
1772fe8fb19SBen Gras char *t;
1782fe8fb19SBen Gras int c, q;
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras _DIAGASSERT(p != NULL);
1812fe8fb19SBen Gras *zapchar = '\0';
1822fe8fb19SBen Gras
1832fe8fb19SBen Gras for (q = 0, t = p; (c = *p) != '\0'; p++) {
1842fe8fb19SBen Gras if (c == '"') {
1852fe8fb19SBen Gras q ^= QUOTED; /* obscure, but nice */
1862fe8fb19SBen Gras continue;
1872fe8fb19SBen Gras }
1882fe8fb19SBen Gras if (q == QUOTED && *p == '\\' && *(p+1) == '"')
1892fe8fb19SBen Gras p++;
1902fe8fb19SBen Gras *t++ = *p;
1912fe8fb19SBen Gras if (q == QUOTED)
1922fe8fb19SBen Gras continue;
1932fe8fb19SBen Gras if (c == '#') {
1942fe8fb19SBen Gras *zapchar = c;
1952fe8fb19SBen Gras *p = '\0';
1962fe8fb19SBen Gras *--t = '\0';
1972fe8fb19SBen Gras return p;
1982fe8fb19SBen Gras }
1992fe8fb19SBen Gras if (c == '\t' || c == ' ' || c == '\n') {
2002fe8fb19SBen Gras *zapchar = c;
2012fe8fb19SBen Gras *p++ = '\0';
2022fe8fb19SBen Gras while ((c = *p) == '\t' || c == ' ' || c == '\n')
2032fe8fb19SBen Gras p++;
2042fe8fb19SBen Gras *--t = '\0';
2052fe8fb19SBen Gras return p;
2062fe8fb19SBen Gras }
2072fe8fb19SBen Gras }
2082fe8fb19SBen Gras if (t != p)
2092fe8fb19SBen Gras *t = '\0';
2102fe8fb19SBen Gras return p;
2112fe8fb19SBen Gras }
2122fe8fb19SBen Gras
2132fe8fb19SBen Gras static char *
value(char * p)2142fe8fb19SBen Gras value(char *p)
2152fe8fb19SBen Gras {
2162fe8fb19SBen Gras
2172fe8fb19SBen Gras _DIAGASSERT(p != NULL);
2182fe8fb19SBen Gras
2192fe8fb19SBen Gras return (p = strchr(p, '=')) != NULL ? ++p : NULL;
2202fe8fb19SBen Gras }
2212fe8fb19SBen Gras
2222fe8fb19SBen Gras int
setttyentpath(const char * path)2232fe8fb19SBen Gras setttyentpath(const char *path)
2242fe8fb19SBen Gras {
2252fe8fb19SBen Gras lineno = 0;
2262fe8fb19SBen Gras if (tf) {
2272fe8fb19SBen Gras rewind(tf);
2282fe8fb19SBen Gras return 1;
229*84d9c625SLionel Sambuc }
230*84d9c625SLionel Sambuc
231*84d9c625SLionel Sambuc /*
232*84d9c625SLionel Sambuc * Try <path>.$MACHINE (e.g. etc/ttys.amd64)
233*84d9c625SLionel Sambuc */
234*84d9c625SLionel Sambuc {
235*84d9c625SLionel Sambuc char machine[_SYS_NMLN];
236*84d9c625SLionel Sambuc const int mib[] = { [0] = CTL_HW, [1] = HW_MACHINE, };
237*84d9c625SLionel Sambuc size_t len = sizeof(machine);
238*84d9c625SLionel Sambuc
239*84d9c625SLionel Sambuc if (sysctl(mib, (u_int)__arraycount(mib), machine, &len,
240*84d9c625SLionel Sambuc NULL, 0) != -1) {
241*84d9c625SLionel Sambuc char npath[PATH_MAX];
242*84d9c625SLionel Sambuc (void)snprintf(npath, sizeof(npath), "%s.%s", path,
243*84d9c625SLionel Sambuc machine);
244*84d9c625SLionel Sambuc if ((tf = fopen(npath, "re")) != NULL)
245*84d9c625SLionel Sambuc return 1;
246*84d9c625SLionel Sambuc }
247*84d9c625SLionel Sambuc }
248*84d9c625SLionel Sambuc
249*84d9c625SLionel Sambuc if ((tf = fopen(path, "re")) != NULL)
2502fe8fb19SBen Gras return 1;
2512fe8fb19SBen Gras return 0;
2522fe8fb19SBen Gras }
2532fe8fb19SBen Gras
2542fe8fb19SBen Gras int
setttyent(void)2552fe8fb19SBen Gras setttyent(void)
2562fe8fb19SBen Gras {
2572fe8fb19SBen Gras return setttyentpath(_PATH_TTYS);
2582fe8fb19SBen Gras }
2592fe8fb19SBen Gras
2602fe8fb19SBen Gras int
endttyent(void)2612fe8fb19SBen Gras endttyent(void)
2622fe8fb19SBen Gras {
2632fe8fb19SBen Gras int rval;
2642fe8fb19SBen Gras
2652fe8fb19SBen Gras if (tf) {
2662fe8fb19SBen Gras rval = !(fclose(tf) == EOF);
2672fe8fb19SBen Gras tf = NULL;
2682fe8fb19SBen Gras return rval;
2692fe8fb19SBen Gras }
2702fe8fb19SBen Gras return 1;
2712fe8fb19SBen Gras }
272