-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
41 lines (34 loc) · 899 Bytes
/
app.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
char buf[128];
int main()
{
int fd, m, n;
fd = open("/dev/hello", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open file \"/dev/hello\" failed\n");
exit(-1);
}
printf("read: ");
while ((m = read(fd, buf, 1)) > 0 && buf[0] != '\0') {
printf("%c", buf[0]);
}
printf("\n");
lseek64(fd, 0, 0);
n = write(fd, "hello world from user1!", 23);
printf("write length: %d\n", n);
n = write(fd, " ", 1);
printf("write length: %d\n", n);
n = write(fd, "hello world from user2!", 23);
printf("write length: %d\n", n);
lseek64(fd, 0, 0);
printf("read: ");
while ((m = read(fd, buf, 1)) > 0 && buf[0] != '\0') {
printf("%c", buf[0]);
}
printf("\n");
close(fd);
return 0;
}