/*-1* Copyright (c) 1989, 1991, 1993, 19942* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/types.h>3031#include <ctype.h>32#include <err.h>33#include <fcntl.h>34#include <limits.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>3940#include "stty.h"41#include "extern.h"4243int44main(int argc, char *argv[])45{46struct info i;47enum FMT fmt;48int ch;49const char *file, *errstr = NULL;5051fmt = NOTSET;52i.fd = STDIN_FILENO;53file = "stdin";5455opterr = 0;56while (optind < argc &&57strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&58(ch = getopt(argc, argv, "aef:g")) != -1)59switch(ch) {60case 'a': /* undocumented: POSIX compatibility */61fmt = POSIX;62break;63case 'e':64fmt = BSD;65break;66case 'f':67if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)68err(1, "%s", optarg);69file = optarg;70break;71case 'g':72fmt = GFLAG;73break;74case '?':75default:76goto args;77}7879args: argc -= optind;80argv += optind;8182if (tcgetattr(i.fd, &i.t) < 0)83errx(1, "%s isn't a terminal", file);84if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)85err(1, "TIOCGETD");86if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)87warn("TIOCGWINSZ");8889checkredirect(); /* conversion aid */9091switch(fmt) {92case NOTSET:93if (*argv)94break;95/* FALLTHROUGH */96case BSD:97case POSIX:98print(&i.t, &i.win, i.ldisc, fmt);99break;100case GFLAG:101gprint(&i.t, &i.win, i.ldisc);102break;103}104105for (i.set = i.wset = 0; *argv; ++argv) {106if (ksearch(&argv, &i))107continue;108109if (csearch(&argv, &i))110continue;111112if (msearch(&argv, &i))113continue;114115if (isdigit(**argv)) {116speed_t speed;117118speed = strtonum(*argv, 0, UINT_MAX, &errstr);119if (errstr)120err(1, "speed");121cfsetospeed(&i.t, speed);122cfsetispeed(&i.t, speed);123i.set = 1;124continue;125}126127if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {128gread(&i.t, *argv + sizeof("gfmt1") - 1);129i.set = 1;130continue;131}132133warnx("illegal option -- %s", *argv);134usage();135}136137if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)138err(1, "tcsetattr");139if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)140warn("TIOCSWINSZ");141exit(0);142}143144void145usage(void)146{147148(void)fprintf(stderr,149"usage: stty [-a | -e | -g] [-f file] [arguments]\n");150exit (1);151}152153154