-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_config.c
94 lines (82 loc) · 1.75 KB
/
echo_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <sys/types.h>
#include <sys/ioctl.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ECHO_CLEAR_BUFFER _IO('E', 1)
#define ECHO_SET_BUFFER_SIZE _IOW('E', 2, int)
static enum {UNSET, CLEAR, SETSIZE} action = UNSET;
/*
* The usage statement: echo_config -c | -s size
*/
static void
usage()
{
/*
* Arguments for this program are "either-or." That is,
* 'echo_config -c' and 'echo_config -s size' are valid; however,
* 'echo_config -c -s size' is invalid.
*/
fprintf(stderr, "usage: echo_config -c | -s size\n");
exit(1);
}
/*
* This program clears or resizes the memory buffer
* found in /dev/echo.
*/
int
main(int argc, char *argv[])
{
int ch, fd, i, size;
char *p;
/*
* Parse the command-line argument list to determine
* the correct course of action.
*
* -c: clear the memory buffer.
* -s size: resize the memory buffer to size.
*/
while ((ch = getopt(argc, argv, "cs:")) != -1)
switch(ch) {
case 'c':
if (action != UNSET)
usage();
action = CLEAR;
break;
case 's':
if (action != UNSET)
usage();
action = SETSIZE;
size = (int)strtol(optarg, &p, 10);
if (*p)
errx(1, "illegal size -- %s", optarg);
break;
default:
usage();
}
/*
* Perform the chose action
*/
if (action == CLEAR) {
fd = open("/dev/echo", O_RDWR);
if (fd < 0)
err(1, "open(/dev/echo)");
i = ioctl(fd, ECHO_CLEAR_BUFFER, NULL);
if (i < 0)
err(1, "ioctl(/dev/echo)");
close(fd);
} else if (action == SETSIZE) {
fd = open("/dev/echo", O_RDWR);
if (fd < 0)
err(1, "open(/dev/echo)");
i = ioctl(fd, ECHO_SET_BUFFER_SIZE, &size);
if (i < 0)
err(1, "ioctl(/dev/echo)");
close(fd);
} else
usage();
return 0;
}