feat: implement dup(2), dup2(2)
[lunaix-os.git] / lunaix-os / kernel / demos / iotest.c
1 #include <lunaix/fctrl.h>
2 #include <lunaix/foptions.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/proc.h>
5 #include <lunaix/syslog.h>
6
7 LOG_MODULE("IOTEST")
8
9 void
10 _iotest_main()
11 {
12     char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
13                            "There were two regal sisters who ruled together "
14                            "and created harmony for all the land.";
15
16     int fd = open("/dev/sda", 0);  // bd0 设备 - 硬盘
17     int tty = open("/dev/tty", 0); // tty 设备 - 控制台
18
19     if (fd < 0 || tty < 0) {
20         kprintf(KERROR "fail to open (%d)\n", geterrno());
21         return;
22     }
23
24     // 移动指针至第二个逻辑扇区(LBA=1),并写入
25     lseek(fd, 1, FSEEK_SET);
26     write(fd, test_sequence, sizeof(test_sequence));
27
28     // 读出我们写的内容
29     char read_out[256];
30     lseek(fd, -1, FSEEK_CUR);
31     read(fd, read_out, sizeof(read_out));
32
33     write(tty, read_out, sizeof(read_out));
34
35     close(fd);
36     close(tty);
37
38     kprint_hex(read_out, sizeof(read_out));
39 }