-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuserapp.c
47 lines (38 loc) · 924 Bytes
/
userapp.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
/* user application which reads and writes to a device */
/*
Usage:
% make
% sudo ./userapp
*/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#define DEVICE "/dev/testCharDevice"
int main(int argc, char *argv[])
{
int i, fd;
char ch, write_buf[100], read_buf[100];
fd = open(DEVICE, O_RDWR); /* open for read/write */
if(fd == -1){
printf("file %s either does not exit, or locked by another process, or you are not root!\n", DEVICE);
exit(-1);
}
printf("r - read from device,\nw- write from device\nEnter command: ");
scanf("%c", &ch);
switch (ch){
case 'w':
printf ("enter data: ");
scanf("%s", write_buf);
write(fd, write_buf, sizeof(write_buf));
break;
case 'r':
read(fd, read_buf, sizeof(read_buf));
printf ("device: %s\n", read_buf);
break;
default:
printf ("command not recognized\n");
break;
}
close(fd);
return 0;
}