📄 myshell.c
字号:
#include "myfilesys.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct command {
char word[15][15];
int num;
} Command;
struct {
char pnstack[15][15];
char pathname[50];
int sp;
}Default_Dir;
void ls(Command command);
void cd(Command command);
void MKDIR(Command command);
void cat(Command command);
Command GetCommand(const char *string);
Command GetCommand(const char *string)
{
Command command;
int i,j=0;
command.num=0;
for(i=0; ;i++)
{
while((*string!=' ') && (*string!='\012'))
{
command.word[i][j]=*string;
j++;
string++;
}
command.word[i][j]='\0';
command.num++;
if (*string == '\012')
break;
string++;
j = 0;
}
return command;
}
void ls(Command command)
{
if (command.num == 1)
List(Default_Dir.pathname, 0);
else if (strcmp(command.word[1], "-l") == 0)
List(Default_Dir.pathname, 1);
else
printf("\nError! usage : ls or ls -l\n");
}
void cd(Command command)
{
int i;
char tempname[15], temppathname[50];
if (command.num != 2)
printf("\nError! usage : cd dirname\n");
else {
if (strcmp(command.word[1], "..") == 0) {
if (Default_Dir.sp != 0) {
Default_Dir.sp--;
strcpy(Default_Dir.pathname, Default_Dir.pnstack[0]);
}
if (Default_Dir.sp >= 1)
strcat(Default_Dir.pathname, Default_Dir.pnstack[1]);
if (Default_Dir.sp >= 2)
for (i = 2; i <= Default_Dir.sp; i++) {
strcpy(tempname, "/");
strcat(tempname, Default_Dir.pnstack[i]);
// printf("nstack[%d]: %s", i, Default_Dir.pnstack[i]);
// printf("tempname:%s", tempname);
strcat(Default_Dir.pathname, tempname );
}
} else if (strcmp(command.word[1], ".") != 0) {
strcpy(temppathname, Default_Dir.pathname);
if (Default_Dir.sp == 0)
strcat(temppathname, command.word[1]);
else
strcat(strcat(temppathname, "/"), command.word[1]);
if (SearchDir(temppathname) == 0)
printf("\ncd():Can't find the dir: %s\n", command.word[1]);
else {
if (Default_Dir.sp == 0) strcat(Default_Dir.pathname, command.word[1]);
else {
strcat(strcat(Default_Dir.pathname,"/"), command.word[1]);
}
strcpy(Default_Dir.pnstack[++Default_Dir.sp], command.word[1]);
}
}
}
}
void MKDIR(Command command)
{
char temppathname[50];
strcpy(temppathname, Default_Dir.pathname);
if (Default_Dir.sp == 0)
strcat(temppathname, command.word[1]);
else
strcat(strcat(temppathname, "/"), command.word[1]);
if (command.num != 2)
printf("\nError! usage: mkdir filename\n");
else {
mymkdir(temppathname);
}
}
void cat(Command command)
{
int fd,n;
char string[1024],temppathname[50];
strcpy(temppathname, Default_Dir.pathname);
if((strcmp(command.word[1], ">") == 0))
{
if (Default_Dir.sp == 0)
strcat(temppathname, command.word[2]);
else
strcat(strcat(temppathname, "/"), command.word[2]);
fd=Open(temppathname, O_RDWR|O_CREAT, 0644 );
if(fd<0)
{
printf("the file not exist\n");
}
else
{
scanf("%s", string);
getchar();
Write(fd,string,1024);
}
}
else
{
if (Default_Dir.sp == 0)
strcat(temppathname, command.word[1]);
else
strcat(strcat(temppathname, "/"), command.word[1]);
fd=Open(temppathname, O_RDONLY);
if(fd<0)
{
printf("the file not exist\n");
}
else
{
while(1)
{n=Read(fd,string,1024);
if(n<=0)
break;
printf("%s\n",string);
}
}
}
Close(fd);
}
int main(void)
{
char string[50];
int i;
Command command;
strcpy(Default_Dir.pnstack[0],"/"); //init Default_Dir
strcpy(Default_Dir.pathname,"/");
Default_Dir.sp = 0;
while (1) {
printf("[myshell@%s]$", Default_Dir.pathname);
i = 0;
while(1)
{
string[i] = getchar();
if(string[i]=='\012')
break;
i++;
}
command = GetCommand(string);
/*
for (i = 0; i < command.num; i++)
printf("num: %d, word[%d]:%s\n", command.num, i, command.word[i]);
*/
if (strcmp(command.word[0], "ls") == 0)
ls(command);
else if (strcmp(command.word[0], "mkdir") == 0)
MKDIR(command);
else if (strcmp(command.word[0], "cd") == 0)
cd(command);
else if (strcmp(command.word[0], "cat") == 0)
cat(command);
else if (strcmp(command.word[0], "exit") == 0){
Over();
break;
} else
printf("-myshell: %s : command not found.\n", command.word[0]);
}
printf("\n\n\n\n\n\n\n\n Thank you for using!\n\n\n ---------Copyright (c) 2005.1.9 HGMJCL Team. All rights reserved!\n\n\n\n\n\n\n\n\n\n\n\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -