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