📄 shell.c~
字号:
/* shell.c */
#include "shell.h"
/* setup root dir, allocate inode and block, add file entry.
return root's inode number. */
int fs_setup(FILE *fp)
{
char *self=".";
struct fd_entry root_entry;//根i-node 表项
strcpy( root_entry.name, self);//根i-node的名
root_entry.inode_no = get_ind(fp);//根i-node的号
char *user = "user";
struct fs_inode root_inode;
strcpy( root_inode.user, user);
root_inode.type = 'd';//目录
root_inode.links = 1;//1个连接
root_inode.size = 2*sizeof(root_entry);
root_inode.addr[0] = get_blk(fp);//获得磁盘块号
update_ind( fp, root_entry.inode_no, root_inode);//根结点写入结点表
fseek( fp, SEEK_BLK(root_inode.addr[0]), SEEK_SET);//找到磁盘块在文件系统中的地址
fwrite( &root_entry, sizeof(root_entry), 1, fp);//写入inode表项
char *parent = "..";
strcpy( root_entry.name, parent);
fseek( fp, SEEK_BLK(root_inode.addr[0])+sizeof(root_entry), SEEK_SET);
fwrite( &root_entry, sizeof(root_entry), 1, fp);
return root_entry.inode_no;
}
/* this function provide an small shell,
it need 2 env variable (*fp) and (*pd_ind_no) */
void shell( FILE *fp, int *pd_ind_no)//模拟文件系统的文件指针和根结点号
{
printf("##########################################\n"
"# Welcome to the Small LINUX Filesystem #\n"
"##########################################\n"
"Login as tuu, in " __DATE__ " " __TIME__ "\n\n");
help();
int i;
char command[MAX_COMMAND_LEN], ch, path[STACK_MAX];
while(1){
pwd( fp, pd_ind_no, path);//转到当前路径pd_ind_no和path会改变
fprintf( stdout, "tuu@hfut:%s$ ", path);
fflush(stdout);
for( i=0; (ch=fgetc(stdin)) != '\n'; i++){
command[i] = ch;
};
command[i] = '\0';
processor( command, fp, pd_ind_no, path);
};
return;
}
int main(int argc, char *argv[])
{
int dir_ind_no, *pd_ind_no;
char *file = "FS_DISK";
FILE *fp;
if(argc > 1)
file = argv[1];
if((fp=fopen( file, "r+b")) == NULL){//以读和写,二进制方式打开
fprintf( stderr, "Creating file %s, and ", file);
fp = fopen( file, "w+b");//使文件为0长,为读和写,二进制方式 打开
fclose(fp);
fp = fopen( file, "r+b");
fs_format(fp); //创建盘并格式化文件系统
dir_ind_no = fs_setup(fp);//创建文件系统,得到根i-node的号
}
else{
dir_ind_no = STACK_MAX - 1; /* get the root inode number */
}
printf("use file %s as a hard disk.\n", file);
// test_part(fp);
pd_ind_no = &dir_ind_no;
shell( fp, pd_ind_no);//把模拟文件系统的文件指针和根结点号传给SHELL程序
fclose(fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -