Question

In: Computer Science

Writing your own shell in Linux with execvp Only code void cmdToArray() Program should be able...

Writing your own shell in Linux with execvp

Only code void cmdToArray()

Program should be able to execute ls -l.

#include <stdio.h>
#include <string,h>// strcmp
#include <stdlib.h>// exit
#include <unistd.h>// fork, exec
#include <sys/wait.h>// wait

#define MAXARGS 20
#define ARGLEN 256

void execute(char *cmd, char *arglist[]);
void cmdToArray(char *cmd, char *arglist);

int main(void)
{
int pid;
char *arglist[MAXARGS+1]; // array of pointers
char cmd[ARGLEN]; // read stuff here

while (1)
{
printf("cmd> ");
fgets(cmd, ARGLEN, stdin);

// remove newline from cmd
cmd[strlen(cmd) - 1] = '\0';

// exit if user types exit
if (strcmp(cmd, "exit") == 0)
{
exit(0);
}
else if (*cmd != '\0')
{
// TODO: break cmd into individual strings as elements of arglist
cmdToArray(cmd, arglist);
execute(arglist);
}
}

return 0;
}

void cmdToArray(char *cmd, char *arglist[]) //code here
{
// cmd = "*cp file1.txt file2.txt"
// char cmd[25] = ('c', 'p', ' ', 'f', ')
// arglist[0] = cmd;
// arglist[1] = &cmd[3];
// arglist[2] = &cmd[9];
// arglist[3] = "NULL";
// arglist = (**char)malloc(sizeof(char*) * cmd);
// arglist[0] = (char *)malloc(sizeof(char) *MAXARGS * string);
// int length = strlen(cmd);
/* for (int i = 0; i < length; ++i)
{
if (cmd[i] != ' ')
{
arglist[argc] = &cmd[i];
++argc;
}
else if(cmd[i] == ' ')
{
cmd[i] = '\0';
}
} */

}

void execute(char *arglist[])
{
int pid, exitstatus;

pid = fork(); // make new process
if (pid == -1)
{
perror("fork failed");
exit(1);
}
else if (pid == 0)
{
execvp(arglist[0], arglist);
// exec cannot return
fprintf(stderr, "Cannot execute %s\n", arglist[0]);
exit(1); // exec failed
}
else
{
int got_pid = wait(&exitstatus);
if (got_pid == pid)
{
printf("The child we were waiting for died\n");

if (WIFEXITED(exitstatus))
{
printf("child process %d exited with value %d\n", pid, WEXITSTATUS(exitstatus));
}
}
else if (got_pid == -1)
{
perror("wait failed");
}
}
}

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>// strcmp
#include <stdlib.h>// exit
#include <unistd.h>// fork, exec
#include <sys/wait.h>// wait

#define MAXARGS 20
#define ARGLEN 256

void execute(char *cmd, char *arglist[]);
void cmdToArray(char *cmd, char *arglist);

int main(void)
{
int pid;
char *arglist[MAXARGS+1]; // array of pointers
char cmd[ARGLEN]; // read stuff here

while (1)
{
printf("cmd> ");
fgets(cmd, ARGLEN, stdin);

// remove newline from cmd
cmd[strlen(cmd) - 1] = '\0';

// exit if user types exit
if (strcmp(cmd, "exit") == 0)
{
exit(0);
}
else if (*cmd != '\0')
{
// TODO: break cmd into individual strings as elements of arglist
cmdToArray(cmd, arglist);
execute(arglist);
}
}

return 0;
}

void cmdToArray(char *cmd, char *arglist[]) //code here
{
// cmd = "*cp file1.txt file2.txt"
// char cmd[25] = ('c', 'p', ' ', 'f', ')
// arglist[0] = cmd;
// arglist[1] = &cmd[3];
// arglist[2] = &cmd[9];
// arglist[3] = "NULL";
// arglist = (**char)malloc(sizeof(char*) * cmd);
// arglist[0] = (char *)malloc(sizeof(char) *MAXARGS * string);
// int length = strlen(cmd);
/* for (int i = 0; i < length; ++i)
{
if (cmd[i] != ' ')
{
arglist[argc] = &cmd[i];
++argc;
}
else if(cmd[i] == ' ')
{
cmd[i] = '\0';
}
} */

}

void execute(char *arglist[])
{
int pid, exitstatus;

pid = fork(); // make new process
if (pid == -1)
{
perror("fork failed");
exit(1);
}
else if (pid == 0)
{
execvp(arglist[0], arglist);
// exec cannot return
fprintf(stderr, "Cannot execute %s\n", arglist[0]);
exit(1); // exec failed
}
else
{
int got_pid = wait(&exitstatus);
if (got_pid == pid)
{
printf("The child we were waiting for died\n");

if (WIFEXITED(exitstatus))
{
printf("child process %d exited with value %d\n", pid, WEXITSTATUS(exitstatus));
}
}
else if (got_pid == -1)
{
perror("wait failed");
}
}
}


Related Solutions

Name your source code file sh.c Write your own simple shell. Your shell should prompt the...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the user for a command, run it, then prompt the user for their next command. If the user types "exit", then the shell should terminate. The shell should ignore Ctrl-C. Inside the attached parse.h header file, there is a parse function that you can use to split up the command line into separate strings. Recall that execvp accepts two arguments: the name of the command,...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
In this assignment you will write your own shell program, Mav shell (msh), similar to bourne...
In this assignment you will write your own shell program, Mav shell (msh), similar to bourne shell (bash), c-shell (csh), or korn shell (ksh). It will accept commands, fork a child process and execute those commands. The shell, like csh or bash, will run and accept commands until the user exits the shell. Your file must be named msh.c Functional Requirements Requirement 1: Your program will print out a prompt of msh> when it is ready to accept input. It...
Create a program named MergeSort.java then copy the following code to your program: public static void...
Create a program named MergeSort.java then copy the following code to your program: public static void mergeSort(int[] arr){    mergeSortRec(arr, 0, arr.length-1); } private static void mergeSortRec(int[] arr, int first, int last){    if(first<last){     int mid=(first+last)/2; mergeSortRec(arr, first, mid);     mergeSortRec(arr, mid+1,last);     merge(arr, first, mid, mid+1, last);    } } private static void merge(int[] arr, int leftFirst,    int leftLast, int rightFirst, int rightLast){    int[] aux=new int[arr.length];    //extra space, this is downside of this algorithm   ...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given only two integer numbers from command line arguments and computing their multiplication. If two integer numbers are not given, print “Wrong Input” on your screen. Note that, the number of arguments is known when the script runs. Take a screenshot showing your shell program and its execution step. Design a shell program to remove all the shell programming files ending with sh on your...
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
Write shell script code that displays the process tree on a Linux machine. The process tree...
Write shell script code that displays the process tree on a Linux machine. The process tree should show the process identifier of each process. Those processes that have the same ancestor should be sorted by process identifier instead of by name. The process tree should show the full names of all threads, if any and available. Whenever the user identifier associated with a process differs from the user identifier associated with the parent of that process, the process tree should...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT