⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmd.c

📁 基于UNIX6的文件系统模拟程序
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include "filesys.h"
#include <string.h>


help()
{
	printf("\n");
	printf("\nThis is the shell to test the file system");
	printf("\n 1: \"help\" to view this list");
	printf("\n 2: \"cls\" to clear the screen");
	printf("\n 3: \"dir\" or \"ls\" to view the current directories");
	printf("\n 4: \"cd [name]\" to go in to the directory point out by [name]");
	printf("\n 5: \"mkdir [name]\" to creat a directory with the [name]");
	printf("\n 6: \"creat [name]\" to creat a new file with the [name]");
	printf("\n 7: \"type\/cat [name]\" to view the file");
	printf("\n 8: \"edit [name]\" to creat a new file or open an existed file and edit it");
	printf("\n 9: \"del [name]\"  Sorry,this still has some problem");
	printf("\n10: \"exit\" to exit");
	printf("\n\n");
}

int cmd_login()
{
	int u_id;
	int i=0;
	char passwd[14];

	scanf("%d",&u_id);
	printf("password:");
	fflush(stdin);
	i=0;
	while((passwd[i]=getch()) != 13 && i<14)
	{
		i++;
		putchar('*');
	}
	
	passwd[i]='\0';
	putchar('\n');
	/*scanf("%s",passwd);*/
	return login(u_id,passwd);
}

cmd_ls()
{
	_dir();
	printf("\n");
}

int Iscmd(cmd)
char cmd[6];
{
	int i;
	if(!strcmp(cmd,"cd") || 
		!strcmp(cmd,"mkdir") || 
		!strcmp(cmd,"creat") || 
		!strcmp(cmd,"type") || 
		!strcmp(cmd,"cat") || 
		!strcmp(cmd,"edit") || 
		!strcmp(cmd,"del"))
		return 1;
	else
		return 0;
}

cmd_cmd(cmd,para)
char cmd[6];
char para[8];
{
	char *buf;
	char string[81];
	int i,k;
	int fileid;
	unsigned int dinodeid;
	struct inode *inode;


	if(!strcmp(cmd,"cd"))
	{
		chdir(para);
		return;
	}
	if(!strcmp(cmd,"mkdir"))
	{
		mkdir(para);
		return;
	}
	if(!strcmp(cmd,"creat"))
	{
		dinodeid = namei(para);
		if(dinodeid != NULL)
		{
			printf("the file already existed!\n");
			return;
		}
		fileid = creat(user[user_id].u_uid,para,01777);
		fflush(stdin);//
		//getch();
		i = 0;k=0;
		//while(buf[i++]=getchar() != '#') ;
		buf = malloc(BLOCKSIZ*sizeof(char));
		printf("please input the new file,end inputing by input \"###\":\n");
		while(k!=3)
		{
			buf[i] = getchar();
			if(buf[i] == '#')
			{
				k++;
				if(k == 3)
					break;
			}
			else
				k=0;
			i++;
			if(i/BLOCKSIZ > 0)
			{
				buf = realloc(buf,BLOCKSIZ*(i/BLOCKSIZ+1));
			}
		}
		buf[i-2]='\0';
		write(fileid,buf,i-2);
//		printf("i is:%d,buf is :%s",i,buf);
		/*gets( buf );
//		printf("in creat fileid%d",fileid);
		write(fileid,buf,strlen(buf)+1);
//		printf("inoput is %s",buf);*/
		close(user_id,fileid);
		free(buf);
		return;
	}
	if(!strcmp(cmd,"type") || !strcmp(cmd,"cat"))
	{
		
		dinodeid = namei(para);
		if(dinodeid == NULL)
		{
			printf("system cannot find the file!\nYou'd better check the path or creat it.\n");
			return;////
		}
		fileid = open(user[user_id].u_uid,para,FREAD);//
//		printf("in type fileid%d",fileid);
		inode = iget(dinodeid );
		buf = malloc(inode->di_size*sizeof(char)+1);
		seek(fileid,0,0);
		read(fileid,buf,inode->di_size);
		buf[inode->di_size] = '\0';//
		printf("\n%s:\n%s\n",para,buf);//
		close(user_id,fileid);
		free(buf);
		return;
	}
	if(!strcmp(cmd,"edit"))
	{
		printf("Edit the file with appending :\n");
		dinodeid = namei(para);
		if(dinodeid == NULL)
		{
			fileid = creat(user[user_id].u_uid,para,01777);
		}
		else
		{
			fileid = open(user[user_id].u_uid,para,FAPPEND);
			inode = iget(dinodeid);
			buf = malloc(inode->di_size*sizeof(char)+1);
			seek(fileid,0,0);
			read(fileid,buf,inode->di_size);
			buf[inode->di_size] = '\0';//
			printf("\n%s:\n%s\n",para,buf);//
			//close(user_id,fileid);
			free(buf);
		}
		buf = malloc(BLOCKSIZ*sizeof(char));
		i=0;
		while(k!=3)
		{
			buf[i] = getchar();
			if(buf[i] == '#')
			{
				k++;
				if(k == 3)
					break;
			}
			else
				k=0;
			i++;
			if(i/BLOCKSIZ > 0)
			{
				buf = realloc(buf,BLOCKSIZ*(i/BLOCKSIZ+1));
			}
		}
		buf[i-2]='\0';
		seek(fileid,0,2);
		write(fileid,buf,i-2);
		close(user_id,fileid);
		free(buf);
		return;
	}
	if(strcmp(cmd,"del"))
	{
		del(para);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -