DONT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER WILL BE REPORTED IF NOT A UNIQUE SOLUTION.
Write a C program called myshell.c, which, when compiled and run, will do what the shell does, namely, it executes in a loop (until user types exit on the keyboard),
prints a prompt on the screen, reads the command typed on the keyboard (terminated by \n),
creates a new process and lets the child execute the user’s command,
waits for the child to terminate and then goes back to beginning of the loop.
If the command typed is exit, then your program should terminate.
Print the total number of commands executed just before terminating your program.
Assume that each line represents one command only, no command will end with & (all commands will be attached commands, no background execution),
user will not type ^c or ^z, all commands are simple commands, etc.
I developed this code last year in operating systems lab it works perfectly for most of the commands if there is any issue just comment once I will look into it. Also, I have tried to explain the code hope it helps
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64
#define STDOUT 1
#define STDIN 0
#define STDERR 2
char **tokenize(char *line)
{
char **tokens = (char **)malloc(MAX_NUM_TOKENS * sizeof(char
*));
char *token = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
int i, tokenIndex = 0, tokenNo = 0;
for(i =0; i < strlen(line); i++){
char readChar = line[i];
if (readChar == ' ' || readChar == '\n' || readChar == '\t')
{
token[tokenIndex] = '\0';
if (tokenIndex != 0){
tokens[tokenNo] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(tokens[tokenNo++], token);
tokenIndex = 0;
}
}
else {
token[tokenIndex++] = readChar;
}
}
free(token);
tokens[tokenNo] = NULL ;
return tokens;
}
char cwd[1024];
int bgprocesses[1024], num_bgp;
void execute(char** args, int bg, int fd){
if(strcmp(args[0],"cd")==0){ //comparing the string with cd
if(args[1]==NULL || args[2]!=NULL){
fprintf(stderr, "cd: Wrong number of arguments\n");
//checking if you have passed only one command to cd if more of
less than one it gives error.
}
else{
chdir(args[1]); // changes the current working directory of the
calling process
}
}
else{
int pid = fork();//creation of a child process
if(pid==0){
//child process
if(fd!=STDOUT){
int copy = dup(STDOUT); //copy of the file descriptor
close(STDOUT);
dup(fd);
execvp(args[0], args); //execute the file
fprintf(stderr, "%s: No such file or directory\n", args[0]);
//printing the necessary print statements
}
else{
execvp(args[0], args);
fprintf(stderr, "%s: No such file or directory\n", args[0]);
}
}
else{
if(bg==0)
{
int status;
waitpid(pid, &status, 0); //waiting for child process to
end
}
else{
}
}
}
}
//shell
void Shell(){
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL); //Determines the path name
of the working directory and stores it in buffer
else{
perror("Failed to fetch current working directory\n");
exit(1);
}
}
void main(void)
{
char line[MAX_INPUT_SIZE];
char **tokens;
int i;
Shell();
while (1) {
printf("shell>");
bzero(line, MAX_INPUT_SIZE); //copies n bytes, each with a value of
zero, into string line.
gets(line); //reads a line from stdin and stores it
line[strlen(line)] = '\n'; //terminate with newline
tokens = tokenize(line);
char ** lastCommand = tokens;
int fd = STDOUT;
int isSemiColon[66];
for(int i=0; i<66; i++)
isSemiColon[i]=0;
for(int i=0; tokens[i]!=NULL; i++){
if(strcmp(tokens[i],";;")==0){ //comapring the all the tokens to
check if there are two semicolons
tokens[i] = NULL;
isSemiColon[i] = 1; // if there is double semi colon set it to
1.
}
}
for(i=0;;i++){
if(isSemiColon[i]){
execute(lastCommand, 0, fd);
if(tokens[i+1]==NULL){
break;
}
else{
lastCommand = &tokens[i+1];
}
}
else if(tokens[i]==NULL){
execute(lastCommand, 0, fd);
break;
}
else if(strcmp(tokens[i],">")==0){ //again comaping the tokens
to search for ">"
printf("> found\n"); //">" print found if enter this
loop
tokens[i] = NULL;
if(isSemiColon[i+1]){
fprintf(stderr, "bash: syntax error near unexpected token
`;;`");
}
else if(tokens[i+1]==NULL){
fprintf(stderr, "bash: syntax error near unexpected token
`newline`");
}
else{
fd = open(tokens[i+1], O_CREAT | O_WRONLY, S_IRUSR |
S_IWUSR);
if(fd>=0){
execute(tokens, 0, fd);
}
else{
fprintf(stderr, "%s: Is a directory\n", tokens[i+1]);
}
}
}
}
// Freeing the allocated memory
for(i=0;tokens[i]!=NULL;i++){
free(tokens[i]);
}
free(tokens);
}
}
DONT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER WILL...
DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER WILL BE REPORTED IF NOT A UNIQUE SOLUTION. Write a C program called myshell.c, which, when compiled and run, will do what the shell does, namely, it executes in a loop (until user types exit on the keyboard), prints a prompt on the screen, reads the command typed on the keyboard (terminated by \n), creates a new process and lets the child execute the...
Project 1: Implementing a Shell 1 Overview In this individual project you will have to design and implement a simple shell command interpreter called mysh. The basic function of a shell is to accept lines of text as input and execute programs in response. The shell must be able to execute built-in commands in a process different from the one executing mysh. 2 Requirements When first started, your shell should initialize any necessary data structures and then enter a loop...
This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell interface gives the user a prompt, after which the next command is entered. The example below illustrates the prompt cse222> and the user’s next command: cat prog.c. cse222> cat prog.c One technique for implementing a shell interface is to have the parent process first read what the user enters on the...
Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...
The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...
Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...
Edit the code (shell.c) given to do the tasks asked! will rate
for correct answer! Also, include a screen shot of the output and
terminal window of each command you used. Read carefully to do this
task please.
shell.c code given below.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
int main()
{
int PID;
char lineGot[256];
char *cmd;
while (1){
printf("cmd: ");
fgets(lineGot, 256, stdin); // Get a string from user (includes \n)
cmd = strtok(lineGot, "\n");...
Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...
2. capture This solution to this problem will require using some system calls that will be discussed in week 3. This program should accept the name of a file that includes a list of commands (one per line) to execute. Each line of the file should be executed by a subprocess and the output written to the file capture.txt For example, given the command file: /bin/ls-1 /bin/cat apple banana usr/bin/wc -1-wcanteloupe The output saved to capture.txt might be: W1 schubert...
Please do not copy other solution from the site as they never fully implement all the listed guidelines below. Please write a shell script called atm.bash similar to the ones used in ATM machines. Essentially your script is to handle a person's savings and checking accounts and should handle the following services: Transfer from savings account to checking account Transfer from checking account to savings account Cash withdrawal from either account Balance statements for both the...