GC !__DragonFly__ section.
[dragonfly.git] / sbin / comcontrol / comcontrol.c
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  * $FreeBSD: src/sbin/comcontrol/comcontrol.c,v 1.8.2.2 2001/08/01 06:01:51 obrien Exp $
29  * $DragonFly: src/sbin/comcontrol/comcontrol.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
30  */
31
32 #include <ctype.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.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     print_dtrwait = 1, print_drainwait = 1;
57         int     dtrwait = -1, drainwait = -1;
58
59         if (argc < 2)
60                 usage();
61
62         if (strcmp(argv[1], "-") == 0)
63                 fd = STDIN_FILENO;
64         else {
65                 fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
66                 if (fd < 0) {
67                         warn("couldn't open file %s", argv[1]);
68                         return 1;
69                 }
70         }
71         if (argc == 2) {
72                 if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
73                         print_dtrwait = 0;
74                         if (errno != ENOTTY) {
75                                 res = 1;
76                                 warn("TIOCMGDTRWAIT");
77                         }
78                 }
79                 if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
80                         print_drainwait = 0;
81                         if (errno != ENOTTY) {
82                                 res = 1;
83                                 warn("TIOCGDRAINWAIT");
84                         }
85                 }
86                 if (print_dtrwait)
87                         printf("dtrwait %d ", dtrwait);
88                 if (print_drainwait)
89                         printf("drainwait %d ", drainwait);
90                 printf("\n");
91         } else {
92                 while (argv[2] != NULL) {
93                         if (!strcmp(argv[2],"dtrwait")) {
94                                 if (dtrwait >= 0)
95                                         usage();
96                                 if (argv[3] == NULL || !isdigit(argv[3][0]))
97                                         usage();
98                                 dtrwait = atoi(argv[3]);
99                                 argv += 2;
100                         } else if (!strcmp(argv[2],"drainwait")) {
101                                 if (drainwait >= 0)
102                                         usage();
103                                 if (argv[3] == NULL || !isdigit(argv[3][0]))
104                                         usage();
105                                 drainwait = atoi(argv[3]);
106                                 argv += 2;
107                         } else
108                                 usage();
109                 }
110                 if (dtrwait >= 0) {
111                         if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
112                                 res = 1;
113                                 warn("TIOCMSDTRWAIT");
114                         }
115                 }
116                 if (drainwait >= 0) {
117                         if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
118                                 res = 1;
119                                 warn("TIOCSDRAINWAIT");
120                         }
121                 }
122         }
123
124         close(fd);
125         return res;
126 }