int main(int argc, char *argv[], char *envp[]){ char c; int i, fd; char *tmp = (char *)malloc(sizeof(char) * 100); char *pathString = (char *)malloc(sizeof(char) * 256); char *cmd = (char *)malloc(sizeof(char) * 100); signal(SIGINT, handleSignal); copyEnvp(envp); getPath(myEnvParams, pathString); insertPath(pathString); if(fork() == 0){ execve("/usr/bin/clear", argv, myEnvParams); exit(1); } else{ wait(NULL); } // printPrompt(); /* uncomment this line and comment the next line to change the prompt to '<userName>@<hostName>@myShell:' */ printf("[myShell]: "); fflush(stdout); while(c != EOF){ c = getchar(); switch(c){ case '\n': if(tmp[0] == '\0'){ // printPrompt(); /* uncomment this line and comment the next line to change the prompt to '<userName>@<hostName>@myShell:' */ printf("[myShell]: "); } else{ fillArgv(tmp); strcpy(cmd, myArguments[0]); strcat(cmd, "\0"); if(index(cmd, '/') == NULL){ if(attachPath(cmd) == 0){ executeCommand(cmd); } else{ printf("%s: command not found\n", cmd); } } else{ if((fd = open(cmd, O_RDONLY)) > 0){ close(fd); executeCommand(cmd); } else{ printf("%s: command not found\n", cmd); } } freeMemory(); // printPrompt(); /* uncomment this line and comment the next line to change the prompt to '<userName>@<hostName>@myShell:' */ printf("[myShell]: "); memset(cmd, '\0', 100); } memset(tmp, '\0', 100); break; default: strncat(tmp, &c, 1); break; } } free(tmp); free(pathString); for(i = 0; myEnvParams[i] != NULL; i++) free(myEnvParams[i]); for(i = 0; i < 10; i++) free(searchPath[i]); printf("\n"); return 0; }
int main(int argc, char *argv[], char *envp[]) { char *line; int returnValue; int i, fd; char *pathString = (char *)malloc(sizeof(char) * 256); char *cmd = (char *)malloc(sizeof(char) * 256); bzero(pathString, 256); bzero(cmd, 256); /* Manage Signals */ signal(SIGINT, SIG_IGN); signal(SIGQUIT,SIG_IGN); // signal(SIGCHLD,SIG_IGN); signal(SIGKILL,SIG_IGN); signal(SIGTERM,SIG_IGN); signal(SIGINT, handleSignal); /* Extract the PATH= var from envp */ getPathString(envp, pathString); /* Insert each path to be searched into searchPath[][] */ insertPathStringToSearch(searchPath, pathString); /* Print about Slash */ clrScreen(argv, envp); printf("%s", aboutSlash); printf("PID=%d\n", getpid()); #if SLASH_DEBUG for(i=0; envp[i] != NULL; i++) printf("%s\n", envp[i]); #endif /* Bind our completer + History Feature */ initialize_readline(); while(1) { line = readline ("\x1b[96m<$lash/>\x1b[00m "); /* Print in Red! */ #if SLASH_DEBUG printf("Input: %s\n", line); #endif if(*line) { add_history (line); returnValue = execute_line (line); if(returnValue == CMD_EXIT) break; if(returnValue == CMD_UNKNOWN) { #if SLASH_DEBUG printf("CMD_UNKNOWN: %s\n", line); #endif /* Extract args to command into slashArgv[][] */ nullifyArgv(slashArgv); fillArgv(line, slashArgv); bzero(cmd, 256); strncpy(cmd, slashArgv[0], strlen(slashArgv[0])); strncat(cmd, "\0", 1); #if SLASH_DEBUG printf("arg[0]: %s\n", slashArgv[0]); printf("cmd: %s\n", cmd); #endif if(index(cmd, '/') == NULL) { if(attachPath(cmd, searchPath) == 0) { callFork(cmd, slashArgv, envp); } else printf("%s: command not found\n", cmd); } else { if((fd = open(cmd, O_RDONLY)) > 0) { close(fd); callFork(cmd, slashArgv, envp); } else printf("%s: command not found\n", cmd); } freeArgv(slashArgv); } } if(line != NULL) free(line); fflush(stdout); } free(cmd); free(pathString); for(i=0;i<50;i++) free(searchPath[i]); return 0; }