GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/ttyflags/ttyflags.c Lines: 0 102 0.0 %
Date: 2017-11-07 Branches: 0 81 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ttyflags.c,v 1.13 2012/12/04 02:27:00 deraadt Exp $	*/
2
/*	$NetBSD: ttyflags.c,v 1.8 1996/04/09 05:20:30 cgd Exp $	*/
3
4
/*
5
 * Copyright (c) 1996 Theo de Raadt
6
 * Copyright (c) 1994 Christopher G. Demetriou
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. All advertising materials mentioning features or use of this software
18
 *    must display the following acknowledgement:
19
 *      This product includes software developed by Christopher G. Demetriou.
20
 * 4. The name of the author may not be used to endorse or promote products
21
 *    derived from this software without specific prior written permission
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
#include <sys/types.h>
36
#include <sys/ioctl.h>
37
38
#include <err.h>
39
#include <errno.h>
40
#include <fcntl.h>
41
#include <limits.h>
42
#include <paths.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <ttyent.h>
47
#include <unistd.h>
48
49
int all(int);
50
int ttys(char **, int);
51
int ttyflags(struct ttyent *, int);
52
void usage(void);
53
54
int nflag, vflag;
55
56
/*
57
 * Ttyflags sets the device-specific tty flags, based on the contents
58
 * of /etc/ttys.  It can either set all of the ttys' flags, or set
59
 * the flags of the ttys specified on the command line.
60
 */
61
int
62
main(int argc, char *argv[])
63
{
64
	int aflag, ch, rval, pflag = 0;
65
66
	aflag = nflag = vflag = 0;
67
	while ((ch = getopt(argc, argv, "panv")) != -1)
68
		switch (ch) {
69
		case 'a':
70
			aflag = 1;
71
			break;
72
		case 'n':		/* undocumented */
73
			nflag = 1;
74
			break;
75
		case 'p':
76
			pflag = 1;
77
			break;
78
		case 'v':
79
			vflag = 1;
80
			break;
81
		case '?':
82
		default:
83
			usage();
84
		}
85
	argc -= optind;
86
	argv += optind;
87
88
	if (aflag && argc != 0)
89
		usage();
90
91
	if (setttyent() == 0)
92
		err(1, "setttyent");
93
94
	if (aflag)
95
		rval = all(pflag);
96
	else
97
		rval = ttys(argv, pflag);
98
99
	if (endttyent() == 0)
100
		warn("endttyent");
101
102
	exit(rval);
103
}
104
105
/*
106
 * Change all /etc/ttys entries' flags.
107
 */
108
int
109
all(int print)
110
{
111
	struct ttyent *tep;
112
	int rval;
113
114
	rval = 0;
115
	for (tep = getttyent(); tep != NULL; tep = getttyent())
116
		if (ttyflags(tep, print))
117
			rval = 1;
118
	return (rval);
119
}
120
121
/*
122
 * Change the specified ttys' flags.
123
 */
124
int
125
ttys(char **ttylist, int print)
126
{
127
	struct ttyent *tep;
128
	int rval;
129
130
	rval = 0;
131
	for (; *ttylist != NULL; ttylist++) {
132
		tep = getttynam(*ttylist);
133
		if (tep == NULL) {
134
			warnx("couldn't find an entry in %s for \"%s\"",
135
			    _PATH_TTYS, *ttylist);
136
			rval = 1;
137
			continue;
138
		}
139
140
		if (ttyflags(tep, print))
141
			rval = 1;
142
	}
143
	return (rval);
144
}
145
146
147
/*
148
 * Actually do the work; find out what the new flags value should be,
149
 * open the device, and change the flags.
150
 */
151
int
152
ttyflags(struct ttyent *tep, int print)
153
{
154
	int fd, flags = 0, rval = 0, st, sep = 0;
155
	char path[PATH_MAX];
156
	char strflags[256];
157
158
	st = tep->ty_status;
159
	strflags[0] = '\0';
160
161
	/* Find the full device path name. */
162
	(void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
163
164
	if (print == 0) {
165
		/* Convert ttyent.h flags into ioctl flags. */
166
		if (st & TTY_LOCAL) {
167
			flags |= TIOCFLAG_CLOCAL;
168
			(void)strlcat(strflags, "local", sizeof strflags);
169
			sep++;
170
		}
171
		if (st & TTY_RTSCTS) {
172
			flags |= TIOCFLAG_CRTSCTS;
173
			if (sep++)
174
				(void)strlcat(strflags, "|", sizeof strflags);
175
			(void)strlcat(strflags, "rtscts", sizeof strflags);
176
		}
177
		if (st & TTY_SOFTCAR) {
178
			flags |= TIOCFLAG_SOFTCAR;
179
			if (sep++)
180
				(void)strlcat(strflags, "|", sizeof strflags);
181
			(void)strlcat(strflags, "softcar", sizeof strflags);
182
		}
183
		if (st & TTY_MDMBUF) {
184
			flags |= TIOCFLAG_MDMBUF;
185
			if (sep++)
186
				(void)strlcat(strflags, "|", sizeof strflags);
187
			(void)strlcat(strflags, "mdmbuf", sizeof strflags);
188
		}
189
		if (vflag)
190
			printf("%s setting flags to: %s\n", path, strflags);
191
	}
192
193
	if (nflag)
194
		return (0);
195
196
	/* Open the device NON-BLOCKING, set the flags, and close it. */
197
	if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
198
		if (!(errno == ENXIO ||
199
		      (errno == ENOENT && (st & TTY_ON) == 0)))
200
			rval = 1;
201
		if (vflag)
202
			warn("open %s", path);
203
		return (rval);
204
	}
205
	if (print == 0) {
206
		if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
207
			if (errno != ENOTTY || vflag) {
208
				warn("TIOCSFLAGS on %s", path);
209
				rval = (errno != ENOTTY);
210
			}
211
	} else {
212
		if (ioctl(fd, TIOCGFLAGS, &flags) == -1)
213
			if (errno != ENOTTY || vflag) {
214
				warn("TIOCGFLAGS on %s", path);
215
				rval = (errno != ENOTTY);
216
			}
217
		if (flags & TIOCFLAG_CLOCAL) {
218
			(void)strlcat(strflags, "local", sizeof strflags);
219
			sep++;
220
		}
221
		if (flags & TIOCFLAG_CRTSCTS) {
222
			if (sep++)
223
				(void)strlcat(strflags, "|", sizeof strflags);
224
			(void)strlcat(strflags, "rtscts", sizeof strflags);
225
		}
226
		if (flags & TIOCFLAG_SOFTCAR) {
227
			if (sep++)
228
				(void)strlcat(strflags, "|", sizeof strflags);
229
			(void)strlcat(strflags, "softcar", sizeof strflags);
230
		}
231
		if (flags & TIOCFLAG_MDMBUF) {
232
			if (sep++)
233
				(void)strlcat(strflags, "|", sizeof strflags);
234
			(void)strlcat(strflags, "mdmbuf", sizeof strflags);
235
		}
236
		printf("%s flags are: %s\n", path, strflags);
237
	}
238
	if (close(fd) == -1) {
239
		warn("close %s", path);
240
		return (1);
241
	}
242
	return (rval);
243
}
244
245
/*
246
 * Print usage information when a bogus set of arguments is given.
247
 */
248
void
249
usage(void)
250
{
251
	(void)fprintf(stderr, "usage: ttyflags [-pv] [-a | tty ...]\n");
252
	exit(1);
253
}