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

📄 main.c

📁 操作系统课程设计
💻 C
字号:

#include <stdio.h>
#include "filesys.h"
#include <stdlib.h>
#include <string.h>
#include <windows.h>     //for Windows console functions
#include <conio.h>       //for kbhit(), getche()
#include <math.h>        //for sin, cos
#include "msoftcon.h"

struct hinode hinode[NHINO];
struct dir dir;
struct file sys_ofile[SYSOPENFILE];
struct filsys filsys;
struct pwd pwd[PWDNUM];
struct user user[USERNUM];
FILE * fd;
struct inode * cur_path_inode;
int user_id,flag=1;
char cpath[512];
char buff[BLOCKSIZ];
static int current=-1;
static int all_user[USERNUM];
int countn=0;
extern  draw();

void main()
{
	int user_id=0;
	char stmp[512];
	char ch;
	int ret,i;
	char * buf;
	unsigned short cr_fd;
	
	init_graphics();   //初始化图形
	draw();
	while(1){
        countn++;
		if(countn>4)  {
			set_cursor_pos(1,1);//一列i行
            draw();
			set_cursor_pos(1,25);//一列i行
		}
    set_color(cBLACK,cWHITE);

	if(current>=0)printf("user%d:%s>",user[all_user[current]].u_uid,cpath);
	else printf("%s>",cpath);

	scanf("%s",stmp);
    set_color(cBLACK,cWHITE);

	if (!stricmp(stmp,"format")||!stricmp(stmp,"0"))
	{
		if(current>=0){
			printf("用户登陆中,不允许格式化!\n");
			continue;
		}
		printf("\n磁盘格式化将删除所有文件,确定格式化么(y/n)?");
		getchar();
		ch=getchar();
		if ((ch=='Y')||(ch=='y')){
			format();
		}
		continue;
	}
	
	if (!stricmp(stmp,"install")||!stricmp(stmp,"1"))
	{   
		printf("系统启动中...");
		wait(2000);
		install();
		current=-1;
		continue;
	}

	if (!stricmp(stmp,"login")||!stricmp(stmp,"2"))
	{
		char user[64],pass[64];
		int usr_id,ret;
		printf("用户ID:");
		scanf("%s",user);
		printf("密码:");
		scanf("%s",pass);
		usr_id=atoi(user);
		if (!usr_id)
		{
			printf("\n用户密码错误!\n");
			continue;
		}
		ret=login(usr_id,pass,current,all_user);
		if (ret)
		{
			printf("\n%s 登陆成功!\n",user);
			current++;
		}
		continue;
	}
	
	if (!stricmp(stmp,"logout")||!stricmp(stmp,"3"))
	{ 
		if(strcmp(cpath,"\\")){printf("请退回根目录再注销。\n");continue;}
		if(current<0){printf("所有用户均已注销!\n");continue;}
		logout(user[all_user[current]].u_uid);
		printf("%d注销成功.\n",user[all_user[current]].u_uid);
		user[all_user[current]].u_uid=0;
		current--;
		continue;
	}

    if (!stricmp(stmp,"quit")||!stricmp(stmp,"11"))
	{
		halt();
	}	

	if (current<0){
		printf("\n 没有用户登陆!\n");
		continue;
	}

     if (!stricmp(stmp,"dir")||!stricmp(stmp,"4"))
	{
		_dir();
		continue;
	}

	if (!stricmp(stmp,"md")||!stricmp(stmp,"5"))
	{
		char mdir[64];
    	scanf("%s",mdir);   
		mkdir(mdir);
		_dir();
		continue;
	}

	if (!stricmp(stmp,"cd")||!stricmp(stmp,"6"))
	{
		char cdir[64];
		scanf("%s",cdir);         
		chdir(cdir);
		_dir();
		continue;
	}

	if (!stricmp(stmp,"creat")||!stricmp(stmp,"7"))
	{
		char file[64];
		int i;
	/*	printf("please input the file name you want to create:"); */
		scanf("%s",file);      
		i=creat(user_id,file,01777);
		if(i==-1)
		{
			printf("文件创建失败.\n");
			continue;
		}
		cr_fd=i;
		printf("你是否想写这个文件(others/n)?  ");
		getchar();
		ch=getchar();
		getchar();
		if (ch!='n')
		{
			int i,j,t;
			char file[64];
			printf("请输入你想要写的内容:\n");
		    gets(file);	
			j=strlen(file);
		    buf = (char *) malloc(j);
			for(i=0;i<j;i++)    buf[i]=file[i];
			t=write(cr_fd, buf,j);
			
		}
		_dir();
		printf("\n");
		close(user_id, cr_fd);
		continue;
	}

	if (!stricmp(stmp,"del")||!stricmp(stmp,"10"))
	{
		char file[64];
	/*	printf("which file do you want to delete?");     */
		scanf("%s",file);
		delete(file);
		_dir();
		//printf("文件已被删除.\n");
		continue;
	}
    
    if (!stricmp(stmp,"write")||!stricmp(stmp,"8"))
	{
		char tfname[64];
		char file[64];
		int i,j,t,openid;
 /*     printf("please input the file name you want to open:");      */
		scanf("%s",tfname);
		openid=aopen(user_id,tfname,FAPPEND);
 		if(!openid)
		{
			printf("\n不能打开文件%s! \n",tfname);	
			continue;    
		} 
	   getchar();
	   while(1){
       printf("输入你要写的文件内容:\n");      
	   gets(file);     
	   j=strlen(file);
       buf = (char *) malloc(j);
	   for(i=0;i<j;i++)    buf[i]=file[i];
	   t=write(openid, buf,j);
	   printf("按任意键继续写,n 退出:  ");
	   ch=getchar();
	   getchar();
		if ((ch=='N')||(ch=='n'))
			break;
	   }
	   _dir();		   	  
	   close(user_id, openid);
	}
	if (!stricmp(stmp,"read")||!stricmp(stmp,"9"))
	{
		int fsize,tcount;
		char * tbuf;
		char tfname[64];
     /* printf("please input the file name you want to read:");    */
		scanf("%s",tfname);
		ret=aopen(user_id,tfname,READ);   //返回用户打开文件表中指向该文件的位置
		if (!ret)
		{
			printf("\n不能打开文件%s \n",tfname);
			continue;
		}
		fsize=getfilesize(ret);            //要打开的文件的大小
		printf("文件内容:");
		tbuf=malloc(fsize);                //缓冲区
		read(ret,tbuf,fsize);              //把文件读到缓冲区里面
		printf("\n");
		tcount=0;
		for(i=0;i<fsize;i++)
		{
			printf("%c",tbuf[i]);
			tcount++;
			if (tcount>70)
			{
				tcount=0;
				printf("\n");
			}
		}
		printf("\n");
		close(user_id, ret);
		continue;
	}

	}//while

}

draw(){
       set_color(cBLACK,cWHITE);
	printf("***************************欢迎使用本文件系统******************************* \n");
    printf("                                                                             \n");
	printf("   0(format)---------格式化磁盘          6(cd)-------------更改目录(路径)    \n");
	printf("   1(install)--------启动系统            7(creat)----------创建文件          \n");
	printf("   2(login)----------用户登陆            8(write)----------填写文件           \n");
	printf("   3(logout)---------用户注销            9(read)-----------读取文件          \n");
	printf("   4(dir)------------显示目录表         10(del)------------删除文件          \n");
	printf("   5(md)-------------创建新目录         11(quit)-----------退出文件系统      \n");
	printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
}

⌨️ 快捷键说明

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