Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sbin/comcontrol/comcontrol.c
39476 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 1992 Christopher G. Demetriou
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
* 3. The name of the author may not be used to endorse or promote
16
* products derived from this software without specific written permission.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
* SUCH DAMAGE.
29
*/
30
31
#include <sys/cdefs.h>
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 usage(void) __dead2;
44
45
static void
46
usage(void)
47
{
48
fprintf(stderr,
49
"usage: comcontrol <filename> [drainwait <n>]\n");
50
exit(1);
51
}
52
53
int
54
main(int argc, char *argv[])
55
{
56
int fd;
57
int res = 0;
58
int print_drainwait = 1;
59
int drainwait = -1;
60
61
if (argc < 2)
62
usage();
63
64
if (strcmp(argv[1], "-") == 0)
65
fd = STDIN_FILENO;
66
else {
67
fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
68
if (fd < 0) {
69
warn("couldn't open file %s", argv[1]);
70
return 1;
71
}
72
}
73
if (argc == 2) {
74
if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
75
print_drainwait = 0;
76
if (errno != ENOTTY) {
77
res = 1;
78
warn("TIOCGDRAINWAIT");
79
}
80
}
81
if (print_drainwait)
82
printf("drainwait %d ", drainwait);
83
printf("\n");
84
} else {
85
while (argv[2] != NULL) {
86
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 (drainwait >= 0) {
97
if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
98
res = 1;
99
warn("TIOCSDRAINWAIT");
100
}
101
}
102
}
103
104
close(fd);
105
return res;
106
}
107
108