xref: /freebsd-src/sbin/comcontrol/comcontrol.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
15b81b6b3SRodney W. Grimes /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
45b81b6b3SRodney W. Grimes  * Copyright (c) 1992 Christopher G. Demetriou
55b81b6b3SRodney W. Grimes  * All rights reserved.
65b81b6b3SRodney W. Grimes  *
75b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
85b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
95b81b6b3SRodney W. Grimes  * are met:
105b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
115b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
125b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
135b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
145b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
155b81b6b3SRodney W. Grimes  * 3. The name of the author may not be used to endorse or promote
165b81b6b3SRodney W. Grimes  *    products derived from this software without specific written permission.
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
195b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
215b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
225b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
235b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
245b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
255b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
265b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
275b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
285b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
295b81b6b3SRodney W. Grimes  */
305b81b6b3SRodney W. Grimes 
31c69284caSDavid E. O'Brien #include <sys/cdefs.h>
32d286dcb0SPhilippe Charnier #include <ctype.h>
33d286dcb0SPhilippe Charnier #include <err.h>
3409dd94d8SAndrey A. Chernov #include <errno.h>
35d286dcb0SPhilippe Charnier #include <fcntl.h>
365b81b6b3SRodney W. Grimes #include <stdio.h>
37cc4ca263SAndrey A. Chernov #include <stdlib.h>
38cafefe8cSDima Dorfman #include <string.h>
39d286dcb0SPhilippe Charnier #include <unistd.h>
40d286dcb0SPhilippe Charnier #include <sys/types.h>
41d286dcb0SPhilippe Charnier #include <sys/ioctl.h>
425b81b6b3SRodney W. Grimes 
43*65f3be91SAlfonso Gregory static void usage(void) __dead2;
442f92fd9bSJohan Karlsson 
45d286dcb0SPhilippe Charnier static void
usage(void)4610bc3a7fSEd Schouten usage(void)
475b81b6b3SRodney W. Grimes {
48d286dcb0SPhilippe Charnier 	fprintf(stderr,
49c886a5d4SSheldon Hearn 	"usage: comcontrol <filename> [dtrwait <n>] [drainwait <n>]\n");
505b81b6b3SRodney W. Grimes 	exit(1);
515b81b6b3SRodney W. Grimes }
525b81b6b3SRodney W. Grimes 
53d286dcb0SPhilippe Charnier int
main(int argc,char * argv[])54d286dcb0SPhilippe Charnier main(int argc, char *argv[])
555b81b6b3SRodney W. Grimes {
565b81b6b3SRodney W. Grimes 	int	fd;
57b0e399e3SAndrey A. Chernov 	int     res = 0;
5809dd94d8SAndrey A. Chernov 	int     print_dtrwait = 1, print_drainwait = 1;
59b0e399e3SAndrey A. Chernov 	int     dtrwait = -1, drainwait = -1;
605b81b6b3SRodney W. Grimes 
6112981249SBruce Evans 	if (argc < 2)
62d286dcb0SPhilippe Charnier 		usage();
635b81b6b3SRodney W. Grimes 
6485a23112SAndrey A. Chernov 	if (strcmp(argv[1], "-") == 0)
6585a23112SAndrey A. Chernov 		fd = STDIN_FILENO;
6685a23112SAndrey A. Chernov 	else {
675b81b6b3SRodney W. Grimes 		fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
685b81b6b3SRodney W. Grimes 		if (fd < 0) {
69d286dcb0SPhilippe Charnier 			warn("couldn't open file %s", argv[1]);
70b0e399e3SAndrey A. Chernov 			return 1;
715b81b6b3SRodney W. Grimes 		}
7285a23112SAndrey A. Chernov 	}
735b81b6b3SRodney W. Grimes 	if (argc == 2) {
74cc4ca263SAndrey A. Chernov 		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
7509dd94d8SAndrey A. Chernov 			print_dtrwait = 0;
7609dd94d8SAndrey A. Chernov 			if (errno != ENOTTY) {
77b0e399e3SAndrey A. Chernov 				res = 1;
78d286dcb0SPhilippe Charnier 				warn("TIOCMGDTRWAIT");
795b81b6b3SRodney W. Grimes 			}
8009dd94d8SAndrey A. Chernov 		}
81b0e399e3SAndrey A. Chernov 		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
8209dd94d8SAndrey A. Chernov 			print_drainwait = 0;
8309dd94d8SAndrey A. Chernov 			if (errno != ENOTTY) {
84b0e399e3SAndrey A. Chernov 				res = 1;
85d286dcb0SPhilippe Charnier 				warn("TIOCGDRAINWAIT");
86b0e399e3SAndrey A. Chernov 			}
8709dd94d8SAndrey A. Chernov 		}
8809dd94d8SAndrey A. Chernov 		if (print_dtrwait)
8909dd94d8SAndrey A. Chernov 			printf("dtrwait %d ", dtrwait);
9009dd94d8SAndrey A. Chernov 		if (print_drainwait)
9109dd94d8SAndrey A. Chernov 			printf("drainwait %d ", drainwait);
9209dd94d8SAndrey A. Chernov 		printf("\n");
93cc4ca263SAndrey A. Chernov 	} else {
94cc4ca263SAndrey A. Chernov 		while (argv[2] != NULL) {
95123e9a59SAndrey A. Chernov 			if (!strcmp(argv[2],"dtrwait")) {
96cc4ca263SAndrey A. Chernov 				if (dtrwait >= 0)
97d286dcb0SPhilippe Charnier 					usage();
9809dd94d8SAndrey A. Chernov 				if (argv[3] == NULL || !isdigit(argv[3][0]))
9909dd94d8SAndrey A. Chernov 					usage();
100cc4ca263SAndrey A. Chernov 				dtrwait = atoi(argv[3]);
101cc4ca263SAndrey A. Chernov 				argv += 2;
102b0e399e3SAndrey A. Chernov 			} else if (!strcmp(argv[2],"drainwait")) {
103b0e399e3SAndrey A. Chernov 				if (drainwait >= 0)
104d286dcb0SPhilippe Charnier 					usage();
10509dd94d8SAndrey A. Chernov 				if (argv[3] == NULL || !isdigit(argv[3][0]))
10609dd94d8SAndrey A. Chernov 					usage();
107b0e399e3SAndrey A. Chernov 				drainwait = atoi(argv[3]);
108b0e399e3SAndrey A. Chernov 				argv += 2;
109b0e399e3SAndrey A. Chernov 			} else
110d286dcb0SPhilippe Charnier 				usage();
111cc4ca263SAndrey A. Chernov 		}
112cc4ca263SAndrey A. Chernov 		if (dtrwait >= 0) {
113cc4ca263SAndrey A. Chernov 			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
114b0e399e3SAndrey A. Chernov 				res = 1;
115d286dcb0SPhilippe Charnier 				warn("TIOCMSDTRWAIT");
116b0e399e3SAndrey A. Chernov 			}
117b0e399e3SAndrey A. Chernov 		}
118b0e399e3SAndrey A. Chernov 		if (drainwait >= 0) {
119b0e399e3SAndrey A. Chernov 			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
120b0e399e3SAndrey A. Chernov 				res = 1;
121d286dcb0SPhilippe Charnier 				warn("TIOCSDRAINWAIT");
122cc4ca263SAndrey A. Chernov 			}
123cc4ca263SAndrey A. Chernov 		}
1245b81b6b3SRodney W. Grimes 	}
1255b81b6b3SRodney W. Grimes 
1265b81b6b3SRodney W. Grimes 	close(fd);
127b0e399e3SAndrey A. Chernov 	return res;
1285b81b6b3SRodney W. Grimes }
129