1 /*	$OpenBSD: stty.c,v 1.11 2003/06/11 23:42:12 deraadt 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 #ifndef lint
34 static char copyright[] =
35 "@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
42 #else
43 static char rcsid[] = "$OpenBSD: stty.c,v 1.11 2003/06/11 23:42:12 deraadt Exp $";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/types.h>
48 
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "stty.h"
59 #include "extern.h"
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	struct info i;
65 	enum FMT fmt;
66 	int ch;
67 
68 	fmt = NOTSET;
69 	i.fd = STDIN_FILENO;
70 
71 	opterr = 0;
72 	while (optind < argc &&
73 	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
74 	    (ch = getopt(argc, argv, "aef:g")) != -1)
75 		switch(ch) {
76 		case 'a':		/* undocumented: POSIX compatibility */
77 			fmt = POSIX;
78 			break;
79 		case 'e':
80 			fmt = BSD;
81 			break;
82 		case 'f':
83 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
84 				err(1, "%s", optarg);
85 			break;
86 		case 'g':
87 			fmt = GFLAG;
88 			break;
89 		default:
90 			goto args;
91 		}
92 
93 args:	argc -= optind;
94 	argv += optind;
95 
96 	if (tcgetattr(i.fd, &i.t) < 0)
97 		errx(1, "not a terminal");
98 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0	)
99 		err(1, "TIOCGETD");
100 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
101 		warn("TIOCGWINSZ");
102 
103 	switch(fmt) {
104 	case NOTSET:
105 		if (*argv)
106 			break;
107 		/* FALLTHROUGH */
108 	case BSD:
109 	case POSIX:
110 		print(&i.t, &i.win, i.ldisc, fmt);
111 		break;
112 	case GFLAG:
113 		gprint(&i.t, &i.win, i.ldisc);
114 		break;
115 	}
116 
117 	for (i.set = i.wset = 0; *argv; ++argv) {
118 		if (ksearch(&argv, &i))
119 			continue;
120 
121 		if (csearch(&argv, &i))
122 			continue;
123 
124 		if (msearch(&argv, &i))
125 			continue;
126 
127 		if (isdigit(**argv)) {
128 			int speed;
129 
130 			speed = atoi(*argv);
131 			cfsetospeed(&i.t, speed);
132 			cfsetispeed(&i.t, speed);
133 			i.set = 1;
134 			continue;
135 		}
136 
137 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
138 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
139 			i.set = 1;
140 			continue;
141 		}
142 
143 		warnx("illegal option -- %s", *argv);
144 		usage();
145 	}
146 
147 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
148 		err(1, "tcsetattr");
149 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
150 		warn("TIOCSWINSZ");
151 	return (0);
152 }
153 
154 void
usage(void)155 usage(void)
156 {
157 	fprintf(stderr, "usage: %s [-a|-e|-g] [-f file] [operands]\n",
158 	    __progname);
159 	exit (1);
160 }
161