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