📄 utility.c
字号:
#include "myshell.h"
void printPrompt()
{ char buf[MaxCommandLine]; char buf1[MaxCommandLine+2]; char * promptStr = getcwd(buf,sizeof(buf)); if( !promptStr ) { sprintf(buf,"<null>"); promptStr = buf; } else { sprintf(buf1,"%s::",buf); promptStr = buf1; }
if ( write(STDOUT_FILENO, promptStr, strlen(promptStr) )!=strlen(promptStr))
{
errhandle("cannot write to stdout");
}
}
void errhandle(char* msg)
{
perror(msg);
exit(1);
}
int execute(const char *cmd){ int stat; pid_t pid; struct sigaction sa, savintr, savequit; sigset_t saveblock; if (cmd == NULL) return(1); sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigemptyset(&savintr.sa_mask); sigemptyset(&savequit.sa_mask); sigaction(SIGINT, &sa, &savintr); sigaction(SIGQUIT, &sa, &savequit); sigaddset(&sa.sa_mask, SIGCHLD); sigprocmask(SIG_BLOCK, &sa.sa_mask, &saveblock); if ((pid = fork()) == 0) { sigaction(SIGINT, &savintr, (struct sigaction *)0); sigaction(SIGQUIT, &savequit, (struct sigaction *)0); sigprocmask(SIG_SETMASK, &saveblock, (sigset_t *)0); execl("/bin/sh", "sh", "-c", cmd, (char *)0); _exit(127); } if (pid == -1) { stat = -1; /* errno comes from fork() */ } else { while (waitpid(pid, &stat, 0) == -1) { if (errno != EINTR){ stat = -1; break; } } } sigaction(SIGINT, &savintr, (struct sigaction *)0); sigaction(SIGQUIT, &savequit, (struct sigaction *)0); sigprocmask(SIG_SETMASK, &saveblock, (sigset_t *)0); return(stat);}int newEnvironParams(const char * name,const char * value){ return setenv(name,value,1);}void InitEnvironParams(){ char buf[MaxCommandLine]; char buf2[MaxCommandLine*2]; char * str = getenv("shell"); char * pwd = getcwd(buf,sizeof(buf)); if(!pwd) { return; } if(!str) {// printf("shell params do not exist.\n"); sprintf(buf2,"%s",buf); } else {// printf("shell params exist.\n"); sprintf(buf2,"%s;%s",str,buf); } unsetenv("shell"); newEnvironParams("shell",buf2); newEnvironParams("parent",buf2);}
void quitShell(char* params[], int paramNumber)
{
exit(0);
}
void changeDir(char* params[], int paramNumber)
{
if (paramNumber!=2)
{// int i = 0;// for( i =0 ; i< paramNumber ; i++)// printf("--%s--%d\n",params[i],paramNumber);
printf(helpStr);
}
else
{
if (chdir(params[1])<0)
{
if (errno==ENOTDIR||errno==ENOENT)
{ char buf[MaxCommandLine]; getcwd(buf,MaxCommandLine); printf("The directory %s does not exist\nThe Current Directory is %s\n", params[1],buf);
}
}
}
}
void dodir(char* path,FILE * fp)
{
DIR* dp;
struct dirent* dirnode;
if ((dp=opendir(path))!=NULL)
{
while ((dirnode=readdir(dp))!=NULL)
{ if(fp) { fwrite(dirnode->d_name,strlen(dirnode->d_name),1,fp); fwrite("\n",1,1,fp); } else
printf("%s\n", dirnode->d_name);
}
}
else
{ char buf[MaxCommandLine]; char buf2[MaxCommandLine*2]; getcwd(buf,MaxCommandLine);
sprintf(buf2,"The directory %s does not exist. \nThe Current directory:%s\n", path, buf); if(fp) { fwrite(buf2,strlen(buf2),1,fp); fwrite("\n",1,1,fp); } else
printf("%s\n", dirnode->d_name);
}
}
void listDir(char* params[], int paramNumber)
{ int index0 = -1; FILE * fhandle = NULL; int outputtype = outputDirect(params,paramNumber,&index0); if( index0 >= paramNumber) { printf("Something Wrong. Please contact ningyu@siu.edu.\n"); return; } switch(outputtype) { case 1: if( index0 == -1 ) { break; } fhandle = fopen(params[index0],"w"); break; case 2: if( index0 == -1 ) { break; } fhandle = fopen(params[index0],"a"); break; case 0: default: break; }
if ( (paramNumber==3 && outputtype) || paramNumber == 1)
{
dodir(".",fhandle);
}
else
{
dodir(params[1],fhandle);
} if(fhandle)
fclose(fhandle);
}
void clrFile(char* params[], int paramNumber){ execute("clear");}
void environFile(char* params[], int paramNumber){ int index0 = -1; int outputtype = outputDirect(params,paramNumber,&index0); if( index0 >= paramNumber) { printf("Something Wrong. Please contact ningyu@siu.edu.\n"); return; } char str[MaxCommandLine]; switch(outputtype) { case 1: if( index0 == -1 ) { sprintf(str,"set"); } sprintf(str,"set > %s",params[index0]); break; case 2: if( index0 == -1 ) { sprintf(str,"set"); } sprintf(str,"set >> %s",params[index0]); break; default: sprintf(str,"set"); break; } execute(str);}
void echoFile(char* params[], int paramNumber){ int i = 0; int index0 = -1; FILE * fhandle = NULL; int outputtype = outputDirect(params,paramNumber,&index0); if( index0 >= paramNumber) { printf("Something Wrong. Please contact ningyu@siu.edu.\n"); return; } char str[MaxCommandLine]; switch(outputtype) { case 1: if( index0 == -1 ) { for( i=1 ; i < paramNumber ; i++ ) { printf("%s ",params[i]); } printf("\n"); break; } fhandle = fopen(params[index0],"w"); for( i=1 ; i < index0 - 1 ; i++ ) { fwrite(params[i],strlen(params[i]),1,fhandle); fwrite(" ",1,1,fhandle); } fclose(fhandle); break; case 2: if( index0 == -1 ) { for( i=1 ; i < paramNumber ; i++ ) { printf("%s ",params[i]); } printf("\n"); break; } fhandle = fopen(params[index0],"a"); for( i=1 ; i < index0 - 1 ; i++ ) { fwrite(params[i],strlen(params[i]),1,fhandle); fwrite(" ",1,1,fhandle); } fclose(fhandle); break; default: for( i=1 ; i < paramNumber ; i++ ) { printf("%s ",params[i]); } printf("\n"); break; } }void helpFile(char* params[], int paramNumber){// printf(helpStr); int index0 = -1; FILE * fhandle; int outputtype = outputDirect(params,paramNumber,&index0); if( index0 >= paramNumber) { printf("Something Wrong. Please contact ningyu@siu.edu.\n"); return; } char str[MaxCommandLine]; switch(outputtype) { case 1: if( index0 == -1 ) { execute("more readme"); } fhandle = fopen(params[index0],"w"); fwrite(helpStr,strlen(helpStr),1,fhandle); fclose(fhandle); break; case 2: if( index0 == -1 ) { execute("more readme"); } fhandle = fopen(params[index0],"a"); fwrite(helpStr,strlen(helpStr),1,fhandle); fclose(fhandle); break; default: execute("more readme"); break; }}void pauseFile(char* params[], int paramNumber){ char buf[MaxCommandLine]; char str[] = "enter";
int n;
printf("To continue, please press \'enter\' command:\n"); memset(buf,0,sizeof(buf)); while ((n=read(STDIN_FILENO, buf, MaxCommandLine))>0)
{ if( (n=strcmp(buf, str)) >= 0)
{ break;
} else { printf("enter error%d,%s\n",n,buf); printf("To continue, please press \'enter\' command:\n"); } memset(buf,0,sizeof(buf));
}}//The returned value presents the type of output direction.//1 means '>'//2 means '>>'//0 means no output direction.//The returned param fileIndex is behalf of the index number in params of the output file name.int outputDirect(char* params[], int paramNumber, int * fileIndex){ int i = 0; for( i=1 ; i < paramNumber ; i++ ) { if (strcmp(params[i],">>") == 0) { if(i+1 < paramNumber ) { *fileIndex = i+1; return 2; } } else if (strcmp(params[i],">") == 0) { if ( i+1 < paramNumber ) { *fileIndex = i+1; return 1; } } } return 0;}void inputDirect(char* params[], int paramNumber){ int i = 0; int nIndex = -1; printf("xxxx%d \n",paramNumber); for( i=1 ; i < paramNumber ; i++ ) { if (strcmp(params[i],"<") == 0) { if ( i+1 < paramNumber ) { nIndex = i+1; } } } //It means that no input redirection exists. return 0; if( nIndex == -1 ) { printf("no input redirection!\n"); return ; } printf("input redirection ok!\n"); return ;}int ampersandFunc(char* cmdStr){ int n, paramNumber;
int commandType;
char* params[MaxParamNumber]; pid_t pID = fork(); if (pID == 0) // child { strtok(cmdStr, "&");// printf("+---%s\n",cmdStr); commandType=parseCommand(cmdStr, params, ¶mNumber);
switch (commandType)
{ case -2:
printf("illegal command params.\n%s"); break; case -1: printf("The command \'%s\' is executed by /bin/shell\n",cmdStr); execute(cmdStr); break; case -3: break; default: commandArray[commandType](params, paramNumber); break;
} _exit(127); } else if (pID < 0) // failed to fork { printf("Failed to fork"); exit(1); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -