/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1992 Christopher G. Demetriou4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. The name of the author may not be used to endorse or promote15* products derived from this software without specific written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31#include <ctype.h>32#include <err.h>33#include <errno.h>34#include <fcntl.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>39#include <sys/types.h>40#include <sys/ioctl.h>4142static void usage(void) __dead2;4344static void45usage(void)46{47fprintf(stderr,48"usage: comcontrol <filename> [drainwait <n>]\n");49exit(1);50}5152int53main(int argc, char *argv[])54{55int fd;56int res = 0;57int print_drainwait = 1;58int drainwait = -1;5960if (argc < 2)61usage();6263if (strcmp(argv[1], "-") == 0)64fd = STDIN_FILENO;65else {66fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);67if (fd < 0) {68warn("couldn't open file %s", argv[1]);69return 1;70}71}72if (argc == 2) {73if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {74print_drainwait = 0;75if (errno != ENOTTY) {76res = 1;77warn("TIOCGDRAINWAIT");78}79}80if (print_drainwait)81printf("drainwait %d ", drainwait);82printf("\n");83} else {84while (argv[2] != NULL) {85if (!strcmp(argv[2],"drainwait")) {86if (drainwait >= 0)87usage();88if (argv[3] == NULL || !isdigit(argv[3][0]))89usage();90drainwait = atoi(argv[3]);91argv += 2;92} else93usage();94}95if (drainwait >= 0) {96if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {97res = 1;98warn("TIOCSDRAINWAIT");99}100}101}102103close(fd);104return res;105}106107108