xref: /freebsd/sbin/comcontrol/comcontrol.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
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 /* comcontrol.c */
30 
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <fcntl.h>
37 
38 
39 void usage(char *progname)
40 {
41 	fprintf(stderr, "usage: %s <filename> [dtrwait <n>] [drainwait <n>]\n", progname);
42 	exit(1);
43 }
44 
45 int main(int argc, char *argv[])
46 {
47 	int	fd;
48 	int     res = 0;
49 	int     dtrwait = -1, drainwait = -1;
50 
51 	if (argc < 2)
52 		usage(argv[0]);
53 
54 	fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
55 	if (fd < 0) {
56 		perror("open");
57 		fprintf(stderr, "%s: couldn't open file %s\n", argv[0], argv[1]);
58 		return 1;
59 	}
60 
61 	if (argc == 2) {
62 		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
63 			res = 1;
64 			perror("TIOCMGDTRWAIT");
65 		}
66 		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
67 			res = 1;
68 			perror("TIOCGDRAINWAIT");
69 		}
70 		printf("dtrwait %d drainwait %d\n", dtrwait, drainwait);
71 	} else {
72 		char *prg = argv[0];
73 
74 		while (argv[2] != NULL) {
75 			if (!strcmp(argv[2],"dtrwait")) {
76 				if (dtrwait >= 0)
77 					usage(prg);
78 				if (argv[3] == NULL || !isdigit(argv[3][0]))
79 					usage(prg);
80 				dtrwait = atoi(argv[3]);
81 				argv += 2;
82 			} else if (!strcmp(argv[2],"drainwait")) {
83 				if (drainwait >= 0)
84 					usage(prg);
85 				if (argv[3] == NULL || !isdigit(argv[3][0]))
86 					usage(prg);
87 				drainwait = atoi(argv[3]);
88 				argv += 2;
89 			} else
90 				usage(prg);
91 		}
92 		if (dtrwait >= 0) {
93 			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
94 				res = 1;
95 				perror("TIOCMSDTRWAIT");
96 			}
97 		}
98 		if (drainwait >= 0) {
99 			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
100 				res = 1;
101 				perror("TIOCSDRAINWAIT");
102 			}
103 		}
104 	}
105 
106 	close(fd);
107 	return res;
108 }
109