Exemplo n.º 1
0
void
PlaygroundUnlinkTask::exec(void)
{
	switch (getType()) {
	case Task::TYPE_COM:
		execCom();
		break;
	case Task::TYPE_FS:
		execFs();
		break;
	}
}
Exemplo n.º 2
0
int main(int argc,char* argv[]){
	//获取当前路径
	int i=0;
	for(i=0;i<argc;i++){
		puts(argv[i]);
	}
	char command[1000];	//限制命令长度为999个字符
	int comBegPos[20];
	char **args;	
	short argNum=0;
	pid_t child_id;
	int redirectPos=0;
	char*redirectedFileName;
	int fd[2];
	int readPipe;
	int comNum=0;
	int lastOut[2];
	getCurPath(curPath);
	printf("%s$",curPath);
	gets(command);
	while(strcmp(command,"exit")!=0){
		getComAndArgs(command,&args,&argNum);
		comNum=getComNum(args,argNum,comBegPos);
		getComNumAndRedirectedPos(args,&comNum,&redirectPos);
		printf("command:%s,redirectPos:%d\n",command,redirectPos);
		printf("\n\ncommandInfo\ncomNum=%d,isRedirected:%d\n",comNum,redirectPos);
		for(i=0;i<argNum;i++){
			if(args[i]==NULL){
				puts("NULL");
			}else{
				puts(args[i]);
			}
		}
		for(i=0;i<comNum;i++){
			printf("pos:%d\t",comBegPos[i]);
		}
		printf("\n\n\ncontent below:\n");
		lastOut[1]=1;
		if(redirectPos!=0)
		{
			pipe(lastOut);
			redirectedFileName=args[comBegPos[comNum-1]+redirectPos]+1;
			args[comBegPos[comNum-1]+redirectPos]=NULL;
		}
		if(comNum==1){
		//just one command and no pipe
			execCom(0,lastOut[1],args);			
		}else{
			//Given that if pipe(fd) ,fd[0]>0&&fd[1]>0;
			for(i=0;i<comNum;i++){
				if(i==0){
					//the first one
					pipe(fd);
					readPipe=0;
					execCom(readPipe,fd[1],args+comBegPos[i]);
				}else if(i==comNum-1){
					//the last one
					close(fd[1]);
					if(readPipe!=0)
						close(readPipe);
					readPipe=fd[0];
					execCom(readPipe,lastOut[1],args+comBegPos[i]);
				}
				else{
					//not first nor last
					close(fd[1]);
					if(readPipe!=0)
						close(readPipe);
					readPipe=fd[0];
					pipe(fd);
					execCom(readPipe,fd[1],args+comBegPos[i]);
				}
			}
			if(readPipe!=0)
				close(readPipe);
		}
		//has redirectPos and the result must in lastOut[0];
		if(redirectPos!=0){
			int file=open(redirectedFileName,O_RDWR|O_CREAT,S_IRWXU);
			char buffer[10000];
			int len;
			len=read(lastOut[0],buffer,10000);
			write(file,buffer,len);
			close(file);
			close(lastOut[0]);
			close(lastOut[1]);
		}
		int k=0;
		for(k=0;k<argNum;k++){
			//	puts(args[k]);	
			if(args[k]!=NULL);
				free(args[k]);
		}
		free(args);
		printf("%s$",curPath);
		gets(command);
	}	
	puts("sh3.c结束运行");
}