int main(int argc,char *argv[])
{
while(1)
{
printf("lb@lb-virtualbox:~$ ");
ReadCommand();
AnalysisCommand();
DoCommand();
}
return 0;
}
void ReadCommand()
{
char a;
int i=0;
while(scanf("%c",&a)!EOF&&a!='\n'&&i<256)
{
command[i]=a;
i++;
}
command[i]=0;
}
void AnalysisCommand()
{
int i=0,j=0;
int flag=0;
for(int k=0,k<strlen(command);k++)
{
if(command[k]==' '||command[k]=='\t')
{
if(flag==0)
{
par[i][j]=0;
i++;
j=0;
flag=1;
}
}
else
{
par[i][j]=command[i];
j++;
flag=0;
}
}
if(flag==1)
{
par[i][j]=0;
i++;
par[i][0]=0;
}
}
void CreatFile()
{
int tt;
char path[255];
getcwd(path,255);
strcat(path,"/");
strcat(path,par[1]);
tt=syscall(SYS_creat,path,6666);
syscall(SYS_close,tt);
}
void ShowFile()
{
char path[255];
FILE *fp;
char c;
getcwd(path,255);
strcat(path,"/");
stract(path,par[1]);
if((fp=fopen(path,"r"))==NULL)
printf("打不开文件\n");
while((c=fgetc(fp))!=EOF)
printf("%c",c);
}
void CopyFile()
{
char oldpath[100],newpath[100];
FILE *oldfp,*newfp;
char c;
getcwd(oldpath,100);
getcwd(newpath,100);
stract(oldpath,"/");
stract(oldpath,par[1]);
stract(newpath,"/");
stract(newpath,par[2]);
if((oldfp=fopen(oldpath,"r"))==NULL)
printf("打不开原文件\n");
if((newfp=fopen(newpath,"w"))==NULL)
printf("新文件创建失败\n");
while((c=fgetc(oldfp))!=EOF)
fputc(c,newfp);
fclose(oldfp);
fclose(newfp);
}
int DeleteFile()
{
char path[255];
getcwd(path,255);
stract(path,"/");
stract(path,par[1]);
if(remove(path)==0)
return 0;
}
void ShowDir()
{
DIR* dirp;
struct dirent * ent;
if(strcmp("",par[1])==0)
dirp=opend(".");
else dirp=opendir(par[1]);
while((ent=readdir(dirp))!=NULL)
{
if(ent->d_name[0]!='.')
printf("%s\t",ent->d_name);
}
printf("\n");
closedir(dirp);
}
int DoCommand()
{
if(strcmp("ShowDir",par[0])==0)
{
ShowDir();
return 0;
}
else if(strcmp("CreatFile",par[0])==0)
{
CreatFile();
return 0;
}
else if(strcmp("ShowFile",par[0])==0)
{
ShowFile();
return 0;
}
else if(strcmp("CopyFile",par[0])==0)
{
CopyFile();
return 0;
}
else if(strcmp("DeleteFile",par[0])==0)
{
DeleteFile();
return 0;
}
else
{
char *p;
char d[]=" ";
int n=1,i=1;
char q[100][100];
char **a;
strcopy(q[0],strtok(command,d));
while(p=strtok(NULL,d))
{
strcpy(q[i++],p);
n++;
}
a=malloc(sizeof(char *)*n+1);
for(int i=0;i<n+1;i++)
{
a[i]=malloc(sizeof(char)*100);
if(i<n)
strcpy(a[i],q[i]);
}
a[n]==NULL;
if(fork()==0)
{
if(execvp(a[0],a)<0)
{
printf("失败\n");
return -1;
}
}
else
{
int pidReturn =wait(NULL);
}
free(a);
}
}