📄 shell.c
字号:
/* Practise 2 shell */
/* lichangshun */
#include<stdio.h>
#include<stdlib.h>
#include<error.h>
#include<sys/wait.h>
#define MAXLINE 200
#define MAXARG 20
#define false 0
#define true 1
#define bool int
extern char **environ;
int T_and=0; // whether have &
int T_lt=0; // <
int T_rt=0; // >
//char * inname;
//char *outname; //use for redirect
bool getargs(int *argcp,char *argv[],int max,bool *eofp){
static char cmd[MAXLINE];
char *cmdp;
int i=0;
int len=0;
*eofp=false;
if(fgets(cmd,sizeof(cmd),stdin)==NULL){
*eofp=true;
return false;
}
if(strchr(cmd,'\n')==NULL){
/*eat up rest of line */
while(1){
switch(getchar()){
case '\n': break;
default: continue;
}//switch
break;
}//while
printf("Line is too long!\n");
return false;
}//if
/*find whether have & or | or < or >*/
len=strlen(cmd);
//printf("len=%d shiyan:%s",len,cmd);
if(cmd[len-2]=='&'){
T_and=1;
cmd[len-2]='\0';
}
//printf("haha%s",cmd);
/*
for(i=0;i<len;i++){
if(cmd[i]=='<'){
T_lt=1;
}
if(cmd[i]=='>'){
T_rt=1;
///////////////////
// cmd[i]='\0';
}
}
*/
/////////////////////
cmdp=cmd;
for(i=0;i<max;i++){
if((argv[i]=(char *)strtok(cmdp," \t\n"))==NULL)
break;
cmdp=NULL; /*tell strtok to keep going*/
}
if(i>=max){
printf("Too many args -- commands ignored!");
return false;
}
*argcp=i;
return true;
/* */
}//getargs
void set(int argc,char *argv[]){
int i=0;
if(argc!=1)
printf("Extra args!\n");
else
for(i=0;environ[i]!=NULL;i++)
printf("%s\n",environ[i]);
}
int main(void){
void execute(int ,char *[]);
char *argv[MAXARG];
int argc=0;
int eof=0;
int len=0;
int i=0;
int counter=0;
char paths[80];
char *temp;
while(1){
eof=0;
//get environment and print and you should change it if nessary!!!
getcwd(paths,80);
// paths[strlen(paths)]='\0';
argv[0]="";
printf("[%s]#",paths);
//
if(getargs(&argc,argv,MAXARG,&eof)&&argc>0){
//
if(strcmp(argv[0],"exit")==0){
//eof=1; //
exit(0);
}
else if(strcmp(argv[0],"lichangshun")==0){
printf("lichangshun is my owner!\n");
}
else if(!strcmp(argv[0],"cd")){ //command_cd
if(argv[1]!=NULL)// if cd char*
{//if(!access(,0)){}
// strcpy(temp,argv[1]);
getcwd(paths,80);
strcat(paths,"/");
strcat(paths,temp);
// printf("@@%s_",argv[1]);
if(!access(paths,0)){
chdir(paths);
//printf("1%s",paths);
}
else if(!access(argv[1],0)){
// printf("%s_",argv[1]);
chdir(argv[1]);
getcwd(paths,80);
// printf("d%s",paths);
}
else printf("bash: cannot find the file or directory!\n");
}
else if(argv[1]==NULL){
//return to higher dir
getcwd( paths,80);
len=strlen(paths);
for(counter=0;counter<len;counter++){
if(paths[len-counter]=='/') {
paths[len-counter]='\0';
break;
}
}
// printf("%s\n",paths);
chdir(paths);
}
}
else if(strcmp(argv[0],"set")==0)
set(argc,argv);
else{
// printf("_%d_%s_%s_%s_%s_%s__",argc,argv[0],argv[1],argv[2],argv[3],argv[4]);
execute(argc,argv);
// printf("__ok__\n");
}
}//if
if(eof) exit(EXIT_SUCCESS);
}//while(1)
}//main
void execute(int argc,char *argv[]){
int pid;
int status;
int i,j;
// printf("*%d*%s*%s**\n",argc,argv[0],argv[1]);
pid=fork();
//printf("..%d..\n",pid);
switch (pid){
//printf("...%d..%s",pid,argv[0]);
case -1:
//
printf("wrong 1 !~\n");
case 0:
j=execvp(argv[0],argv);
if(j==-1){
printf("Wrong command!\n");
exit(1); //if wrong exit(1)
}
printf("..ttttt..\n");
// break;
default:
//
if(T_and==0){
i=waitpid(pid,&status,0);
}
else if(T_and==1){
return;
}
// display_status(pid,status);
// break;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -