-
Notifications
You must be signed in to change notification settings - Fork 8
/
simple_char_test.c
69 lines (59 loc) · 1.25 KB
/
simple_char_test.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define OPEN 'o'
#define CLOSE 'c'
#define READ 'r'
#define WRITE 'w'
#define EXIT 'e'
#define ENTER '\0'
#define BUFFER_SIZE 1024
openDevice(){
open("/dev/simple_char_device", O_RDWR | O_APPEND);
getInput();
}
readDevice(){
printf("\nData read from the device:\n");
system("cat /dev/simple_char_device");
getInput();
}
writeDevice(){
printf("\nEnter data you want to write to the device:\n");
char buffer[BUFFER_SIZE];
char data[BUFFER_SIZE];
if (fgets(data, BUFFER_SIZE, stdin) != NULL){
char * strippedData;
if ((strippedData=strchr(data, '\n')) != NULL){
*strippedData = '\0';
}
char cmd[BUFFER_SIZE + 40]= {0x0};
sprintf(cmd,"echo \"%s\" >> /dev/simple_char_device", data);
system(cmd);
}
getInput();
}
getInput(){
char inputBuffer[BUFFER_SIZE];
printf("\nPlease enter a command (o,r,w,e): ");
if (fgets(inputBuffer, BUFFER_SIZE, stdin) != NULL){
if (inputBuffer[0] == OPEN){
openDevice();
}
else if (inputBuffer[0] == READ){
readDevice();
}
else if (inputBuffer[0] == WRITE){
writeDevice();
}
else if (inputBuffer[0] == EXIT){
}
else{
printf("You have given incorrect input");
getInput();
}
}
}
main(){
getInput();
}