//Joe Puccio
//I certify that no unauthorized assistance has been received or given in the completion of this work -- Joe Puccio

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAX_NUMBER_OF_ARGS 20

int main() {
	int childPID;

	char* lineRead = NULL;
	size_t length = 0; //not sure what this is
	ssize_t sizeRead; //not sure what this is

	printf("%%");
	while((sizeRead = getline(&lineRead, &length, stdin)) != -1){
		
		//remove newline (last character)
		lineRead[strlen(lineRead)-1] = 0;

		childPID = fork();

		if(childPID == 0){

			//initialize argv. Can't change what the elements point to
			char *argv[MAX_NUMBER_OF_ARGS];
			int positionInArgv = 0;

			char* command = strtok(lineRead," 	"); //have a space and a tab in here, should delimit by either
			char* argument = strtok(NULL," 	");

			char pathToCommand[] = "/bin/";
			strcat(pathToCommand,command);
			argv[positionInArgv] = command;
			positionInArgv++;

			while(argument != NULL){
				argv[positionInArgv] = argument; 
				positionInArgv++;
				argument = strtok(NULL, " 	");

				if(positionInArgv>MAX_NUMBER_OF_ARGS-1){
					printf("Too many args, dude.");
				}

			}
			//null terminate the list of arguments
			argv[positionInArgv]=NULL;

			execvp(argv[0], argv);
			printf("failure to execute\n");
    
			//code for child process, so in here we should look at the passed arguments and 
			//exec the appropriate file 
		}else{
			//code for parent process, shouldn't do anything except wait?
			wait();
		}

		printf("%%");

	}
}
