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