1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 34 #include <ctype.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <limits.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 #include "stty.h" 44 #include "extern.h" 45 46 int 47 main(int argc, char *argv[]) 48 { 49 struct info i; 50 enum FMT fmt; 51 int ch; 52 const char *file, *errstr = NULL; 53 54 fmt = NOTSET; 55 i.fd = STDIN_FILENO; 56 file = "stdin"; 57 58 opterr = 0; 59 while (optind < argc && 60 strspn(argv[optind], "-aefg") == strlen(argv[optind]) && 61 (ch = getopt(argc, argv, "aef:g")) != -1) 62 switch(ch) { 63 case 'a': /* undocumented: POSIX compatibility */ 64 fmt = POSIX; 65 break; 66 case 'e': 67 fmt = BSD; 68 break; 69 case 'f': 70 if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0) 71 err(1, "%s", optarg); 72 file = optarg; 73 break; 74 case 'g': 75 fmt = GFLAG; 76 break; 77 case '?': 78 default: 79 goto args; 80 } 81 82 args: argc -= optind; 83 argv += optind; 84 85 if (tcgetattr(i.fd, &i.t) < 0) 86 errx(1, "%s isn't a terminal", file); 87 if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0) 88 err(1, "TIOCGETD"); 89 if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0) 90 warn("TIOCGWINSZ"); 91 92 checkredirect(); /* conversion aid */ 93 94 switch(fmt) { 95 case NOTSET: 96 if (*argv) 97 break; 98 /* FALLTHROUGH */ 99 case BSD: 100 case POSIX: 101 print(&i.t, &i.win, i.ldisc, fmt); 102 break; 103 case GFLAG: 104 gprint(&i.t, &i.win, i.ldisc); 105 break; 106 } 107 108 for (i.set = i.wset = 0; *argv; ++argv) { 109 if (ksearch(&argv, &i)) 110 continue; 111 112 if (csearch(&argv, &i)) 113 continue; 114 115 if (msearch(&argv, &i)) 116 continue; 117 118 if (isdigit(**argv)) { 119 speed_t speed; 120 121 speed = strtonum(*argv, 0, UINT_MAX, &errstr); 122 if (errstr) 123 err(1, "speed"); 124 cfsetospeed(&i.t, speed); 125 cfsetispeed(&i.t, speed); 126 i.set = 1; 127 continue; 128 } 129 130 if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) { 131 gread(&i.t, *argv + sizeof("gfmt1") - 1); 132 i.set = 1; 133 continue; 134 } 135 136 warnx("illegal option -- %s", *argv); 137 usage(); 138 } 139 140 if (i.set && tcsetattr(i.fd, 0, &i.t) < 0) 141 err(1, "tcsetattr"); 142 if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0) 143 warn("TIOCSWINSZ"); 144 exit(0); 145 } 146 147 void 148 usage(void) 149 { 150 151 (void)fprintf(stderr, 152 "usage: stty [-a | -e | -g] [-f file] [arguments]\n"); 153 exit (1); 154 } 155