Question Write a C program, call it myBash, to act like Bash. Yourprogram should go in an infinite loop waiting for the user to entera line of commands (single or multiple). Once a line is entered,your program should assemble and execute each command and performpiping if needed. To make it simple, you can assume that piping islimited to two commands only. The commands/programs location can beanywhere in your $PATH and might have arguments. Examples of inputlines entered by the user : • myBash> ps -e • myBash> moreletter.txt | wc -c • myBash> ps -ef; date ; echo Good Day Inparticular, you should only use Unix I/O system

AKNRLQ The Asker · Computer Science

Write a C program, call it myBash, to act like Bash. Your program should go in an infinite loop waiting for the user to enter a line of commands (single or multiple). Once a line is entered, your program should assemble and execute each command and perform piping if needed. To make it simple, you can assume that piping is limited to two commands only. The commands/programs location can be anywhere in your $PATH and might have arguments. Examples of input lines entered by the user : • myBash> ps -e • myBash> more letter.txt | wc -c • myBash> ps -ef; date ; echo Good Day In particular, you should only use Unix I/O system

More
Community Answer
VIPZKF

Part A :-The C program hat will act as a shell command line interpreter for the Linux kernel.#include<stdio.h>#include<string.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>#include<readline/readline.h>#include<readline/history.h>#define MAXCOM 1000 // max number of letters to be supported#define MAXLIST 100 // max number of commands to be supported// Clearing the shell using escape sequences#define clear() printf("\033[H\033[J")// Greeting shell during startupvoid init_shell(){    clear();    printf("\n\n\n\n******************"        "************************");    printf("\n\n\n\t****MY SHELL****");    printf("\n\n\t-USE AT YOUR OWN RISK-");    printf("\n\n\n\n*******************"        "***********************");    char* username = getenv("USER");    printf("\n\n\nUSER is: @%s", username);    printf("\n");    sleep(1);    clear();}// Function to take inputint takeInput(char* str){    char* buf;    buf = readline("\n>>> ");    if (strlen(buf) != 0) {        add_history(buf);        strcpy(str, buf);        return 0;    } else {        return 1;    }}// Function to print Current Directory.void printDir(){    char cwd[1024];    getcwd(cwd, sizeof(cwd));    printf("\nDir: %s", cwd);}// Function where the system command is executedvoid execArgs(char** parsed){    // Forking a child    pid_t pid = fork();    if (pid == -1) {        printf("\nFailed forking child..");        return;    } else if (pid == 0) {        if (execvp(parsed[0], parsed) < 0) {            printf("\nCould not execute command..");        }        exit(0);    } else {        // waiting for child to terminate        wait(NULL);        return;    }}// Function where the piped system commands is executedvoid execArgsPiped(char** parsed, char** parsedpipe){    // 0 is read end, 1 is write end    int pipefd[2];    pid_t p1, p2;    if (pipe(pipefd) < 0) {        printf("\nPipe could not be initialized");        return;    }    p1 = fork();    if (p1 < 0) {        printf("\nCould not fork");        return;    }    if (p1 == 0) {        // Child 1 executing..        // It only needs to write at the write end        close(pipefd[0]);        dup2(pipefd[1], STDOUT_FILENO);        close(pipefd[1]);        if (execvp(parsed[0], parsed) < 0) {            printf("\nCould not execute command 1..");            exit(0);        }    } else {        // Parent executing        p2 = fork();        if (p2 < 0) {            printf("\nCould not fork");            return;        }        // Child 2 executing..        // It only needs to read at the read end        if (p2 == 0) {            close(pipefd[1]);            dup2(pipefd[0], STDIN_FILENO);            close(pipefd[0]);            if (execvp(parsedpipe[0], parsedpipe) < 0) {                printf("\nCould not execute command 2..");                exit(0);            }        } else {            // parent executing, waiting for two children            wait(NULL);            wait(NULL);        }    }}// Help command builtinvoid openHelp(){    puts("\n***WELCOME TO MY SHELL HELP***"        "\nCopyright @ Suprotik Dey"        "\n-Use the shell ... See the full answer