-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioctl.c
71 lines (59 loc) · 1.26 KB
/
ioctl.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
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define DM510_MAJOR 255
#define IOCTL_GET_MAX_READERS _IOR(DM510_MAJOR, 0, ssize_t)
#define IOCTL_SET_MAX_READERS _IOW(DM510_MAJOR, 1, size_t)
#define IOCTL_GET_BUFFER_SIZE _IOR(DM510_MAJOR, 2, ssize_t)
#define IOCTL_SET_BUFFER_SIZE _IOW(DM510_MAJOR, 3, size_t)
#define IOCTL_MAX_NR 3
unsigned long ioctl_cmds[] = {
IOCTL_GET_MAX_READERS,
IOCTL_SET_MAX_READERS,
IOCTL_GET_BUFFER_SIZE,
IOCTL_SET_BUFFER_SIZE,
};
char *cmd_names[] = {
"get_max_readers",
"set_max_readers",
"get_buffer_size",
"set_buffer_size",
};
int main(int argc, char *argv[])
{
if (argc < 3) {
return 1;
}
int fd;
long result;
char *cmd_name;
unsigned long cmd, arg;
fd = open(argv[1], O_RDWR);
if (fd < 0) {
perror("open");
return 1;
}
cmd_name = argv[2];
if (argc > 3)
arg = strtol(argv[3], NULL, 10);
for (int i = 0; i <= IOCTL_MAX_NR; i++) {
if (strcmp(cmd_name, cmd_names[i]) == 0) {
cmd = ioctl_cmds[i];
break;
}
}
result = argc > 3 ? ioctl(fd, cmd, arg) : ioctl(fd, cmd);
close(fd);
if (result < 0) {
perror("ioctl");
return result;
}
if (argc == 3)
printf("%ld\n", result);
return 0;
}