예제 #1
0
파일: myshell.c 프로젝트: salexa92/espl161
void execute(cmdList list, cmdLine *pCmdLine){
	int **pipes;
	int *currPipe;
	cmdLine *cmdline = pCmdLine;
	int i = 0;
	int count = countCmds(cmdline);
	pipes = createPipes(count -1);
	while(i<count){
		pid_t pid = fork();
		if(pid == -1 ){
			perror("fork() ");
			return;
		}
		if(pid == 0 ){
			if(count>1){
				if(cmdline->idx != 0){
					currPipe = leftPipe(pipes,cmdline);
					close(0);
					dup2(currPipe[0],0);
					close(currPipe[0]);
				}
				if(cmdline->next != NULL){
					currPipe = rightPipe(pipes,cmdline);
					close(1);
					dup2(currPipe[1],1);
					close(currPipe[1]);
				}
			}
			if(cmdline->inputRedirect != NULL){
				close(0);
				fopen(cmdline->inputRedirect, "r");
			}
			if(cmdline->outputRedirect != NULL){
				close(1);
				fopen(cmdline->outputRedirect, "w");
			}
			execvp(cmdline->arguments[0], cmdline->arguments);
			perror("Error: ");
			freeCmdLines(pCmdLine);
			freeCmdList(list);
			releasePipes(pipes,count - 1);
			_exit(errno);
		}
		else{
			currPipe = leftPipe(pipes,cmdline);
			if(currPipe != NULL)
				close(currPipe[0]);
			currPipe = rightPipe(pipes,cmdline);
			if(currPipe != NULL)
				close(currPipe[1]);
			if((cmdline->blocking)==1){
				int status;
				waitpid(pid,&status,0);
			}
		}
		cmdline=cmdline->next;
		i++;
	}
	releasePipes(pipes,count-1);
}
예제 #2
0
파일: myshell.c 프로젝트: salexa92/espl161
void testFunc(cmdLine *cmdline){
	cmdLine *counter = cmdline;
	while(counter -> next)
		counter = counter -> next;
	int **pipes = createPipes(counter-> idx);
	if(leftPipe(pipes,cmdline) == NULL)
		printf("Left pipe of ls is null\n");
	if(rightPipe(pipes,counter) == NULL)
		printf("Right pipe of more is null\n");
	cmdLine *tee = cmdline->next;
	printf("fd left pipe: input %d,output %d. fd right pipe: input %d,output %d. \n",leftPipe(pipes,tee)[0],leftPipe(pipes,tee)[1],rightPipe(pipes,tee)[0],rightPipe(pipes,tee)[1]);
	releasePipes(pipes,counter->idx);
}
예제 #3
0
파일: shell.c 프로젝트: GBT3101/Shell
List* execString(cmdLog* cLog, List* env, char* incoming) {
    cmdLine* line;
    cmdLine* firstLine;
    cmdLine* currLine;
    int pipeCounter = 0;
    int * pid;
    int pid2;
    int i;
    int j;
    int **pipefd;
    line = parseCmdLines(incoming);
    currLine = line;
    firstLine = line;
    if (line==NULL || !strcmp(line->arguments[0], "quit")) {
        freeList(env);
        freeCmdLines(line);
        return (List*)-1;
    }
    line= cmdEnvReplace(env, line);
    if (!strcmp(line->arguments[0], "assign")) {
        env= cmdAssign(env, line);
    }
    else if (!strcmp(line->arguments[0], "unassign")) {
        env= cmdUnassign(env, line);
    }
    else if (!strcmp(line->arguments[0], "cd")) {
        cmdCD(cLog, line, incoming);
    }
    else if (line->arguments[0][0]=='!') {
        env= cmdReadLog(cLog, env, line);
    }
    else {
        j = 0;
        pushArray(cLog, incoming);
        if(line->next) {
            while(currLine != NULL) {
                currLine = currLine->next;
                pipeCounter++;
            }
            pipefd= createPipes(pipeCounter-1);
            pid = (int *)malloc(pipeCounter * sizeof(int));
            if (!(pid[j] =fork())) { /* first son*/
                close(STDOUT_FILENO);          /* Close unused write end */
                dup(rightPipe(pipefd, line)[1]);
                close(rightPipe(pipefd, line)[1]);
                coreExec(cLog, env, line);
            }
            close(rightPipe(pipefd, line)[1]);
            j++;
            line = line->next;
            for (i=0; i<pipeCounter-2; i++) {
                if (!(pid[j] =fork())) { /* first son*/
                    close(STDOUT_FILENO);          /* Close unused write end */
                    dup(rightPipe(pipefd, line)[1]);
                    close(rightPipe(pipefd, line)[1]);
                    close(STDIN_FILENO);
                    dup(leftPipe(pipefd, line)[0]);
                    close(leftPipe(pipefd, line)[0]);
                    coreExec(cLog, env, line);
                }
                close(leftPipe(pipefd, line)[0]);
                close(rightPipe(pipefd, line)[1]);
                j++;
                line = line->next;
            }

            if (!(pid[j] =fork())) { /* first son*/
                close(STDIN_FILENO);          /* Close unused write end */
                dup(leftPipe(pipefd, line)[0]);
                close(leftPipe(pipefd, line)[0]);
                coreExec(cLog, env, line);
            }
            close(leftPipe(pipefd, line)[0]);
            for(i=0; i<=j; i++) {
                if(line->blocking!=0) {
                    waitpid(pid[i],0,0);
                }
            }
            releasePipes(pipefd, pipeCounter-1);
        } else {
            if (!(pid2=fork())) {
                coreExec(cLog, env, line);
            }
        }
        if(line->blocking!=0) {
            waitpid(pid2,0,0);
        }
    }
    freeCmdLines(firstLine);
    free(pid);
    return env;
}