Example #1
0
int main (void) {
	// sentinal
	int s = 1;	
	char buf[1024];
	struct cmd command;

	int fd[2], fdin, fdout,fdinflag = 0,fdoutflag = 0,ndx,mypid;
    	pid_t pid, pidBG;

	while(s) {
		// custom cmd line
 		printf("CMDLINE$-->");

		// wil get first line of input		 
		if((gets(buf) == NULL)){
			continue;
	        }
	        else if(strcmp(buf, "exit") == 0){
			s = 0;
            		continue;
        	}
		
		// will read into struct
		if (cmdscan(buf,&command)){
            		printf("Illegal Format: \n");
            		continue;
        	}
		
		// if there is for redirect_in set a flag
		if(command.redirect_in == 1){	
            		if((fdin = open(command.infile, O_RDONLY)) == -1){
                		perror("fdin");
                		exit(-1);
            		}
			fdinflag = 1;
        	}
		
		// if there is for redirect_out set a flag
        	if(command.redirect_out == 1){
			
        		if(command.redirect_append == 1){
                		if((fdout = open(command.outfile, O_WRONLY | O_APPEND | O_CREAT, 0600)) == -1){
                 			perror("open append");
                   			exit(-1);
               			}		
            		}
            		else
			{
               			if((fdout = open(command.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1){
                    			perror("open out");
                    			exit(-1);
                		}
            		}
			fdoutflag = 1;
        	}
		// if the user wants to pipe do here
		if (command.piping) {
			if ( command.background == 1){
           			pipe(fd);
           			ndx = 0;
           			switch( fork()){
               				case -1: 
                 	  			perror("fork error");
                   				exit(-1);
               				case 0:
                   				switch( fork()){
                       					case -1:
                           					perror("fork error");
                           					exit(-1);
                       					case 0:
                           					ndx = 1;
                           					switch( fork()){
                               						case -1:
                                   						perror("fork error");
                                   						exit(-1);
                               						case 0:
                                   						ndx = 2;
                                   						break;
                               						default:
                                   						break;
                           					}
                       					default:
                           					exit(0);
                   				}
               				default:
                   				wait(NULL);
           			}
		// the user does not want to background       		
		}else{  
           		pipe(fd);
           		
           		ndx = 0;

           		switch(fork()){
               			case -1:
                   			perror("fork error");
                   			exit(-1);
               			case 0:
                   			ndx = 1;
                   			mypid = getpid();
                   			switch(fork()){
                       				case -1:
                           				perror("fork error");
                           				exit(-1);
                       				case 0:
                           				ndx = 2;
                           				break;
                       				default:
                           				break;        
                   			}
               			default:
                   			break;        
           		}
       	 	}   
       		if (ndx == 0){
           		close(fd[0]);
           		close(fd[1]);
			// need to close them in the parent process!!!! why my program kept writing to my out program all the time 
	   		if (fdinflag) {
	   			close(fdin);
	   		} 
           		if (fdoutflag) {
	   			close(fdout);
	   		}
           		waitpid(mypid, NULL, 0);
       		}
       		if (ndx == 1){
           		close(fd[1]);
           		if (fdoutflag){
               		dup2(fdout, STDOUT_FILENO);
               		close(fdout); 
           		}
           		dup2(fd[0], STDIN_FILENO);
           		close(fd[0]);
           		execvp(command.argv2[0], command.argv2);
           		perror("exec error");
           		exit(-1); 
       		}
       		if (ndx == 2){
         		close(fd[0]);
           		if (fdinflag){
	       			dup2(fdin, STDIN_FILENO);          
               			close(fdin);
           		}
           		dup2(fd[1], STDOUT_FILENO);
           		close(fd[1]);
           		execvp(command.argv1[0], command.argv1);
           		perror("exec error");
           		exit(-1); 
       		}
   		
	
		} else {
				switch(pid = fork()) {
					case -1:
						perror("fork error");
						exit(-1);
					case 0:
						if (command.background == 1) {	
							switch(fork()){
								case -1:
									perror("fork error");
									exit(-1);
								case 0:
									if (fdinflag) {
										dup2(fdin,STDIN_FILENO);
										close(fdin); 
									}
									if (fdoutflag) {
										dup2(fdout,STDOUT_FILENO);
										close(fdout);
									}
									execvp(command.argv1[0],command.argv1);
									perror("exec error");
									exit(-1);
								default:
									exit(0); 	
							}					
						}
						else{
							if (fdinflag) {
                                                		dup2(fdin,STDIN_FILENO);
                                                                close(fdin); 
                                                        }
                                                        if (fdoutflag) {
                                                                dup2(fdout,STDOUT_FILENO);
                                                                close(fdout);
                                                        }
                                                        execvp(command.argv1[0],command.argv1);
                                                        perror("exec error");
                                                        exit(-1); 
						}
					default:
						if (command.background == 0){
							waitpid(pid,NULL,0);
							// needed to close the fdins and fdouts because they were still linked to parent processes and therefor all my commands were all being sent to the outfile. 							
							if (fdinflag) {
                                                		close(fdin); 
                                                        }
                                                        if (fdoutflag) {
                                                                
                                                                close(fdout);
                                                        }
							
						}else {
							if (fdinflag) {
								close(fdin);
							}
							if (fdoutflag) {
								close(fdout);
							}						
						}	
				}
		}	
	}
	exit(0);
}
Example #2
0
int main() {
    
char buf[BUFSIZE];
pid_t pid, pid1, pid2;
int fd[2];

// get USER name and print
printf("--------- Welcome %s ---------\n",getenv("USER"));	
printf("[%s]: ", getenv("USER"));

// while stdin != Null run while loop
while((fgets(buf,BUFSIZE,stdin) != NULL)) {


	//if error from cmdscan.c print error and continue
	if(cmdscan(buf,&command) == -1) {
        	printf("ILLEGAL FORMAT\n");
		printf("[%s]: ", getenv("USER"));
		continue;
	}//end if error from cmdscan.c
	
	//if user types exit terminate shell
	if(strcmp(command.argv1[0],"exit") == 0) {
		exit(0);
        }//end if exit 
	
	// create first child process
    	if((pid = fork()) < 0){
       		perror("FORK ERROR\n");
	}//end if fork error check
	// if child process
    	else if (pid == 0) {
		
		//if backgrounding = true create another child process pid2
		if (command.background){
			pid2 = fork(); 
			if(pid2 < 0){
                		perror("FORK ERROR\n");
			}//end if pid2 error
		}//end if backgrounding
		
		//if backgrounding and parent of child pid2 exit 
		//pid2 is now adopted by init
		if(command.background && (pid2 != 0)){
			exit(0);
		}//end if backgrounding and pid2 !=0
		
		// if not backgrounding or backgrounding and child pid2
		if(!command.background || (command.background && pid2 == 0)){
		
			//if piping
			if(command.piping){
				pipe(fd);
				pid1 = fork();	
        			
				//if pid1 is the parent process
				if (pid1 > 0) {

					redirectCheck();
					
					close(fd[1]);
					dup2(fd[0], STDIN_FILENO);
      					execvp(command.argv2[0],command.argv2);

				}//end if pid1 is parent process
				
				// if pid1 is the child process
				else if( pid1 == 0){

					redirectCheck();
       		
					close(fd[0]);
       					dup2(fd[1],STDOUT_FILENO);
					execvp(command.argv1[0],command.argv1);
				}//end else if child process
			}//end if piping
			
			//if !piping
			if(!command.piping){
	
				redirectCheck();

				// error check
                		if(execvp(command.argv1[0],command.argv1) == -1){
					printf("NOT A VALID USAGE\n");
					exit(1);
				}//end if error check
		
			//execute arguments
			execvp(command.argv1[0],command.argv1);
			}//end if !piping
        	exit(0);
		}//end if not backgrounding or backgrounding and child pid2
    }//end else if child 
	else {
        if(wait(&status) != pid)
            	perror("WAIT ERROR");
		printf("[%s]: ", getenv("USER")); 
    }//end else
}//end while loop

    return 0;

}//end main
Example #3
0
int main(){
	int stop = 0;
	char buf[1024];
	struct cmd command;
	int i;
	int fd[2], fdin, fdout;
	pid_t pid, pidBG;

	while(!stop){ 
		write(STDOUT_FILENO, ">", 1);
		if((gets(buf) == NULL)){
			continue;
		}
		else if(strcmp(buf, "exit") == 0){
			stop = 1;
			continue;
		}
	    	if (cmdscan(buf,&command)){
	    		printf("Illegal Format: \n");
			continue;
	    	}
		if(command.redirect_in){
			if((fdin = open(command.infile, O_RDONLY)) == -1){
				perror("fdin");
				exit(-1);
			}
		}
		if(command.redirect_out){
			if(command.redirect_append){
				if((fdout = open(command.outfile, O_WRONLY | O_APPEND | O_CREAT, 0600)) == -1){
					perror("open append");
					exit(-1);
				}
			}
			else{
				if((fdout = open(command.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1){
					perror("open out");
					exit(-1);
				}
			}
		}
		switch(pid = fork()){
			case -1:
				perror("fork");
				exit(-1);
			case 0:
				if(pipe(fd) == -1){
					perror("pipe");
					exit(-1);
				}
				switch(pidBG = fork()){
					case -1:
						perror("fork2");
						exit(-1);
					case 0:
						close(fd[0]);
						if(command.redirect_in){
							close(0);
							dup(fdin);
							close(fdin);
						}
						if(command.redirect_out && !command.piping){
							close(1);
							dup(fdout);
							close(fdout);
						}
						else if(command.piping){
							dup2(fd[1], STDOUT_FILENO);
						}
						close(fd[1]);
						execvp(command.argv1[0], command.argv1);
						perror("execvp arg1");
						exit(-1);
						break;
					default:
						if(!command.piping){
							exit(0);
						}
						else{
							dup2(fd[0], STDIN_FILENO);
							close(fd[0]);
							if(command.redirect_out){
								close(1);
								dup(fdout);
								close(fdout);
							}
							close(fd[1]);
							execvp(command.argv2[0], command.argv2);
							perror("execvp arg2");
							exit(-1);
						}
				}
			default:
				if(!command.background){
					if(waitpid(pid, NULL, 0) != pid){
						perror("waitpid error");
						exit(-1);
					}
				}
		}
	}
  exit(0);
}