xref: /original-bsd/bin/stty/stty.c (revision b4971bb3)
1 /*-
2  * Copyright (c) 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1989, 1991, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)stty.c	8.1 (Berkeley) 06/03/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <ctype.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include "stty.h"
30 #include "extern.h"
31 
32 int
33 main(argc, argv)
34 	int argc;
35 	char *argv[];
36 {
37 	struct info i;
38 	enum FMT fmt;
39 	int ch;
40 
41 	fmt = NOTSET;
42 	i.fd = STDIN_FILENO;
43 
44 	opterr = 0;
45 	while (optind < argc &&
46 	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
47 	    (ch = getopt(argc, argv, "aef:g")) != EOF)
48 		switch(ch) {
49 		case 'a':		/* undocumented: POSIX compatibility */
50 			fmt = POSIX;
51 			break;
52 		case 'e':
53 			fmt = BSD;
54 			break;
55 		case 'f':
56 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
57 				err(1, "%s", optarg);
58 			break;
59 		case 'g':
60 			fmt = GFLAG;
61 			break;
62 		case '?':
63 		default:
64 			goto args;
65 		}
66 
67 args:	argc -= optind;
68 	argv += optind;
69 
70 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
71 		err(1, "TIOCGETD");
72 	if (tcgetattr(i.fd, &i.t) < 0)
73 		err(1, "tcgetattr");
74 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
75 		warn("TIOCGWINSZ: %s\n", strerror(errno));
76 
77 	checkredirect();			/* conversion aid */
78 
79 	switch(fmt) {
80 	case NOTSET:
81 		if (*argv)
82 			break;
83 		/* FALLTHROUGH */
84 	case BSD:
85 	case POSIX:
86 		print(&i.t, &i.win, i.ldisc, fmt);
87 		break;
88 	case GFLAG:
89 		gprint(&i.t, &i.win, i.ldisc);
90 		break;
91 	}
92 
93 	for (i.set = i.wset = 0; *argv; ++argv) {
94 		if (ksearch(&argv, &i))
95 			continue;
96 
97 		if (csearch(&argv, &i))
98 			continue;
99 
100 		if (msearch(&argv, &i))
101 			continue;
102 
103 		if (isdigit(**argv)) {
104 			int speed;
105 
106 			speed = atoi(*argv);
107 			cfsetospeed(&i.t, speed);
108 			cfsetispeed(&i.t, speed);
109 			i.set = 1;
110 			continue;
111 		}
112 
113 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
114 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
115 			continue;
116 		}
117 
118 		warnx("illegal option -- %s", *argv);
119 		usage();
120 	}
121 
122 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
123 		err(1, "tcsetattr");
124 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
125 		warn("TIOCSWINSZ");
126 	exit(0);
127 }
128 
129 void
130 usage()
131 {
132 	(void)fprintf(stderr,
133 	    "usage: stty: [-a|-e|-g] [-f file] [options]\n");
134 	exit (1);
135 }
136