1 of 8 CSCE 3600: Systems Programming Major Assignment 2 – The Shell and System Calls Due: 11:59 PM on Wednesday, April 7, 2021 COLLABORATION You should complete this assignment as a group assignment...

1 answer below »
Need to write exit function for no.2 in individual portion ad pipelining function


1 of 8 CSCE 3600: Systems Programming Major Assignment 2 – The Shell and System Calls Due: 11:59 PM on Wednesday, April 7, 2021 COLLABORATION You should complete this assignment as a group assignment with the other members of your group as assigned on Canvas using our GitLab environment (i.e., the same group you had for Major 1). If desired, you may submit only one program per group, but all source code must be committed in GitLab. Also, make sure that you list the names of all group members who participated in this assignment in order for each to get credit. BACKGROUND A shell provides a command-line interface for users. It interprets user commands and executes them. Some shells provide simple scripting terms, such as if or while, and allow users to make a program that facilitates their computing environment. Under the hood, a shell is just another user program. The file /bin/bash is an executable file for the bash shell. The only thing special about your login shell is that it is listed in your login record so that /bin/login (i.e., the program that prompts you for your password) knows what program to start when you log in. If you run "cat /etc/passwd", you will see the login records of the machine. PROGRAM DESCRIPTION GROUP COLLABORATIVE PORTION In this assignment, you will implement the shell “engine” as the “group” component, where all members are responsible for the following functionality: • A Command-Line Interpreter, or Shell Your shell should read the line from standard input (i.e., interactive mode) or a file (i.e., batch mode), parse the line with command and arguments, execute the command with arguments, and then prompt for more input (i.e., the shell prompt) when it has finished. 1. Interactive Mode In interactive mode, you will display a prompt (any string of your choosing) and the user of the shell will type in a command at the prompt. 2. Batch Mode In batch mode, your shell is started by specifying a batch file on its command line. The batch file contains the list of commands that should be executed. In batch mode, you should not display a prompt, but you should echo each line you read from the batch file back to the user before executing it. You will need to use the fork() and exec() family of system calls. You may 2 of 8 not use the system() system call as it simply invokes the system’s /bin/bash shell to do all of the work. You may assume that arguments are separated by whitespace. You do not have to deal with special characters such as ', ", \, etc. However, you will need to handle the redirection operators (< and="">) and the pipeline operator (|), which will be specified in the “individual” portion of this assignment. Each line (either in the batch file or typed at the prompt) may contain multiple commands separate with the semicolon (;) character. Each command separated by a ; should be run sequentially, but the shell should not print the next prompt or take more input until all of these commands have finished executing (the wait() or waitpid() system calls may be useful here). You may assume that the command-line a user types is not longer than 512 bytes (including the '\n'), but you should not assume that there is any restriction on the number of arguments to a given command. INDIVIDUAL PORTION In this assignment, each member of the group will implement the following components as defined below. This means that the individual group member responsible for each portion MUST commit in GitLab the code that supports their responsible area. • Built-In Commands Every shell needs to support a number of built-in commands, which are functions in the shell itself, not external programs. Shells directly make system calls to execute built-in commands, instead of forking a child process to handle them. Each group member is expected to implement 1 of the following built-in commands. Note that the expectation for this assignment assumes that a group contains 4 students, but if, for some reason, a team has only 3 students, then only 3 of the following built-in commands would need to be supported (i.e., 1 for each group member). 1. Add a new built-in cd command that accepts one optional argument, a directory path, and changes the current working directory to that directory. If no argument is passed, the command will change the current working directory to the user’s HOME directory. You may need to invoke the chdir() system call. 2. Add a new built-in exit command that exits from the shell itself with the exit() system call. It is not to be executed like other programs the user types in. If the exit command is on the same line with other commands, you should ensure that the other commands execute (and finish) before you exit your shell. These are all valid examples for quitting the shell: prompt> exit 3 of 8 prompt> exit; cat file1 prompt> cat file1; exit 3. Add a new built-in path command that allows users to show the current pathname list, append one pathname, or remove one pathname. In your shell implementation, you may keep a data structure to deal with the pathname list. If you do not use execle() or execve() that allows you to execute with your own environment variables, you will need to add it to the “real” PATH environment variable for executables in the path to work correctly. The initial value of path within your shell shall be the pathname list contained in the PATH environment variable. Implement the path command as follows: • path (without arguments) displays the pathnames currently set. It should show pathnames separated by colons. For example, "/bin:/user/bin". • path + ./bin appends the pathname to the path variable. You may assume that only one pathname is added at a time. • path - ./bin removes the pathname to the path variable. You may assume that only one pathname is removed at a time. You may assume that there are no duplicate pathnames present, being added, or being deleted. You will restore your PATH environment variable to its original state (i.e., before your shell was invoked) when the user exits your shell. 4. Add a new built-in myhistory command that lists the shell history of previous commands run in your shell (not the bash shell). Note that this does not have to work with the up-arrow key as in bash, but only with a new myhistory command run inside your shell. You may not make use of the history built-in command, but instead keep track of your history of commands in some sort of data structure. Your myhistory built-in command should support a history of 20 most recent commands (i.e., this means that the 21st command will overwrite the 1st command, for example). Your myhistory command should support a couple flags: -c to clear your myhistory list by deleting all entries, and -e to execute one of your twenty myhistory commands in your list. • Redirection, Pipelining, Signal Control, and Alias Support: 1. Extend your shell with I/O redirection (mandatory for teams of 3 or 4) When you start a command, there are always three default file streams open: stdin (maps input from the keyboard by default), stdout (maps output to the terminal by default), and stderr (maps error messages to the terminal by default). These and other open files may be redirected, or mapped, to files or devices that users specify. Modify your shell so that it supports redirecting stdin and stdout to files. You do not need to support redirection for your shell built-in commands (i.e., 4 of 8 cd, exit, path, and myhistory). You do not need to support stderr redirection or appending to files (e.g., cmd3 >> out.txt). You may assume that there will always be spaces around the special characters < and="">. Be aware that the "< file"="" or="" "=""> file" are not passed as arguments to your shell program. Some redirection examples include: $ cmd1 < in.txt="" executes="" cmd1,="" using="" in.txt="" as="" the="" source="" of="" input,="" instead="" of="" the="" keyboard.="" $="" cmd2=""> out.txt executes cmd2 and places the output to file out.txt. You will need to understand Linux file descriptors and use the open(), close(), and dup()/dup2() family of system calls. This portion of the project should only require implementing single input redirection and single output redirection (not both redirection or working with pipes). 2. Extend your shell with pipelining (mandatory for teams of 3 or 4) The command $ cmd1 | cmd2 | cmd3 connects the standard output of cmd1 to the standard input of cmd2, and again connects the standard output of cmd2 to the standard input of cmd3 using the pipeline operator '|'. You will need to use the pipe() system call. Your shell should be able to handle up to three commands chained together with the pipeline operator (i.e., your shell should support up to two pipes pipelined together). This portion of the project should only require implementing support for a pipeline of 2 pipes/3 commands (no working with redirection). Your shell does not need to handle built-in commands implemented above (i.e., cd, exit, path, and myhistory) in pipeline. 3. Support Signal Handling and Terminal Control (mandatory for teams of 3 or 4) Many shells allow you to stop or pause processes with special keystrokes, such as Ctrl-C or Ctrl-Z, that work by sending signals to the shell’s subprocesses. If you try these keystrokes in your shell, the signals would be sent directly to the shell process itself. This is not what we want since, for example, attempting to Ctrl-Z a subprocess of your shell will also stop the shell itself. Instead, we want to have the signals affect only the subprocesses that our shell creates
Answered Same DayApr 17, 2021

Answer To: 1 of 8 CSCE 3600: Systems Programming Major Assignment 2 – The Shell and System Calls Due: 11:59 PM...

Swapnil answered on Apr 17 2021
144 Votes
80964/Code/1.1.c
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 300
int cd(char *pathGiven);
int hasThisPrefix(char const *, char const *);
int main()
{
    char str[BUF_SIZE];
    char *promptShown = "USER_NAME>";
    char *token;
    token = strtok (str," ");
    while(str != NULL)
    {
        bzero(str, BUF_SIZE);
        printf("%s",promptShown);
        fgets(str, BUF_SIZE, stdin);
        if(hasThisPrefix(str,"cd") == 0)
        {
            token = str
chr(str,' ');
            if(token)
            {
                char *tempTok = token + 1;
                token = tempTok;
                char *locationOfNewLine = strchr(token, '\n');
                if(locationOfNewLine)
                {
                    *locationOfNewLine = '\0';
                }
                cd(token);
            }
        }
    }
    return 0;
}
int hasThisPrefix(char const *p, char const *q)
{
    int i = 0;
    for(i = 0;q[i];i++)
    {
        if(p[i] != q[i])
        return -1;
    }
    return 0;
}
int cd(char *pth)
{
    char path[BUF_SIZE];
    strcpy(path,pth);
    char cwd[BUF_SIZE];
    if(pth[0] != '/')
    {
        getcwd(cwd,sizeof(cwd));
        strcat(cwd,"/");
        strcat(cwd,path);
        chdir(cwd);
    }
    else
    {
        chdir(pth);
    }
    return 0;
}
80964/Code/2.1.c
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXCOM 1000
#define MAXLIST 100
#define clear() printf("\033[H\033[J")
void 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();
}
int takeInput(char* str)
{
char* buf;
buf = readline("\n>>> ");
if (strlen(buf) != 0)
{
add_history(buf);
strcpy(str, buf);
return 0;
}
else
{
return 1;
}
}
void printDir()
{
char cwd[1024];
getcwd(cwd, sizeof(cwd));
printf("\nDir: %s", cwd);
}
void execArgs(char** parsed)
{
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
{
wait(NULL);
return;
}
}
void execArgsPiped(char** parsed, char** parsedpipe)
{
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)
{
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
{
p2 = fork();
if (p2 < 0)
{
printf("\nCould not fork");
return;
}
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
{
wait(NULL);
wait(NULL);
}
}
}
void openHelp()
{
puts("\n***WELCOME TO MY SHELL HELP***"
"\nCopyright @ XYZ"
"\n-Use the shell at your own risk..."
"\nList of Commands supported:"
"\n>cd"
"\n>ls"
"\n>exit"
"\n>all other general commands available in UNIX shell"
"\n>pipe handling"
"\n>improper space handling");
return;
}
int ownCmdHandler(char** parsed)
{
int NoOfOwnCmds = 4, i, switchOwnArg = 0;
char* ListOfOwnCmds[NoOfOwnCmds];
char* username;
ListOfOwnCmds[0] = "exit";
ListOfOwnCmds[1] = "cd";
ListOfOwnCmds[2] = "help";
ListOfOwnCmds[3] = "hello";
for (i = 0; i < NoOfOwnCmds; i++)
{
if (strcmp(parsed[0], ListOfOwnCmds[i]) == 0)
{
switchOwnArg = i + 1;
break;
}
}
switch (switchOwnArg)
{
     case 1:    printf("\nGoodbye\n");
         exit(0);
     case 2:    chdir(parsed[1]);
         return 1;
     case 3:    openHelp();
         return 1;
     case 4:    username = getenv("USER");
         printf("\nHello %s.\nMind that this is "
         "not a place to play around."
         "\nUse help to know more..\n",
         username);
         return 1;
     default:
         break;
}
return 0;
}
int parsePipe(char* str, char** strpiped)
{
int i;
for (i = 0; i < 2; i++)
{
strpiped[i] = strsep(&str, "|");
if...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here