xref: /original-bsd/bin/stty/stty.c (revision d54be081)
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.28 (Berkeley) 06/05/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: [-eg] [-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 (strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
44 	    (ch = getopt(argc, argv, "aef:g")) != EOF)
45 		switch(ch) {
46 		case 'a':		/* undocumented: POSIX compatibility */
47 			fmt = POSIX;
48 			break;
49 		case 'e':
50 			fmt = BSD;
51 			break;
52 		case 'f':
53 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
54 				err("%s: %s", optarg, strerror(errno));
55 			break;
56 		case 'g':
57 			fmt = GFLAG;
58 			break;
59 		case '?':
60 		default:
61 			goto args;
62 		}
63 
64 args:	argc -= optind;
65 	argv += optind;
66 
67 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
68 		err("TIOCGETD: %s", strerror(errno));
69 	if (tcgetattr(i.fd, &i.t) < 0)
70 		err("tcgetattr: %s", strerror(errno));
71 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
72 		warn("TIOCGWINSZ: %s\n", strerror(errno));
73 
74 	checkredirect();			/* conversion aid */
75 
76 	switch(fmt) {
77 	case NOTSET:
78 		if (*argv)
79 			break;
80 		/* FALLTHROUGH */
81 	case BSD:
82 	case POSIX:
83 		print(&i.t, &i.win, i.ldisc, fmt);
84 		break;
85 	case GFLAG:
86 		gprint(&i.t, &i.win, i.ldisc);
87 		break;
88 	}
89 
90 	for (i.set = i.wset = 0; *argv; ++argv) {
91 		if (ksearch(&argv, &i))
92 			continue;
93 
94 		if (csearch(&argv, &i))
95 			continue;
96 
97 		if (msearch(&argv, &i))
98 			continue;
99 
100 		if (isdigit(**argv)) {
101 			int speed;
102 
103 			speed = atoi(*argv);
104 			cfsetospeed(&i.t, speed);
105 			cfsetispeed(&i.t, speed);
106 			continue;
107 		}
108 
109 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
110 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
111 			continue;
112 		}
113 
114 		err("illegal option -- %s\n%s", *argv, usage);
115 	}
116 
117 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
118 		err("tcsetattr: %s", strerror(errno));
119 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
120 		warn("TIOCSWINSZ: %s", strerror(errno));
121 	exit(0);
122 }
123