xref: /freebsd-14.2/sbin/comcontrol/comcontrol.c (revision 9b50d902)
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> [[-]bidir] [dtrwait <n>]\n", progname);
42 	exit(1);
43 }
44 
45 int main(int argc, char *argv[])
46 {
47 	int	fd;
48 	int	res;
49 	int     dtrwait;
50 
51 	if ((argc < 2) || (argc > 5)) usage(argv[0]);
52 
53 	fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
54 	if (fd < 0) {
55 		fprintf(stderr, "%s: couldn't open file %s\n", argv[0], argv[1]);
56 		perror("open");
57 		exit(1);
58 	}
59 
60 	if (argc == 2) {
61 		if (ioctl(fd, TIOCMGBIDIR, &res) >= 0) {
62 			if (!res)  printf("-");
63 			printf("bidir ");
64 		}
65 		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
66 			perror("TIOCMGDTRWAIT");
67 			exit(1);
68 		}
69 		printf("dtrwait %d\n", dtrwait);
70 	} else {
71 		char *prg = argv[0];
72 
73 		res = dtrwait = -1;
74 		while (argv[2] != NULL) {
75 			if (!strcmp(argv[2],"bidir")) {
76 				if (res >= 0)
77 					usage(prg);
78 				res = 1;
79 				argv++;
80 			} else if (!strcmp(argv[2],"-bidir")) {
81 				if (res >= 0)
82 					usage(prg);
83 				res = 0;
84 				argv++;
85 			} else if (!strcmp(argv[2],"dtrwait")) {
86 				if (dtrwait >= 0)
87 					usage(prg);
88 				if (argv[3] == NULL || !isdigit(argv[3][0]))
89 					usage(prg);
90 				dtrwait = atoi(argv[3]);
91 				argv += 2;
92 			} else {
93 				usage(prg);
94 			}
95 		}
96 		if (res >= 0) {
97 			if (ioctl(fd, TIOCMSBIDIR, &res) < 0) {
98 				perror("TIOCMSBIDIR");
99 				exit(1);
100 			}
101 		}
102 		if (dtrwait >= 0) {
103 			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
104 				perror("TIOCMSDTRWAIT");
105 				exit(1);
106 			}
107 		}
108 	}
109 
110 	close(fd);
111 	exit(0);
112 }
113