Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <wait.h>
- #include <signal.h>
- #include <sys/types.h>
- #include "data.h"
- void childSignalHandler(int signum) {
- int status;
- pid_t pid;
- pid = waitpid(-1, &status, WNOHANG);
- }
- int main(int argc, char **argv) {
- char bBuffer[BUFSIZ], *pArgs[10], *aPtr = NULL, *sPtr;
- bool background;
- ssize_t rBytes;
- int aCount;
- pid_t pid;
- signal(SIGCHLD, childSignalHandler);
- while(1) {
- write(1, "\e[1;31mmyBash \e[1;32m# \e[0m", 27);
- rBytes = read(0, bBuffer, BUFSIZ-1);
- if(rBytes == -1) {
- perror("read");
- exit(1);
- }
- bBuffer[rBytes-1] = '\0';
- if(!strcasecmp(bBuffer, "exit")) {
- exit(0);
- }
- sPtr = bBuffer;
- aCount = 0;
- do {
- aPtr = strsep(&sPtr, " ");
- pArgs[aCount++] = aPtr;
- } while(aPtr);
- background = FALSE;
- if(!strcmp(pArgs[aCount-2], "&")) {
- pArgs[aCount-2] = NULL;
- background = TRUE;
- }
- if(strlen(pArgs[0]) > 1) {
- pid = fork();
- if(pid == -1) {
- perror("fork");
- exit(1);
- }
- if(pid == 0) {
- execvp(pArgs[0], pArgs);
- exit(0);
- }
- if(!background) {
- wait(NULL);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement