linux程序

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#define ECHO_ORDER 8001
#define LIST_ORDER 8002
#define CD_ORDER 8003
#define PWD_ORDER 8004
#define EXIT_ORDER 8005
#define ERROR_ORDER -1
#define GOOD_ORDER 1
char command[1024];
char curpath[256];
char *margs[10] = {0};
char order[16];
void my_system(char *command)
{
memset(margs,0,sizeof(margs));
int kind;
strcpy(copy,command);
char *argp = NULL;
argp = strtok(" ",command);
strcpy(order,argp);  //\u8bbe\u7f6e\u547d\u4ee4\u53c2\u6570
int argsnum = 0;
while(argp != NULL)
{
margs[argsnum] = (char)malloc(strlen(argp));
if(margs[argsnum] == NULL)
{
cout<<"malloc the room for margs failed"<<endl;
return;
}
argp = strtok(NULL," ");
argsnum++;
}
if(strcmp(order,"echo") == 0)
kind = ECHO_ORDER;
else if(strcmp(order,"ls") == 0)
kind = LIST_ORDER;
else if(strcmp(order,"cd") == 0)
kind = CH_ORDER;
else if(strcmp(order,"pwd") == 0)
kind = PWD_ORDER;
else if(strcmp(order,"exit") == 0)
kind = EXIT_ORDER;
else
kind = ERROR_ORDER;
execute(copy,kind,argsnum);
}
int execute (char *command,int kind,int argsnum)
{
switch(kind)
{
case ECHO_ORDER:
int index = 1;
while(index < argsnum)
{
printf("%s ",margs[index]);
}
printf("/n");
break;
case LIST_ORDER:
execvp("ls",margs);
break;
case CH_ORDER:
if(chdir(margs[2]) < 0)
{
printf("the input dirctory is wrong");
return ERROR_ORDER;
}
break;
case PWD_ORDER:
getcwd(curpath\uff0c256);
printf("%s\n",curpath);
break;
case EXIT_ORDER:
exit(0);
case ERROR_ORDER:
return ERROR_ORDER;
}
}
int main()
{
while(true)
{
cin>>command;
my_system(command);
}
return 0;
}

你可能感兴趣的:(linux程序)