14Srgrimes/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
44Srgrimes * Copyright (c) 1992 Christopher G. Demetriou
54Srgrimes * All rights reserved.
64Srgrimes *
74Srgrimes * Redistribution and use in source and binary forms, with or without
84Srgrimes * modification, are permitted provided that the following conditions
94Srgrimes * are met:
104Srgrimes * 1. Redistributions of source code must retain the above copyright
114Srgrimes *    notice, this list of conditions and the following disclaimer.
124Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
134Srgrimes *    notice, this list of conditions and the following disclaimer in the
144Srgrimes *    documentation and/or other materials provided with the distribution.
154Srgrimes * 3. The name of the author may not be used to endorse or promote
164Srgrimes *    products derived from this software without specific written permission.
174Srgrimes *
184Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
194Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
224Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
234Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
244Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
254Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
264Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
274Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
284Srgrimes * SUCH DAMAGE.
294Srgrimes */
304Srgrimes
31114589Sobrien#include <sys/cdefs.h>
32114589Sobrien__FBSDID("$FreeBSD: stable/11/sbin/comcontrol/comcontrol.c 330449 2018-03-05 07:26:05Z eadler $");
334Srgrimes
3437273Scharnier#include <ctype.h>
3537273Scharnier#include <err.h>
3659838Sache#include <errno.h>
3737273Scharnier#include <fcntl.h>
3837273Scharnier#include <stdio.h>
3937273Scharnier#include <stdlib.h>
4078732Sdd#include <string.h>
4137273Scharnier#include <unistd.h>
424Srgrimes#include <sys/types.h>
434Srgrimes#include <sys/ioctl.h>
444Srgrimes
45118671Sjohanstatic void usage(void);
46118671Sjohan
4737273Scharnierstatic void
48201227Sedusage(void)
494Srgrimes{
5037273Scharnier	fprintf(stderr,
5162470Ssheldonh	"usage: comcontrol <filename> [dtrwait <n>] [drainwait <n>]\n");
524Srgrimes	exit(1);
534Srgrimes}
544Srgrimes
5537273Scharnierint
5637273Scharniermain(int argc, char *argv[])
574Srgrimes{
584Srgrimes	int	fd;
595397Sache	int     res = 0;
6059838Sache	int     print_dtrwait = 1, print_drainwait = 1;
615397Sache	int     dtrwait = -1, drainwait = -1;
624Srgrimes
635459Sbde	if (argc < 2)
6437273Scharnier		usage();
654Srgrimes
6659791Sache	if (strcmp(argv[1], "-") == 0)
6759791Sache		fd = STDIN_FILENO;
6859791Sache	else {
6959791Sache		fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
7059791Sache		if (fd < 0) {
7159791Sache			warn("couldn't open file %s", argv[1]);
7259791Sache			return 1;
7359791Sache		}
744Srgrimes	}
754Srgrimes	if (argc == 2) {
76837Sache		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
7759838Sache			print_dtrwait = 0;
7859838Sache			if (errno != ENOTTY) {
7959838Sache				res = 1;
8059838Sache				warn("TIOCMGDTRWAIT");
8159838Sache			}
82837Sache		}
835397Sache		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
8459838Sache			print_drainwait = 0;
8559838Sache			if (errno != ENOTTY) {
8659838Sache				res = 1;
8759838Sache				warn("TIOCGDRAINWAIT");
8859838Sache			}
895397Sache		}
9059838Sache		if (print_dtrwait)
9159838Sache			printf("dtrwait %d ", dtrwait);
9259838Sache		if (print_drainwait)
9359838Sache			printf("drainwait %d ", drainwait);
9459838Sache		printf("\n");
954Srgrimes	} else {
96837Sache		while (argv[2] != NULL) {
971629Sache			if (!strcmp(argv[2],"dtrwait")) {
98837Sache				if (dtrwait >= 0)
9937273Scharnier					usage();
10059838Sache				if (argv[3] == NULL || !isdigit(argv[3][0]))
10159838Sache					usage();
102837Sache				dtrwait = atoi(argv[3]);
103837Sache				argv += 2;
1045397Sache			} else if (!strcmp(argv[2],"drainwait")) {
1055397Sache				if (drainwait >= 0)
10637273Scharnier					usage();
10759838Sache				if (argv[3] == NULL || !isdigit(argv[3][0]))
10859838Sache					usage();
1095397Sache				drainwait = atoi(argv[3]);
1105397Sache				argv += 2;
1115397Sache			} else
11237273Scharnier				usage();
1134Srgrimes		}
114837Sache		if (dtrwait >= 0) {
115837Sache			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
1165397Sache				res = 1;
11737273Scharnier				warn("TIOCMSDTRWAIT");
118837Sache			}
119837Sache		}
1205397Sache		if (drainwait >= 0) {
1215397Sache			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
1225397Sache				res = 1;
12337273Scharnier				warn("TIOCSDRAINWAIT");
1245397Sache			}
1255397Sache		}
1264Srgrimes	}
1274Srgrimes
1284Srgrimes	close(fd);
1295397Sache	return res;
1304Srgrimes}
131