filesystem.c
资源名称:os2.tar.gz [点击查看]
上传用户:kin20054
上传日期:2022-06-28
资源大小:4k
文件大小:3k
源码类别:
操作系统开发
开发平台:
Unix_Linux
- #include "file.h"
- #define Max 10
- int clear_disk(const char* disk)
- {
- read_disk(disk);
- int i;
- for(i = 0; i < L; i++)
- {
- memset(ldisk[i],0,B);
- }
- int temp;
- for(temp = 0; temp < 4; temp++)
- {
- ldisk[0][temp] = 0xff;
- }
- for(temp = 4; temp < B; temp++)
- {
- ldisk[0][temp] = 0x00;
- }
- struct inode root_inode;
- root_inode.sa[0] = 0;
- root_inode.sa[1] = find_block();
- fill_full(root_inode.sa[1]);
- root_inode.sa[2] = -1;
- root_inode.sa[3] =-1;
- memcpy(ldisk[1],(char*)&root_inode,16);
- save_disk(disk);
- }
- void help()
- {
- printf("这是一个虚拟的文件系统n可以执行以下的命令n");
- printf("1 help 输出帮助文档n");
- printf("2 clear 清理磁盘,使得磁盘初始化n");
- printf("3 create filename 创建文件名为filename的文件n");
- printf("4 open filename 打开文件n");
- printf(" 4.1 write mem输入mem到打开文件!!!不能输入空格到文件中n");
- printf(" 4.2 read num从文件中读入num个字节到buf并且输出n");
- printf(" 4.3 lseek pos更改读写指针到posn");
- printf(" 4.4 close 关闭打开的文件n");
- printf("5 ls or dir 列出目录下文件n");
- printf("6 delete or rm filename 删除文件n");
- printf("7 quit 退出文件系统并保存n");
- }
- void test(int i1,int i2,int j1,int j2)
- {
- int i,j;
- for(i=i1;i<i2;i++)
- for(j=j1;j<j2;j++)
- print_bit(ldisk[i][j]);
- }
- void main(int ag,char** av)
- {
- const char* disk;
- disk = av[1];
- help();
- int index;
- read_disk(disk);
- int i;
- for(i = 0; i < Max; i++)
- table[i].add = -1;
- int k,ll;
- char mem[384];
- char buf[384];
- for(i = 0; i < 384; i++)
- {
- mem[i] = 'a';
- buf[i] = 0;
- }
- char command[8];
- while(1)
- {
- printf("wsc>>");
- scanf("%s",command);
- if(strcmp(command,"help") == 0)
- {
- help();
- continue;
- }
- if(strcmp(command,"clear") == 0)
- {
- clear_disk(disk);
- continue;
- }
- if(strcmp(command,"create") == 0)
- {
- char name[4];
- scanf("%s",name);
- create(name);
- continue;
- }
- if(strcmp(command,"delete") == 0||strcmp(command,"rm") == 0)
- {
- char name[4];
- scanf("%s",name);
- destroy(name);
- continue;
- }
- if(strcmp(command,"open") == 0)
- {
- char name[4];
- scanf("%s",name);
- k = open(name);
- scanf("%s",command);
- while(strcmp(command,"close") != 0)
- {
- if(strcmp(command,"write") == 0)
- {
- printf("n");
- //gets(mem);
- scanf("%s",mem);
- ll=write(k,mem,strlen(mem));
- if(ll != 0)
- {
- printf(" 写入文件错误");
- }
- }
- if(strcmp(command,"read") == 0)
- {
- int num_buf;
- scanf("%d",&num_buf);
- read(k,buf,num_buf);
- printf("%sn",buf);
- memset(buf,0,num_buf);
- }
- if(strcmp(command,"lseek") == 0)
- {
- int pos;
- scanf("%d",&pos);
- lseek(k,pos);
- }
- scanf("%s",command);
- }
- close(k);
- continue;
- }
- if(strcmp(command,"dir") == 0||strcmp(command,"ls") == 0)
- {
- directory();
- continue;
- }
- if(strcmp(command,"quit") == 0)
- {
- printf("quit this filesystem!n");
- break;
- }
- }
- save_disk(disk);
- }