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

📄 main.c

📁 操作系统简单的登入程序
💻 C
字号:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include "filesys.h"
// ***
char* chmod="^(chmode)( +)([A-Za-z0-9\\.]+)( +)([0-7]{3})$";

char* mkfile="^(mkfile)( +)([A-Za-z0-9\\.]+)( +)([0-7]{3})$";

char* loginc="^(login)( +)([A-Za-z0-9]+)( +)([A-Za-z0-9]+)$";

char* wtfile="^(wtfile)( +)([A-Za-z0-9\\.]+)$";

char* rdfile="^(rdfile)( +)([A-Za-z0-9\\.]+)$";

char* rmfile="^(rm)( +)([A-Za-z0-9\\.]+)$";

char* newuser="^(adduser)( +)([A-Za-z0-9]+)( +)([A-Za-z0-9]+)( +)([A-Za-z0-9]+)$";

char* rmuser="^(rmuser)( +)([A-Za-z0-9\\.]+)$";

// ***
char* chpwd="^(chpwd)( +)([A-Za-z0-9]+)( +)([A-Za-z0-9]+)$";


//  ***
char* chgrp="^(chgrp)( +)([A-Za-z0-9]+)( +)([A-Za-z0-9]+)$";

//  ***
char* link="^(link)( +)([A-Za-z0-9]+)(/)([A-Za-z0-9\\.]+)( +)([A-Za-z0-9]+)$";
unsigned short currentuser=65535;

char block_buf[BLOCKSIZE];  

char inode_buf[BLOCKSIZE];            

struct filesys* super; 

unsigned short currentuser;

FILE *fd,*fd1;

struct  usergroup directory_1;

struct dirs directory_2;

char command1[100];

char command2[100];

char command3[100];

int rt;
void print_help();
/*void regprint(char * str)
{
	printf("Regex error: %s\n", str);
}*/

int main(int argc, char *argv[])
{
	char* command;

	int pid;
	
	print_help();
	
	fd1=fopen("filesystem","r");

	if(fd1==NULL)
	{
		char yn;

		printf("Disk has not formatted!Format now!!(Y\\N)");

		scanf("%c",&yn);

		if((yn=='y')||(yn=='Y'))
		{
			format();
			printf("fomat completed!\n");
		}
		else
		{
			exit(0);
		}

	}
	else
	{
		printf("Disk has been formated\n");
	close(fd1);
	}
	load();

	
	while(1)
	{
		break;
	 command=(char*)getinput('\n');
	 
	 if(strreg(command,loginc,2)==0)
	   {
		//puts("into here");
		currentuser=login(command1,command2);
			
		if(currentuser==65535)
		{
			printf("NAME OR PASSWORD ERROR!PLEASE RELOGIN!!\n");
		}
				
		else
		{

		 printf("LOGIN SUCCESSFUL!\n");

		free(command);

		break;
		}
	
	}
	else
	{

		printf("PLEASE LOGIN FIRST!\n");
	}

		 free(command);

	}

	while(1)
    	{
		command=(char*)getinput('\n');
	
		if(strreg(command,loginc,2)==0)
		{
			
			currentuser=login(command1,command2);

			if(currentuser!=65535)

				printf("LOGIN SUCCESSFUL!\n");
			
		}

		if(currentuser==65535)
		{
			printf("PLEASE LOGIN FIRST!\n");

		}
		else
		{

			if(strcmp(command,"lsuser")==0)
			{
				if(currentuser==0)
				{
				  int i;
				  for(i=0;i<USERNUM;i++)
				    {
				      if(directory_1.users[i].n_inode!=0)
				      printf("%-8s %-8s \n",directory_1.users[i].u_uid,directory_1.users[i].u_gid);

					}
	
				}
			}
         if(strcmp(command,"help")==0)
           print_help();

			if(strcmp(command,"pwd")==0)
			{
				pwd();
			}
			if(strcmp(command,"format")==0)
			{
				//puts("before format");
				format();
				//puts("after format");
			}
			if(strcmp(command,"ls")==0)
			{
				//puts("before showdir");
				showdir();
				//puts("after showdir");
			}

			if(strcmp(command,"logout")==0)
			{

				logout();
				printf("LOGOUT SUCCESSFUL!\n");	
			}


			if(strcmp(command,"shutdown")==0)
			{
				break;
			}


			if(strreg(command,mkfile,2)==0)
			{				
			  int id1 =oct2dec(atoi(command2));
			  struct inode* pinode=create(currentuser,command1,id1);
			  if (pinode==NULL)
			     {
			       printf("[%s] FILE HAS EXISTED\n",command1);	
			     }
			}

			if(strreg(command,link,3)==0)
			{
				//puts("into link");
				int ret=flink(command1,command2,command3);

				switch(ret)
				{
						case 0:
							printf("CURRENT DIRECTORY DOES NOT EXSITED!\n");

							break;
						case 1:
							printf("SOURCE FILE DOES NOT EXSITED!\n");
							
							break;
						case 2:
							printf("DIRECTORY IS FULL!\n");
							
							break;
						case 3:
							printf("LINK IS SUCCESSFUL\n");
							
						    break;
						case 4:

							printf("YOUR PRIVILEGE IS LIMITED.\n");
							
						    break;
				}
			}
	

			if(strreg(command,chpwd,2)==0)
			{
				
				int id;
				
				id=username2id(command1);

				if(id==USERNUM)
				{
					printf("[%s] USER HAS EXISTED\n",command1);	

				}else if(id!=currentuser)
				{
					printf("YOUR PRIVILEGE IS LIMITED.PLEASE CHECK IT.\n");
				}
				else
				{
					strcpy(directory_1.users[id].password,command2);

					dir1todisk(((struct inode*)posinode(0))->di_addr[0]);

					printf("CHANGE SUCCESSFUL \n");
				}
	
			}



			if(strreg(command,chgrp,2)==0)
			{				
				int id;
				
				id=username2id(command1);

				if(id==USERNUM)
				{
					printf("[%s] USER HAS EXISTED\n",command1);	

				}else if(0!=currentuser)
				{
					printf("YOUR PRIVILEGE IS LIMITED.PLEASE CHECK IT.\n");
				}
				else
				{
					strncpy(directory_1.users[id].u_gid,command2,7);

					dir1todisk(((struct inode*)posinode(0))->di_addr[0]);

					printf("CHANGE SUCCESSFUL \n");
				}
	
			}

			if(strreg(command,chmod,2)==0)
			{

				int id1 =oct2dec(atoi(command2));

				struct inode* pinode=fchmod(command1,id1);

				if (pinode==NULL)
				{
				printf("[%s] FILE HAS EXISTED\n",command1);	
				}
	
			}

			if(strreg(command,wtfile,1)==0)
			{
				unsigned short inodeid=map2inode(command1);
				if (inodeid==MAPERR)
				{
					 printf("WRITE FAILED.[%s] FILE HAS NOT CREATED .\n",command1);
				}
				else
				{

					printf("PLEASE INPUT TEXT CONTEXT\n");

					char* content=getinput('$');
				
					if(writefile(content,command1)==NULL)
					{
					printf("WRITE FAILED. YOUR PRIVILEGE IS LIMITED.PLEASE CHECK IT.\n");	
					}
					else
					{
						printf("WRITE STRING TO FILE SUCCESSFUL\n");
					}
				}
			}
			if(strreg(command,rdfile,1)==0)
			{


				char* content=(char*)readfile(command1);
				
				if(content==NULL)
				{
				  printf("[%s] FILE HAS NOT CREATED.PLEASE FIRST CREATE IT.\n",command1);	
				}
				else
				{
					printf("%s\n",content);
				}
		
		
			}
			
			if(strreg(command,rmfile,1)==0)
			{


				int i=rmf(command1);
				
				if(i==0)
				{
				  printf("[%s] FILE HAS NOT EXISTED.\n",command1);	
				}
				else if(i==1)
				{
					printf("[%s] FILE HAS DELETED.\n",command1);
				}
				else
				{
					printf("YOUR PRIVILEGE IS LIMITED\n",command1);
				}
		
		
			}

			if(strreg(command,rmuser,1)==0)
			{

				int i=deluser(command1);
				
				if(i==2)
				{
				  printf("[%s] USER HAS NOT EXISTED.\n",command1);	
				}
				else if(i==1)
				{
					printf("[%s]  USER HAS DELETED.\n",command1);
				}
				else
				{
					printf("YOUR PRIVILEGE IS LIMITED\n",command1);
				}
		
		
			}

			if(strreg(command,newuser,3)==0)
			{

				int ret=fnewuser(command1,command2,command3);
				
				if(ret==1)
				{
				  printf("[%s] USER CTREATED SUCCESSFUL\n",command1);	
				}
				else if(ret==2)
				{
					printf("YOUR PRIVILIEGE IS LIMITED\n");
				}
				else
				{
					printf("CREATE FAILED!USER HAS EXISTED\n");
				}						
			}			

		}

		free(command);

    }

    exit(0);

}

void print_help()
{
    system("clear");	
    system("pwd");
    printf("*************************************\n");
    printf("format:creat a new file system\n");
    printf("adduser:add a user\n");
    printf("mkfile FILENAME:create a file\n");
    printf("cat FILENAME:write content to  file\n");
    printf("vi  FILENAME:read  content from  file\n");
    printf("ls:list files\n");
    printf("*************************************\n");
}

⌨️ 快捷键说明

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