parent
bbcac5cf62
commit
3e29933551
@ -0,0 +1,60 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
void ls(char *path) {
|
||||
int dir_fd = opendir_u(path);
|
||||
printu("------------------------------\n");
|
||||
printu("ls \"%s\":\n", path);
|
||||
printu("[name] [inode_num]\n");
|
||||
struct dir dir;
|
||||
int width = 20;
|
||||
while(readdir_u(dir_fd, &dir) == 0) {
|
||||
// we do not have %ms :(
|
||||
char name[width + 1];
|
||||
memset(name, ' ', width + 1);
|
||||
name[width] = '\0';
|
||||
if (strlen(dir.name) < width) {
|
||||
strcpy(name, dir.name);
|
||||
name[strlen(dir.name)] = ' ';
|
||||
printu("%s %d\n", name, dir.inum);
|
||||
}
|
||||
else
|
||||
printu("%s %d\n", dir.name, dir.inum);
|
||||
}
|
||||
printu("------------------------------\n");
|
||||
closedir_u(dir_fd);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char str[] = "hello world";
|
||||
int fd;
|
||||
|
||||
printu("\n======== Test 1: open and read dir ========\n");
|
||||
|
||||
ls("/RAMDISK0");
|
||||
|
||||
printu("\n======== Test 2: make dir ========\n");
|
||||
|
||||
mkdir_u("/RAMDISK0/sub_dir");
|
||||
printu("make: /RAMDISK0/sub_dir\n");
|
||||
|
||||
ls("/RAMDISK0");
|
||||
|
||||
// try to write a file in the new dir
|
||||
printu("write: /RAMDISK0/sub_dir/ramfile\n");
|
||||
|
||||
fd = open("/RAMDISK0/sub_dir/ramfile", O_RDWR | O_CREAT);
|
||||
printu("file descriptor fd: %d\n", fd);
|
||||
|
||||
write_u(fd, str, strlen(str));
|
||||
printu("write content: \n%s\n", str);
|
||||
|
||||
close(fd);
|
||||
|
||||
ls("/RAMDISK0/sub_dir");
|
||||
|
||||
printu("\nAll tests passed!\n\n");
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int fd;
|
||||
int MAXBUF = 512;
|
||||
char buf[MAXBUF];
|
||||
char str[] = "hello world";
|
||||
int fd1, fd2;
|
||||
|
||||
printu("\n======== Test 1: read host file ========\n");
|
||||
printu("read: /hostfile.txt\n");
|
||||
|
||||
fd = open("/hostfile.txt", O_RDONLY);
|
||||
printu("file descriptor fd: %d\n", fd);
|
||||
|
||||
read_u(fd, buf, MAXBUF);
|
||||
printu("read content: \n%s\n", buf);
|
||||
|
||||
close(fd);
|
||||
|
||||
printu("\n======== Test 2: create/write rfs file ========\n");
|
||||
printu("write: /RAMDISK0/ramfile\n");
|
||||
|
||||
fd = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT);
|
||||
printu("file descriptor fd: %d\n", fd);
|
||||
|
||||
write_u(fd, buf, strlen(buf));
|
||||
printu("write content: \n%s\n", buf);
|
||||
close(fd);
|
||||
|
||||
printu("\n======== Test 3: read rfs file ========\n");
|
||||
printu("read: /RAMDISK0/ramfile\n");
|
||||
|
||||
fd = open("/RAMDISK0/ramfile", O_RDWR);
|
||||
printu("file descriptor fd: %d\n", fd);
|
||||
|
||||
read_u(fd, buf, MAXBUF);
|
||||
printu("read content: \n%s\n", buf);
|
||||
close(fd);
|
||||
|
||||
printu("\n======== Test 4: open twice ========\n");
|
||||
|
||||
fd1 = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT);
|
||||
fd2 = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT);
|
||||
|
||||
printu("file descriptor fd1(ramfile): %d\n", fd1);
|
||||
printu("file descriptor fd2(ramfile): %d\n", fd2);
|
||||
|
||||
write_u(fd1, str, strlen(str));
|
||||
printu("write content: \n%s\n", str);
|
||||
|
||||
read_u(fd2, buf, MAXBUF);
|
||||
printu("read content: \n%s\n", buf);
|
||||
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
|
||||
printu("\nAll tests passed!\n\n");
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue