xref: /freebsd/sbin/comcontrol/comcontrol.c (revision a79b71281cd63ad7a6cc43a6d5673a2510b51630)
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 <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 
44 static void
45 usage()
46 {
47 	fprintf(stderr,
48 	"usage: comcontrol <filename>|- [dtrwait <n>] [drainwait <n>]\n");
49 	exit(1);
50 }
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	int	fd;
56 	int     res = 0;
57 	int     print_dtrwait = 1, print_drainwait = 1;
58 	int     dtrwait = -1, drainwait = -1;
59 
60 	if (argc < 2)
61 		usage();
62 
63 	if (strcmp(argv[1], "-") == 0)
64 		fd = STDIN_FILENO;
65 	else {
66 		fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
67 		if (fd < 0) {
68 			warn("couldn't open file %s", argv[1]);
69 			return 1;
70 		}
71 	}
72 	if (argc == 2) {
73 		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
74 			print_dtrwait = 0;
75 			if (errno != ENOTTY) {
76 				res = 1;
77 				warn("TIOCMGDTRWAIT");
78 			}
79 		}
80 		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
81 			print_drainwait = 0;
82 			if (errno != ENOTTY) {
83 				res = 1;
84 				warn("TIOCGDRAINWAIT");
85 			}
86 		}
87 		if (print_dtrwait)
88 			printf("dtrwait %d ", dtrwait);
89 		if (print_drainwait)
90 			printf("drainwait %d ", drainwait);
91 		printf("\n");
92 	} else {
93 		while (argv[2] != NULL) {
94 			if (!strcmp(argv[2],"dtrwait")) {
95 				if (dtrwait >= 0)
96 					usage();
97 				if (argv[3] == NULL || !isdigit(argv[3][0]))
98 					usage();
99 				dtrwait = atoi(argv[3]);
100 				argv += 2;
101 			} else if (!strcmp(argv[2],"drainwait")) {
102 				if (drainwait >= 0)
103 					usage();
104 				if (argv[3] == NULL || !isdigit(argv[3][0]))
105 					usage();
106 				drainwait = atoi(argv[3]);
107 				argv += 2;
108 			} else
109 				usage();
110 		}
111 		if (dtrwait >= 0) {
112 			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
113 				res = 1;
114 				warn("TIOCMSDTRWAIT");
115 			}
116 		}
117 		if (drainwait >= 0) {
118 			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
119 				res = 1;
120 				warn("TIOCSDRAINWAIT");
121 			}
122 		}
123 	}
124 
125 	close(fd);
126 	return res;
127 }
128