Discuss about the i-o redirection in c shell

Assignment Help Operating System
Reference no: EM132084608

I/O redirection in C shell

Please implement input / output redirection in the code posted below. Here are the specs:

Your shell will need to support file redirection. Use the same syntax as defined in the Bash shell: a single '>' implies that one needs to redirect the standard output of the program being started to the referenced file while a single '<' implies the same with standard input. The double '>>' implies that standard output will append to an existing file rather than create a new file (similar behavior is required for the '<<' operator and standard input).

You do not need to implement support for the Bash pipeline operator '|'.

Before calling exec to begin execution, the child process may have to close stdin (file desriptor 0) and/or stdout (file descriptor 0), open the corresponding file and use the dup2 system call to make it the appropriate file descriptor.

Don't forget to use the close system call to close the old file descriptor.

#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int change_dir(char* args[])
{
if (args[1] == NULL)
{
chdir(getenv("HOME"));
return 1;
}

else
{
if (chdir(args[1]) == -1)
{
cout << "No such directory" << args[1];
return -1;
}
}
return 0;
}

int helper_func(char* args[])
{
cout << "Builtin commands so far are; cd, and help.\n";

return 0;
}

int main()
{
while(true)
{
cout << "$ ";
char cmd[128];
cin.getline(cmd,128);

vector <char*> args;
char *line = strtok(cmd, " ");
char *tmp = line;
while (tmp != NULL)
{
args.push_back(tmp);
tmp = strtok(NULL, " ");
}

char** argv = new char*[args.size() + 1];
for (int i = 0; i < args.size(); i++)
argv[i] = args[i];

argv[args.size()] = NULL;


if (strcmp(cmd, "exit") == 0)
{
{
exit(0);
}
}

//else if (strcmp(args[0], "cd") == 0) change_dir(args);

if (strcmp(cmd, "cd") == 0)
{
change_dir(argv);
}

if (strcmp(cmd, "help") == 0)
{
helper_func(argv);
}

else
{
pid_t pid;
pid_t wpid;
int status;
pid = fork();
if (pid == 0)
{
if (execvp(args[0], argv) == -1)
{
perror("cmd");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
perror("cmd");
}
{
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
}
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_DFL);

return 0;
}

Reference no: EM132084608

Questions Cloud

Create three variables of string data type : Create three variables of String data type, called firstName, lastName, and fullName respectively; Assign your first name to the variable firstName;
Securing control of the spice islands : What European power ended up securing control of the Spice Islands (Indonesia)?
What are the key features of an oodbms : How does an object-oriented database management system (OODMS) compare to a relational database management system (RDBMS)?
What was the predominant sector of the growing manufacturing : What was the predominant sector of the growing manufacturing economy of the 16th century? Name two regional centers of this industry.
Discuss about the i-o redirection in c shell : Your shell will need to support file redirection. Use the same syntax as defined in the Bash shell: a single '>' implies that one needs to redirect.
One primary-one secondary source : Please Explain Reasoning & Use One Primary & One Secondary Source. In Chicago Style Format.
Prepare flexible budgets based on the assumptions : Prepare flexible budgets based on the assumptions of service levels at 49,000 hours, 53,000 hours, and 57,000 hours
Contribute to a vision of the physical : How did the traditions of NEOPLATONISM, RENAISSANCE, and HUMANISM contribute to a vision of the physical world that encouraged scientific investigation
Determine positive normalized floating point numbers : Determine (show your work) the smallest and largest positive normalized floating-point numbers (in decimal value) as well as the machine epsilon.

Reviews

Write a Review

Operating System Questions & Answers

  Discuss the benefits and limitations of the mvc

Identify one way in which MVC helps software designers create better code, and one way in which it deters them.

  What are the features that make them different

Why are there so many versions of OS? What are the features that make them different? Explain the pros and cons of each. How can the evolution of OS benefit an organization and cause harm to an organization?

  State the proper syntax needed to configure the routers

Given the physical topology below, state the proper syntax needed to configure the routers and the computers included in the topology.

  Compare open-source and closed-source mobile oss

Compare and contrast open-source and closed-source mobile operating systems (OSs), and explain the main reasons why you would consider utilizing one of these types of OSs over the other.

  What is the size of the physical main memory in words

What is the size of the physical (main) memory in words? What is the address of the following data words found in the cache above?

  What exactly are the static and dynamic chains

Do I have enough information to do this problem and What exactly are the static and dynamic Chains? I am not a programmer and I am trying to get this right.

  Why the lynx companys functional system has been successful

Identify five reasons why the Lynx Company's functional system has been successful (be sure to expand your discussion on each reason). Identify five disadvantages of functional systems and briefly explain each one. Be sure to cite all your sources

  Write a short report on page replacement algorithms

Select one of two possible short reports,  Subject 1. Page Replacement Algorithms and Block Caching,  Subject 2. Storing Blocks and Remembering Where You Stored Them

  Identify one reason indexes are useful

Describe what you would have to do if indexes weren't available in order to get the reason you initially stated and how the query optimizer generates and selects a query plan.

  Unix system calls and library functionsthe goal of this

unix system calls and library functionsthe goal of this homework is to become familiar with the environment in hoare

  What instructions would you give people in a group session

What instructions would you give people in a group session about how to get their computers to look standardized?2. What policy would you suggest for what to put on the screen so it does not offend anyone?

  Share your thoughts on transitioning to an EHR

Discuss the findings of the article and share your thoughts on transitioning to an EHR. Post your findings in a Word document

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd