xref: /dflybsd-src/sbin/comcontrol/comcontrol.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 1992 Christopher G. Demetriou
3*86d7f5d3SJohn Marino  * All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino  * 3. The name of the author may not be used to endorse or promote
14*86d7f5d3SJohn Marino  *    products derived from this software without specific written permission.
15*86d7f5d3SJohn Marino  *
16*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*86d7f5d3SJohn Marino  * SUCH DAMAGE.
27*86d7f5d3SJohn Marino  *
28*86d7f5d3SJohn Marino  * $FreeBSD: src/sbin/comcontrol/comcontrol.c,v 1.8.2.2 2001/08/01 06:01:51 obrien Exp $
29*86d7f5d3SJohn Marino  * $DragonFly: src/sbin/comcontrol/comcontrol.c,v 1.3 2005/03/20 14:07:43 liamfoy Exp $
30*86d7f5d3SJohn Marino  */
31*86d7f5d3SJohn Marino 
32*86d7f5d3SJohn Marino #include <sys/types.h>
33*86d7f5d3SJohn Marino #include <sys/ioctl.h>
34*86d7f5d3SJohn Marino 
35*86d7f5d3SJohn Marino #include <ctype.h>
36*86d7f5d3SJohn Marino #include <err.h>
37*86d7f5d3SJohn Marino #include <errno.h>
38*86d7f5d3SJohn Marino #include <fcntl.h>
39*86d7f5d3SJohn Marino #include <stdio.h>
40*86d7f5d3SJohn Marino #include <stdlib.h>
41*86d7f5d3SJohn Marino #include <string.h>
42*86d7f5d3SJohn Marino #include <unistd.h>
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino static void
usage(void)45*86d7f5d3SJohn Marino usage(void)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino 	fprintf(stderr,
48*86d7f5d3SJohn Marino 	"usage: comcontrol <filename> [dtrwait <n>] [drainwait <n>]\n");
49*86d7f5d3SJohn Marino 	exit(1);
50*86d7f5d3SJohn Marino }
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino int
main(int argc,char * argv[])53*86d7f5d3SJohn Marino main(int argc, char *argv[])
54*86d7f5d3SJohn Marino {
55*86d7f5d3SJohn Marino 	int fd;
56*86d7f5d3SJohn Marino 	int res = 0;
57*86d7f5d3SJohn Marino 	int print_dtrwait = 1, print_drainwait = 1;
58*86d7f5d3SJohn Marino 	int dtrwait = -1, drainwait = -1;
59*86d7f5d3SJohn Marino 
60*86d7f5d3SJohn Marino 	if (argc < 2)
61*86d7f5d3SJohn Marino 		usage();
62*86d7f5d3SJohn Marino 
63*86d7f5d3SJohn Marino 	if (strcmp(argv[1], "-") == 0) {
64*86d7f5d3SJohn Marino 		fd = STDIN_FILENO;
65*86d7f5d3SJohn Marino 	} else {
66*86d7f5d3SJohn Marino 		fd = open(argv[1], O_RDONLY | O_NONBLOCK, 0);
67*86d7f5d3SJohn Marino 		if (fd < 0) {
68*86d7f5d3SJohn Marino 			warn("couldn't open file %s", argv[1]);
69*86d7f5d3SJohn Marino 			return 1;
70*86d7f5d3SJohn Marino 		}
71*86d7f5d3SJohn Marino 	}
72*86d7f5d3SJohn Marino 	if (argc == 2) {
73*86d7f5d3SJohn Marino 		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
74*86d7f5d3SJohn Marino 			print_dtrwait = 0;
75*86d7f5d3SJohn Marino 			if (errno != ENOTTY) {
76*86d7f5d3SJohn Marino 				res = 1;
77*86d7f5d3SJohn Marino 				warn("TIOCMGDTRWAIT");
78*86d7f5d3SJohn Marino 			}
79*86d7f5d3SJohn Marino 		}
80*86d7f5d3SJohn Marino 		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
81*86d7f5d3SJohn Marino 			print_drainwait = 0;
82*86d7f5d3SJohn Marino 			if (errno != ENOTTY) {
83*86d7f5d3SJohn Marino 				res = 1;
84*86d7f5d3SJohn Marino 				warn("TIOCGDRAINWAIT");
85*86d7f5d3SJohn Marino 			}
86*86d7f5d3SJohn Marino 		}
87*86d7f5d3SJohn Marino 		if (print_dtrwait)
88*86d7f5d3SJohn Marino 			printf("dtrwait %d ", dtrwait);
89*86d7f5d3SJohn Marino 		if (print_drainwait)
90*86d7f5d3SJohn Marino 			printf("drainwait %d ", drainwait);
91*86d7f5d3SJohn Marino 		printf("\n");
92*86d7f5d3SJohn Marino 	} else {
93*86d7f5d3SJohn Marino 		while (argv[2] != NULL) {
94*86d7f5d3SJohn Marino 			if (strcmp(argv[2],"dtrwait") == 0) {
95*86d7f5d3SJohn Marino 				if (dtrwait >= 0)
96*86d7f5d3SJohn Marino 					usage();
97*86d7f5d3SJohn Marino 				if (argv[3] == NULL || !isdigit(argv[3][0]))
98*86d7f5d3SJohn Marino 					usage();
99*86d7f5d3SJohn Marino 				dtrwait = atoi(argv[3]);
100*86d7f5d3SJohn Marino 				argv += 2;
101*86d7f5d3SJohn Marino 			} else if (strcmp(argv[2],"drainwait") == 0) {
102*86d7f5d3SJohn Marino 				if (drainwait >= 0)
103*86d7f5d3SJohn Marino 					usage();
104*86d7f5d3SJohn Marino 				if (argv[3] == NULL || !isdigit(argv[3][0]))
105*86d7f5d3SJohn Marino 					usage();
106*86d7f5d3SJohn Marino 				drainwait = atoi(argv[3]);
107*86d7f5d3SJohn Marino 				argv += 2;
108*86d7f5d3SJohn Marino 			} else {
109*86d7f5d3SJohn Marino 				usage();
110*86d7f5d3SJohn Marino 			}
111*86d7f5d3SJohn Marino 		}
112*86d7f5d3SJohn Marino 		if (dtrwait >= 0) {
113*86d7f5d3SJohn Marino 			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
114*86d7f5d3SJohn Marino 				res = 1;
115*86d7f5d3SJohn Marino 				warn("TIOCMSDTRWAIT");
116*86d7f5d3SJohn Marino 			}
117*86d7f5d3SJohn Marino 		}
118*86d7f5d3SJohn Marino 		if (drainwait >= 0) {
119*86d7f5d3SJohn Marino 			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
120*86d7f5d3SJohn Marino 				res = 1;
121*86d7f5d3SJohn Marino 				warn("TIOCSDRAINWAIT");
122*86d7f5d3SJohn Marino 			}
123*86d7f5d3SJohn Marino 		}
124*86d7f5d3SJohn Marino 	}
125*86d7f5d3SJohn Marino 
126*86d7f5d3SJohn Marino 	close(fd);
127*86d7f5d3SJohn Marino 	return res;
128*86d7f5d3SJohn Marino }
129