I am trying to write a shell that will run Unix command as well as convert certain dos commands to unix commands and everything is working properly except for when piping is requested (ie who | sort). I'm trying to wrap my head around pipes and fork() and how to pass results using execvp() and my brain is now noodles.

As I understand how this is supposed to work, I have established a pipe and I'm trying to write the result of the first program (who) to the pipe and then read from the pipe and have that be the input to the second program (sort). So the result of who | sort should be an alphabetized list of users connected to a Unix system. Instead nothing is displayed and the user is then prompted for new input. If I use dir, ls, cat, type, sort, etc... by themselves without piping they work just fine. If anyone has any suggestions as to how I can fix this it would be much appreciated. Thanks!

Code:
#include<stdio.h>
#include<sys/file.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<signal.h>

int fg_id = 0;
void sigchldHandler(){
	int pid, status;
	pid = wait(&status);
	if(pid == fg_id) fg_id = 0;
	signal(SIGCHLD, sigchldHandler);
	}

main(){
	int fd, new_cid;
	int fdes[2];
	int exit = 0;
       	int background, piping, redirection1, redirection2;
	int backgroundPos, pipingPos, redirectionPos;
	int i = 0;
	int j = 0;
	int pos = 11;
	int found = 0;
       	char *delimiter = " ";
       	char *tkn;
	char *dosCMD[11] = {"dir", "md", "type", "sort", "del", "copy", "bg", "|", ">", ">>", "exit"}; 
	char *unixCMD[7] = {"ls", "mkdir", "cat", "sort", "rm", "cp", "&"};
	char *passedCMD[20];
	char input[100];

	signal(SIGCHLD, sigchldHandler);
       	while(exit == 0){
		background = 0;
		piping = 0;
		redirection1 = 0;
		redirection2 = 0;
        	printf("Galbraith Shell> ");
                gets(input);
		tkn = strtok(input, delimiter);
		while(tkn != NULL){
			while(i < 11 && found == 0){
				if(strcasecmp(tkn, dosCMD[i]) == 0){
					pos = i;		
					found = 1;
				}
				else
					i++;
				}
			switch(pos){
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				case 5: passedCMD[j] = unixCMD[pos]; j++; break;
				case 6: passedCMD[j] = NULL;
                                        backgroundPos = j;
                                        background = 1;
                                        j++;
                                        break;
				case 7: passedCMD[j] = NULL;
					pipingPos = j;
					piping = 1;
					j++;
					break;
				case 8: passedCMD[j] = NULL;
					redirectionPos = j;
					redirection1 = 1;
					j++;
					break;
				case 9: passedCMD[j] = NULL;
					redirectionPos = j;
					redirection2 = 1;
					j++;
					break;
				case 10: exit = 1;
				case 11: passedCMD[j] = tkn; j++; break;
				}	
			tkn = strtok(NULL, delimiter);
			found = 0;
			pos = 11;
			i = 0;
			}
			passedCMD[j] = NULL;
			j = 0;
			if(piping == 1){
				pipe(fdes);
				int prog2Start = pipingPos + 1;
				if(fork()==0){
					close(fdes[0]);
					dup2(fdes[1], 1);
					execvp(passedCMD[0], passedCMD);
					if((new_cid = fork()) == 0){
						close(fdes[1]);
						dup2(fdes[0], 0);
						execvp(passedCMD[prog2Start], passedCMD+prog2Start);
					}
				}
				close(fdes[0]);
				close(fdes[1]);
			}
			else{
				if((new_cid = fork()) == 0){
					if(redirection1 == 1){
						fd = open(passedCMD[redirectionPos + 1], O_WRONLY|O_CREAT|O_TRUNC, 0666);
						dup2(fd, 1);
					}
					if(redirection2 == 1){
						fd = open(passedCMD[redirectionPos + 1], O_WRONLY|O_CREAT|O_APPEND, 0666);
						dup2(fd, 1);
					}
				execvp(passedCMD[0], passedCMD);
				close(fd);
			}
			if(background == 0){
				fg_id = new_cid;
				while(fg_id);
			}
		}
 	}
}