SlideShare a Scribd company logo
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
#include
#include
#include
#include
#include
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. ";
return 0;
}
int main()
{
while(true)
{
cout << "$ ";
char cmd[128];
cin.getline(cmd,128);
vector 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;
}
Solution
#include
#include
#include
#include
#include
#include
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. ";
return 0;
}
int main()
{
while(true)
{
cout << "$ ";
char cmd[128];
cin.getline(cmd,128);
vector 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;
}

More Related Content

Similar to IO redirection in C shellPlease implement input output redirect.pdf

Usp
UspUsp
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
Fred Chien
 
Assignment no39
Assignment no39Assignment no39
Assignment no39Jay Patel
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
Os lab final
Os lab finalOs lab final
Os lab final
LakshmiSarvani6
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
Jen Yee Hong
 
srgoc
srgocsrgoc
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlHideaki Ohno
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
anandatalapatra
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 

Similar to IO redirection in C shellPlease implement input output redirect.pdf (20)

Usp
UspUsp
Usp
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Five
FiveFive
Five
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
Assignment no39
Assignment no39Assignment no39
Assignment no39
 
Unit 4
Unit 4Unit 4
Unit 4
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Os lab final
Os lab finalOs lab final
Os lab final
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
7 functions
7  functions7  functions
7 functions
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
 
srgoc
srgocsrgoc
srgoc
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 

More from forecastfashions

Identify the organ labelled 3.liverstomacht h e mesenteryjej.pdf
Identify the organ labelled 3.liverstomacht h e mesenteryjej.pdfIdentify the organ labelled 3.liverstomacht h e mesenteryjej.pdf
Identify the organ labelled 3.liverstomacht h e mesenteryjej.pdf
forecastfashions
 
Fahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdf
Fahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdfFahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdf
Fahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdf
forecastfashions
 
Explain the relationship between three aspects of science hypothesi.pdf
Explain the relationship between three aspects of science hypothesi.pdfExplain the relationship between three aspects of science hypothesi.pdf
Explain the relationship between three aspects of science hypothesi.pdf
forecastfashions
 
Did BP respond to the oil spill in a manner that was appropriate to .pdf
Did BP respond to the oil spill in a manner that was appropriate to .pdfDid BP respond to the oil spill in a manner that was appropriate to .pdf
Did BP respond to the oil spill in a manner that was appropriate to .pdf
forecastfashions
 
Consider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z. (a) Show t.pdf
Consider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z.  (a) Show t.pdfConsider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z.  (a) Show t.pdf
Consider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z. (a) Show t.pdf
forecastfashions
 
Assignment Description When dealing with a performance improvement .pdf
Assignment Description When dealing with a performance improvement .pdfAssignment Description When dealing with a performance improvement .pdf
Assignment Description When dealing with a performance improvement .pdf
forecastfashions
 
Answer ALL 2 QUESTIONS please.... You configured several time settin.pdf
Answer ALL 2 QUESTIONS please.... You configured several time settin.pdfAnswer ALL 2 QUESTIONS please.... You configured several time settin.pdf
Answer ALL 2 QUESTIONS please.... You configured several time settin.pdf
forecastfashions
 
A heritability of body weight of 0.4 means that body weight is deter.pdf
A heritability of body weight of 0.4 means that body weight is deter.pdfA heritability of body weight of 0.4 means that body weight is deter.pdf
A heritability of body weight of 0.4 means that body weight is deter.pdf
forecastfashions
 
After reading the Cultural Competency Module, reflect on your own co.pdf
After reading the Cultural Competency Module, reflect on your own co.pdfAfter reading the Cultural Competency Module, reflect on your own co.pdf
After reading the Cultural Competency Module, reflect on your own co.pdf
forecastfashions
 
A variable that consists of indivisible categories with no other sco.pdf
A variable that consists of indivisible categories with no other sco.pdfA variable that consists of indivisible categories with no other sco.pdf
A variable that consists of indivisible categories with no other sco.pdf
forecastfashions
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
forecastfashions
 
Write a short essay explaining the four primary groups of production.pdf
Write a short essay explaining the four primary groups of production.pdfWrite a short essay explaining the four primary groups of production.pdf
Write a short essay explaining the four primary groups of production.pdf
forecastfashions
 
Why didnt the government just arrange to have excess food commodit.pdf
Why didnt the government just arrange to have excess food commodit.pdfWhy didnt the government just arrange to have excess food commodit.pdf
Why didnt the government just arrange to have excess food commodit.pdf
forecastfashions
 
Where is Virtual Reality going, and where would it be 20 yearsS.pdf
Where is Virtual Reality going, and where would it be 20 yearsS.pdfWhere is Virtual Reality going, and where would it be 20 yearsS.pdf
Where is Virtual Reality going, and where would it be 20 yearsS.pdf
forecastfashions
 
What is the negative side to overusing technical communication tools.pdf
What is the negative side to overusing technical communication tools.pdfWhat is the negative side to overusing technical communication tools.pdf
What is the negative side to overusing technical communication tools.pdf
forecastfashions
 
What is the method of action of aldehydes They damage proteins .pdf
What is the method of action of aldehydes  They damage proteins .pdfWhat is the method of action of aldehydes  They damage proteins .pdf
What is the method of action of aldehydes They damage proteins .pdf
forecastfashions
 
What are rubisco and RuBP And what do they do Briefly list 4 simil.pdf
What are rubisco and RuBP And what do they do  Briefly list 4 simil.pdfWhat are rubisco and RuBP And what do they do  Briefly list 4 simil.pdf
What are rubisco and RuBP And what do they do Briefly list 4 simil.pdf
forecastfashions
 
Virtual Machines Discuss what are the new innovation today in this .pdf
Virtual Machines Discuss what are the new innovation today in this .pdfVirtual Machines Discuss what are the new innovation today in this .pdf
Virtual Machines Discuss what are the new innovation today in this .pdf
forecastfashions
 
Using Java, please write the program for the following prompt in the.pdf
Using Java, please write the program for the following prompt in the.pdfUsing Java, please write the program for the following prompt in the.pdf
Using Java, please write the program for the following prompt in the.pdf
forecastfashions
 
True or FalseBecause IPsec is located in the IP layer, when it is.pdf
True or FalseBecause IPsec is located in the IP layer, when it is.pdfTrue or FalseBecause IPsec is located in the IP layer, when it is.pdf
True or FalseBecause IPsec is located in the IP layer, when it is.pdf
forecastfashions
 

More from forecastfashions (20)

Identify the organ labelled 3.liverstomacht h e mesenteryjej.pdf
Identify the organ labelled 3.liverstomacht h e mesenteryjej.pdfIdentify the organ labelled 3.liverstomacht h e mesenteryjej.pdf
Identify the organ labelled 3.liverstomacht h e mesenteryjej.pdf
 
Fahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdf
Fahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdfFahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdf
Fahrenheit ToCelsiusValue.java Done Program name Fahrenheit To Celsiu.pdf
 
Explain the relationship between three aspects of science hypothesi.pdf
Explain the relationship between three aspects of science hypothesi.pdfExplain the relationship between three aspects of science hypothesi.pdf
Explain the relationship between three aspects of science hypothesi.pdf
 
Did BP respond to the oil spill in a manner that was appropriate to .pdf
Did BP respond to the oil spill in a manner that was appropriate to .pdfDid BP respond to the oil spill in a manner that was appropriate to .pdf
Did BP respond to the oil spill in a manner that was appropriate to .pdf
 
Consider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z. (a) Show t.pdf
Consider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z.  (a) Show t.pdfConsider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z.  (a) Show t.pdf
Consider the subset S = {0, 2, 4, 6, 8, 10, 12} of Z14Z. (a) Show t.pdf
 
Assignment Description When dealing with a performance improvement .pdf
Assignment Description When dealing with a performance improvement .pdfAssignment Description When dealing with a performance improvement .pdf
Assignment Description When dealing with a performance improvement .pdf
 
Answer ALL 2 QUESTIONS please.... You configured several time settin.pdf
Answer ALL 2 QUESTIONS please.... You configured several time settin.pdfAnswer ALL 2 QUESTIONS please.... You configured several time settin.pdf
Answer ALL 2 QUESTIONS please.... You configured several time settin.pdf
 
A heritability of body weight of 0.4 means that body weight is deter.pdf
A heritability of body weight of 0.4 means that body weight is deter.pdfA heritability of body weight of 0.4 means that body weight is deter.pdf
A heritability of body weight of 0.4 means that body weight is deter.pdf
 
After reading the Cultural Competency Module, reflect on your own co.pdf
After reading the Cultural Competency Module, reflect on your own co.pdfAfter reading the Cultural Competency Module, reflect on your own co.pdf
After reading the Cultural Competency Module, reflect on your own co.pdf
 
A variable that consists of indivisible categories with no other sco.pdf
A variable that consists of indivisible categories with no other sco.pdfA variable that consists of indivisible categories with no other sco.pdf
A variable that consists of indivisible categories with no other sco.pdf
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
 
Write a short essay explaining the four primary groups of production.pdf
Write a short essay explaining the four primary groups of production.pdfWrite a short essay explaining the four primary groups of production.pdf
Write a short essay explaining the four primary groups of production.pdf
 
Why didnt the government just arrange to have excess food commodit.pdf
Why didnt the government just arrange to have excess food commodit.pdfWhy didnt the government just arrange to have excess food commodit.pdf
Why didnt the government just arrange to have excess food commodit.pdf
 
Where is Virtual Reality going, and where would it be 20 yearsS.pdf
Where is Virtual Reality going, and where would it be 20 yearsS.pdfWhere is Virtual Reality going, and where would it be 20 yearsS.pdf
Where is Virtual Reality going, and where would it be 20 yearsS.pdf
 
What is the negative side to overusing technical communication tools.pdf
What is the negative side to overusing technical communication tools.pdfWhat is the negative side to overusing technical communication tools.pdf
What is the negative side to overusing technical communication tools.pdf
 
What is the method of action of aldehydes They damage proteins .pdf
What is the method of action of aldehydes  They damage proteins .pdfWhat is the method of action of aldehydes  They damage proteins .pdf
What is the method of action of aldehydes They damage proteins .pdf
 
What are rubisco and RuBP And what do they do Briefly list 4 simil.pdf
What are rubisco and RuBP And what do they do  Briefly list 4 simil.pdfWhat are rubisco and RuBP And what do they do  Briefly list 4 simil.pdf
What are rubisco and RuBP And what do they do Briefly list 4 simil.pdf
 
Virtual Machines Discuss what are the new innovation today in this .pdf
Virtual Machines Discuss what are the new innovation today in this .pdfVirtual Machines Discuss what are the new innovation today in this .pdf
Virtual Machines Discuss what are the new innovation today in this .pdf
 
Using Java, please write the program for the following prompt in the.pdf
Using Java, please write the program for the following prompt in the.pdfUsing Java, please write the program for the following prompt in the.pdf
Using Java, please write the program for the following prompt in the.pdf
 
True or FalseBecause IPsec is located in the IP layer, when it is.pdf
True or FalseBecause IPsec is located in the IP layer, when it is.pdfTrue or FalseBecause IPsec is located in the IP layer, when it is.pdf
True or FalseBecause IPsec is located in the IP layer, when it is.pdf
 

Recently uploaded

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 

Recently uploaded (20)

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 

IO redirection in C shellPlease implement input output redirect.pdf

  • 1. 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 #include #include #include #include #include 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; } }
  • 2. return 0; } int helper_func(char* args[]) { cout << "Builtin commands so far are; cd, and help. "; return 0; } int main() { while(true) { cout << "$ "; char cmd[128]; cin.getline(cmd,128); vector 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);
  • 3. } } //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);
  • 4. } while (!WIFEXITED(status) && !WIFSIGNALED(status)); } } } signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_DFL); return 0; } Solution #include #include #include #include #include #include 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;
  • 5. } int helper_func(char* args[]) { cout << "Builtin commands so far are; cd, and help. "; return 0; } int main() { while(true) { cout << "$ "; char cmd[128]; cin.getline(cmd,128); vector 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); }
  • 6. } //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));