-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyread.c
56 lines (47 loc) · 949 Bytes
/
myread.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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc, char** argv){
//usage
if(argc != 2){
fprintf(stderr, "usage: %s <length>\n", argv[0]);
return -1;
}
//init
errno = 0;
long int len = strtol(argv[1], NULL, 10);
if(errno != 0){
perror("ERROR: strtol");
return -1;
}
char* buf = malloc((size_t)len+1);
if(buf == NULL){
fprintf(stderr, "ERROR: malloc failed\n");
return -1;
}
//open the driver
int f = open("/dev/opsysmem", O_RDONLY);
if(f < 0){
fprintf(stderr, "ERROR: cannot open /dev/opsysmem\n");
return -1;
}
//read and return error codes
errno = 0;
int n = read(f, buf, len);
if(n < 0){
perror("ERROR: read");
close(f);
return n;
}
buf[n] = '\0';
//print
printf("%s", buf);
//clean up
close(f);
return 0;
}