GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/gen/getttyent.c Lines: 0 82 0.0 %
Date: 2017-11-13 Branches: 0 94 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: getttyent.c,v 1.15 2015/09/13 11:47:54 guenther Exp $ */
2
/*
3
 * Copyright (c) 1989, 1993
4
 *	The Regents of the University of California.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. Neither the name of the University nor the names of its contributors
15
 *    may be used to endorse or promote products derived from this software
16
 *    without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include <ttyent.h>
32
#include <stdio.h>
33
#include <ctype.h>
34
#include <string.h>
35
36
static char zapchar;
37
static FILE *tf;
38
39
static char *skip(char *);
40
static char *value(char *);
41
42
struct ttyent *
43
getttynam(const char *tty)
44
{
45
	struct ttyent *t;
46
47
	setttyent();
48
	while ((t = getttyent()))
49
		if (!strcmp(tty, t->ty_name))
50
			break;
51
	endttyent();
52
	return (t);
53
}
54
55
struct ttyent *
56
getttyent(void)
57
{
58
	static struct ttyent tty;
59
	int c;
60
	char *p;
61
#define	MAXLINELENGTH	200
62
	static char line[MAXLINELENGTH];
63
64
	if (!tf && !setttyent())
65
		return (NULL);
66
	for (;;) {
67
		if (!fgets(p = line, sizeof(line), tf))
68
			return (NULL);
69
		/* skip lines that are too big */
70
		if (!strchr(p, '\n')) {
71
			while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
72
				;
73
			continue;
74
		}
75
		while (isspace((unsigned char)*p))
76
			++p;
77
		if (*p && *p != '#')
78
			break;
79
	}
80
81
	zapchar = 0;
82
	tty.ty_name = p;
83
	p = skip(p);
84
	if (!*(tty.ty_getty = p))
85
		tty.ty_getty = tty.ty_type = NULL;
86
	else {
87
		p = skip(p);
88
		if (!*(tty.ty_type = p))
89
			tty.ty_type = NULL;
90
		else
91
			p = skip(p);
92
	}
93
	tty.ty_status = 0;
94
	tty.ty_window = NULL;
95
96
#define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && \
97
		isspace((unsigned char)p[sizeof(e) - 1])
98
#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
99
	for (; *p; p = skip(p)) {
100
		if (scmp(_TTYS_OFF))
101
			tty.ty_status &= ~TTY_ON;
102
		else if (scmp(_TTYS_ON))
103
			tty.ty_status |= TTY_ON;
104
		else if (scmp(_TTYS_SECURE))
105
			tty.ty_status |= TTY_SECURE;
106
		else if (scmp(_TTYS_LOCAL))
107
			tty.ty_status |= TTY_LOCAL;
108
		else if (scmp(_TTYS_RTSCTS))
109
			tty.ty_status |= TTY_RTSCTS;
110
		else if (scmp(_TTYS_SOFTCAR))
111
			tty.ty_status |= TTY_SOFTCAR;
112
		else if (scmp(_TTYS_MDMBUF))
113
			tty.ty_status |= TTY_MDMBUF;
114
		else if (vcmp(_TTYS_WINDOW))
115
			tty.ty_window = value(p);
116
		else
117
			break;
118
	}
119
120
	if (zapchar == '#' || *p == '#')
121
		while ((c = *++p) == ' ' || c == '\t')
122
			;
123
	tty.ty_comment = p;
124
	if (*p == 0)
125
		tty.ty_comment = 0;
126
	if ((p = strchr(p, '\n')))
127
		*p = '\0';
128
	return (&tty);
129
}
130
DEF_WEAK(getttyent);
131
132
#define	QUOTED	1
133
134
/*
135
 * Skip over the current field, removing quotes, and return a pointer to
136
 * the next field.
137
 */
138
static char *
139
skip(char *p)
140
{
141
	char *t;
142
	int c, q;
143
144
	for (q = 0, t = p; (c = *p) != '\0'; p++) {
145
		if (c == '"') {
146
			q ^= QUOTED;	/* obscure, but nice */
147
			continue;
148
		}
149
		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
150
			p++;
151
		*t++ = *p;
152
		if (q == QUOTED)
153
			continue;
154
		if (c == '#') {
155
			zapchar = c;
156
			*p = 0;
157
			break;
158
		}
159
		if (c == '\t' || c == ' ' || c == '\n') {
160
			zapchar = c;
161
			*p++ = 0;
162
			while ((c = *p) == '\t' || c == ' ' || c == '\n')
163
				p++;
164
			break;
165
		}
166
	}
167
	*--t = '\0';
168
	return (p);
169
}
170
171
static char *
172
value(char *p)
173
{
174
175
	return ((p = strchr(p, '=')) ? ++p : NULL);
176
}
177
178
int
179
setttyent(void)
180
{
181
182
	if (tf) {
183
		rewind(tf);
184
		return (1);
185
	} else if ((tf = fopen(_PATH_TTYS, "re")))
186
		return (1);
187
	return (0);
188
}
189
DEF_WEAK(setttyent);
190
191
int
192
endttyent(void)
193
{
194
	int rval;
195
196
	if (tf) {
197
		rval = !(fclose(tf) == EOF);
198
		tf = NULL;
199
		return (rval);
200
	}
201
	return (1);
202
}
203
DEF_WEAK(endttyent);