parent
38183534a3
commit
735dbaa2e1
@ -1,2 +0,0 @@
|
||||
This is an apple.
|
||||
Apples are good for our health.
|
@ -0,0 +1,8 @@
|
||||
/bin/app_mkdir /RAMDISK0/sub_dir
|
||||
/bin/app_touch /RAMDISK0/sub_dir/ramfile1
|
||||
/bin/app_touch /RAMDISK0/sub_dir/ramfile2
|
||||
/bin/app_echo /RAMDISK0/sub_dir/ramfile1
|
||||
/bin/app_cat /RAMDISK0/sub_dir/ramfile1
|
||||
/bin/app_ls /RAMDISK0/sub_dir
|
||||
/bin/app_ls /RAMDISK0
|
||||
END END
|
@ -0,0 +1,23 @@
|
||||
#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 *filename = argv[0];
|
||||
|
||||
printu("\n======== cat command ========\n");
|
||||
printu("cat: %s\n", filename);
|
||||
|
||||
fd = open(filename, O_RDWR);
|
||||
printu("file descriptor fd: %d\n", fd);
|
||||
|
||||
read_u(fd, buf, MAXBUF);
|
||||
printu("read content: \n%s\n", buf);
|
||||
close(fd);
|
||||
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int fd;
|
||||
char str[] = "hello world";
|
||||
char *filename = argv[0];
|
||||
printu("\n======== echo command ========\n");
|
||||
printu("echo: %s\n", filename);
|
||||
|
||||
fd = open(filename, 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);
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
#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[]) {
|
||||
int MAXBUF = 512;
|
||||
char str[] = "hello world";
|
||||
char buf[MAXBUF];
|
||||
int fd1, fd2;
|
||||
|
||||
printu("\n======== establish the file ========\n");
|
||||
|
||||
fd1 = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT);
|
||||
printu("create file: /RAMDISK0/ramfile\n");
|
||||
close(fd1);
|
||||
|
||||
printu("\n======== Test 1: hard link ========\n");
|
||||
|
||||
link_u("/RAMDISK0/ramfile", "/RAMDISK0/ramfile2");
|
||||
printu("create hard link: /RAMDISK0/ramfile2 -> /RAMDISK0/ramfile\n");
|
||||
|
||||
fd1 = open("/RAMDISK0/ramfile", O_RDWR);
|
||||
fd2 = open("/RAMDISK0/ramfile2", O_RDWR);
|
||||
|
||||
printu("file descriptor fd1 (ramfile): %d\n", fd1);
|
||||
printu("file descriptor fd2 (ramfile2): %d\n", fd2);
|
||||
|
||||
// check the number of hard links to ramfile on disk
|
||||
struct istat st;
|
||||
disk_stat_u(fd1, &st);
|
||||
printu("ramfile hard links: %d\n", st.st_nlinks);
|
||||
if (st.st_nlinks != 2) {
|
||||
printu("ERROR: the number of hard links to ramfile should be 2, but it is %d\n",
|
||||
st.st_nlinks);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
write_u(fd1, str, strlen(str));
|
||||
printu("/RAMDISK0/ramfile write content: \n%s\n", str);
|
||||
|
||||
read_u(fd2, buf, MAXBUF);
|
||||
printu("/RAMDISK0/ramfile2 read content: \n%s\n", buf);
|
||||
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
|
||||
printu("\n======== Test 2: unlink ========\n");
|
||||
|
||||
ls("/RAMDISK0");
|
||||
|
||||
unlink_u("/RAMDISK0/ramfile");
|
||||
printu("unlink: /RAMDISK0/ramfile\n");
|
||||
|
||||
ls("/RAMDISK0");
|
||||
|
||||
// check the number of hard links to ramfile2 on disk
|
||||
fd2 = open("/RAMDISK0/ramfile2", O_RDWR);
|
||||
disk_stat_u(fd2, &st);
|
||||
printu("ramfile2 hard links: %d\n", st.st_nlinks);
|
||||
if (st.st_nlinks != 1) {
|
||||
printu("ERROR: the number of hard links to ramfile should be 1, but it is %d\n",
|
||||
st.st_nlinks);
|
||||
exit(-1);
|
||||
}
|
||||
close(fd2);
|
||||
|
||||
printu("\nAll tests passed!\n\n");
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *path = argv[0];
|
||||
int dir_fd = opendir_u(path);
|
||||
printu("---------- ls command -----------\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);
|
||||
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *new_dir = argv[0];
|
||||
printu("\n======== mkdir command ========\n");
|
||||
|
||||
mkdir_u(new_dir);
|
||||
printu("mkdir: %s\n", new_dir);
|
||||
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* This app starts a very simple shell and executes some simple commands.
|
||||
* The commands are stored in the hostfs_root/shellrc
|
||||
* The shell loads the file and executes the command line by line.
|
||||
*/
|
||||
#include "user_lib.h"
|
||||
#include "string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
printu("\n======== Shell Start ========\n\n");
|
||||
int fd;
|
||||
int MAXBUF = 1024;
|
||||
char buf[MAXBUF];
|
||||
char *token;
|
||||
char delim[3] = " \n";
|
||||
fd = open("/shellrc", O_RDONLY);
|
||||
|
||||
read_u(fd, buf, MAXBUF);
|
||||
close(fd);
|
||||
char *command = naive_malloc();
|
||||
char *para = naive_malloc();
|
||||
int start = 0;
|
||||
while (1)
|
||||
{
|
||||
if(!start) {
|
||||
token = strtok(buf, delim);
|
||||
start = 1;
|
||||
}
|
||||
else
|
||||
token = strtok(NULL, delim);
|
||||
strcpy(command, token);
|
||||
token = strtok(NULL, delim);
|
||||
strcpy(para, token);
|
||||
if(strcmp(command, "END") == 0 && strcmp(para, "END") == 0)
|
||||
break;
|
||||
printu("Next command: %s %s\n\n", command, para);
|
||||
printu("==========Command Start============\n\n");
|
||||
int pid = fork();
|
||||
if(pid == 0) {
|
||||
int ret = exec(command, para);
|
||||
if (ret == -1)
|
||||
printu("exec failed!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
wait(pid);
|
||||
printu("==========Command End============\n\n");
|
||||
}
|
||||
}
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int fd;
|
||||
char *filename = argv[0];
|
||||
printu("\n======== touch command ========\n");
|
||||
printu("touch: %s\n", filename);
|
||||
|
||||
fd = open(filename, O_CREAT);
|
||||
printu("file descriptor fd: %d\n", fd);
|
||||
|
||||
close(fd);
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue