#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define SHELL_BUILDIN_CD 1
#define SHELL_NORMAL 2
#define BUFFSIZE 256
/**
using quit cmd to exit
*/
int isQuitCmd(char * arg){
static char *quit = "quit\n";
return strcmp(arg,quit) == 0 ? 1 : 0;
}
/**
decide whether the cmd is build-in or normal
*/
int getCmdType(char *firstCmd){
if(strcmp(firstCmd ,"cd" ) == 0){
return SHELL_BUILDIN_CD;
}else{
return SHELL_NORMAL;
}
}
int parserCMD(char * cmdArray[], char *argv){
int cnt = 0;
int len = strlen(argv);
argv[len-1]='\0';
char *p = strtok (argv, " ");
while(p != NULL)
{
cmdArray[cnt] = new char[25];
strcpy(cmdArray[cnt],p);
cnt++;
if(p!=NULL)
p = (char *)strtok(p+strlen(p)+1," ");
}
return cnt;
}
int buildInCDCMD(char * args[]){
char curDir[BUFFSIZE];
if( args[1] == NULL){
char *cur = getenv("HOME");
printf("current Dir:%s\n",cur);
return 0;
}
char first = args[1][0];
if(first != '/' && first != '.'){
//concat the cd args with current dir
getcwd(curDir, BUFFSIZE);
strncat(curDir,"/",BUFFSIZE - strlen(curDir));
}
strncat(curDir,args[1],BUFFSIZE - strlen(curDir));
if(chdir(curDir) == -1){
fprintf(stderr,"chdir fail.......\n");
}
memset(curDir,0,sizeof(curDir));
getcwd(curDir,BUFFSIZE);
printf("current Dir:%s\n",curDir);
return 0;
}
int normalCMD(char * args[]){
int pid = 0;
int status = 0;
pid = fork();
if(pid == 0){
if(execvp(args[0], args) < 0){
fprintf(stderr,"execvp error.......\n");
exit(1);
}
}
waitpid(pid, &status, 0);
return status;
}
/**
deal the cmd
*/
int handlerCmd(char *cmd){
int cmdnum;
char *args[100];
int argnum;
argnum = parserCMD(args, cmd);
args[argnum] = NULL;
char *firstCmd = args[0];
int cmdType = getCmdType(firstCmd);
switch(cmdType){
case SHELL_BUILDIN_CD:
buildInCDCMD(args);
break;
case SHELL_NORMAL:
normalCMD(args);
break;
}
}
int main(){
char cmd[BUFFSIZE];
printf("enter a shell cmd (quit to exit)\n");
while(1){
memset(cmd,0,sizeof(cmd));
int readCnt = 0;
readCnt = read(STDIN_FILENO,cmd,BUFFSIZE);
if(readCnt < 0){
fprintf(stderr,"read cmd failed and exit(-1)\n");
exit(-1);
}
if(isQuitCmd(cmd)){
break;
}
handlerCmd(cmd);
printf("enter new shell cmdi (quit to exit )\n");
}
return 0;
}
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define SHELL_BUILDIN_CD 1
#define SHELL_NORMAL 2
#define BUFFSIZE 256;
/**
using quit cmd to exit
*/
int isQuitCmd(char * arg){
static char *quit = "quit";
return strcmp(arg,quit) == 0 ? 1 : 0;
}
/**
decide whether the cmd is build-in or normal
*/
int getCmdType(char *firstCmd){
if(strcmp(firstCmd == "cd" )){
return SHELL_BUILDIN_CD;
}else{
return SHELL_NORMAL;
}
}
/**
deal the cmd
*/
int handlerCmd(char *cmd){
int cmdnum;
char *args[100];
int argnum;
argnum = parserCMD(args, cmd);
args[argnum] = NULL;
char *firstCmd = args[0];
int cmdType = getCmdType(firstCmd);
switch(cmdType){
case SHELL_BUILDIN_CD:
buildInCDCMD();
break;
case SHELL_NORMAL:
normalCMD();
break;
}
}
int buildInCDCMD(char * args[]){
char curDir[BUFFSIZE];
char first = args[1][0];
if(first != '/' && first != '.'){
//concat the cd args with current dir
getcwd(curDir, BUFFSIZE);
strncat(curDir,"/",BUFFSIZE - strlen(curDir));
}
strncat(curDir,args[1],BUFFSIZE - strlen(curDir));
if(chdir(curDir) == -1){
fprintf(stderr,"chdir fail.......");
}
return 0;
}
int normalCMD(char * args[]){
int pid = 0;
int status = 0;
pid = fork();
if(pid == 0){
if(execvp(args[0], args) < 0){
fprintf(stderr,"chdir fail.......");
exit(1);
}
}
waitpid(pid, &status, 0);
return status;
}
int parserCMD(char * cmdArray[], char *argv){
int cnt = 0;
char *p = strtok (argv, " ");
while(p != NULL)
{
strcpy(cmdArray[cnt],p);
cnt++;
if(p!=NULL)
p = (char *)strtok(p+strlen(p)+1," ");
}
return cnt;
}
int main(int argc, char *[]argv){
char cmd[BUFFSIZE];
while(1){
memset(cmd,0,sizeof(cmd));
int readCnt = 0;
readCnt = read(STDIN_FILENO,cmd,BUFFSIZE);
if(readCnt < 0){
fprintf(stderr,"read cmd failed and exit(-1)");
exit(-1);
}
if(isQuitCmd(cmd)){
break;
}
handlerCmd(cmd);
}
}
|