GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/stty/stty.c Lines: 20 61 32.8 %
Date: 2017-11-07 Branches: 16 56 28.6 %

Line Branch Exec Source
1
/*	$OpenBSD: stty.c,v 1.20 2016/07/23 08:57:18 bluhm Exp $	*/
2
/*	$NetBSD: stty.c,v 1.11 1995/03/21 09:11:30 cgd Exp $	*/
3
4
/*-
5
 * Copyright (c) 1989, 1991, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/types.h>
34
#include <sys/ioctl.h>
35
36
#include <ctype.h>
37
#include <err.h>
38
#include <errno.h>
39
#include <fcntl.h>
40
#include <limits.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <termios.h>
45
#include <unistd.h>
46
47
#include "stty.h"
48
#include "extern.h"
49
50
int
51
main(int argc, char *argv[])
52
{
53
81
	struct info i;
54
	enum FMT fmt;
55
	int ch;
56
57
	fmt = NOTSET;
58
81
	i.fd = STDIN_FILENO;
59
60
81
	opterr = 0;
61

162
	while (optind < argc &&
62
81
	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
63
	    (ch = getopt(argc, argv, "aef:g")) != -1)
64
		switch(ch) {
65
		case 'a':
66
			fmt = POSIX;
67
			break;
68
		case 'e':
69
			fmt = BSD;
70
			break;
71
		case 'f':
72
			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
73
				err(1, "%s", optarg);
74
			break;
75
		case 'g':
76
			fmt = GFLAG;
77
			break;
78
		default:
79
			goto args;
80
		}
81
82
81
args:	argc -= optind;
83
81
	argv += optind;
84
85
81
	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0	)
86
		err(1, "TIOCGETD");
87
88
81
	if (tcgetattr(i.fd, &i.t) < 0)
89
		errx(1, "not a terminal");
90
81
	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
91
		warn("TIOCGWINSZ");
92
93

162
	switch(fmt) {
94
	case NOTSET:
95
81
		if (*argv)
96
			break;
97
		/* FALLTHROUGH */
98
	case BSD:
99
	case POSIX:
100
		if (*argv)
101
			errx(1, "either display or modify");
102
		if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
103
			err(1, "pledge");
104
		print(&i.t, &i.win, i.ldisc, fmt);
105
		break;
106
	case GFLAG:
107
		if (*argv)
108
			errx(1, "either display or modify");
109
		if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
110
			err(1, "pledge");
111
		gprint(&i.t, &i.win, i.ldisc);
112
		break;
113
	}
114
115
	/*
116
	 * Cannot pledge, because of "extproc", "ostart" and "ostop"
117
	 */
118
119
324
	for (i.set = i.wset = 0; *argv; ++argv) {
120
81
		if (ksearch(&argv, &i))
121
			continue;
122
123
81
		if (csearch(&argv, &i))
124
			continue;
125
126
81
		if (msearch(&argv, &i))
127
			continue;
128
129
		if (isdigit((unsigned char)**argv)) {
130
			const char *error;
131
			int speed;
132
133
			speed = strtonum(*argv, 0, INT_MAX, &error);
134
			if (error)
135
				err(1, "%s", *argv);
136
			cfsetospeed(&i.t, speed);
137
			cfsetispeed(&i.t, speed);
138
			i.set = 1;
139
			continue;
140
		}
141
142
		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
143
			gread(&i.t, *argv + sizeof("gfmt1") - 1);
144
			i.set = 1;
145
			continue;
146
		}
147
148
		warnx("illegal option -- %s", *argv);
149
		usage();
150
	}
151
152

162
	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
153
		err(1, "tcsetattr");
154

81
	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
155
		warn("TIOCSWINSZ");
156
81
	return (0);
157
81
}
158
159
void
160
usage(void)
161
{
162
	fprintf(stderr, "usage: %s [-a | -e | -g] [-f file] [operands]\n",
163
	    __progname);
164
	exit (1);
165
}