SlideShare a Scribd company logo
// C Program
How might I modify this code to handle any number of commands piped together? such as ls |
wc or ls | wc -l | ps -elf etc..
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
// keeps track of cmd parameters
typedef struct param {
char *param;
struct param *next;
} pt;
// parses individual commands
typedef struct cmd_list {
char *cmd;
int param_count;
pt *param_list;
char * input_file_name;
char *output_file_name;
struct cmd_list *next;
} cl;
// keeps track of main commands that are piped
typedef struct list {
cl *head;
cl *tail;
int count;
} pt;
void execommands(clt *cmds) {
clt *cmd = cmds;
for (int i = 1; i < cmds->count-1; i++) {
int pipes[2] = {-1, -1};
pid_t pid = -1;
clt *temp = NULL;
pipe(pipes);
pid = fork();
switch (pid) {
case -1:
perror("fork failed");
break;
case 0:
{
char **rhp_argv = NULL;
char *rhp = NULL;
pt *temp = NULL;
int i = 1;
int flag_count = 0;
char flag_string[100] = "";
temp = cmd->param_list;
if (dup2(pipes[STDIN_FILENO], STDIN_FILENO) < 0) {
perror("child process failed dup2");
_exit(EXIT_FAILURE);
}
close(pipes[STDIN_FILENO]);
close(pipes[STDOUT_FILENO]);
rhp = cmd->cmd;
if(0 != cmd->param_count) {
rhp_argv = (char **) calloc((cmd->param_count), sizeof(char *));
rhp_argv[0] = rhp;
while(temp != NULL) {
if (flag_count > 0 && temp->param[0] == '-')
memmove(temp->param, temp->param + 1, strlen(temp->param));
}
strcat(flag_string, temp->param)
flag_count++;
temp = temp->next
}
rhp_argv[i] = strdup(flag_string);
i++;
}
else {
rhp_argv = (char **) calloc(2, sizeof(char *));
rhp_argv[0] = rhp;
}
execvp(rhp_argv[0], rhp_argv);
perror("child cannot exec program");
_exit(EXIT_FAILURE);
if(cmd->param_count != 0) {
for(int celm = 0; celm < flag_count; i++) {
free(rhp_argv[i]);
rhp_argv[i] = NULL;
}
}
free(rhp_argv);
rhp_argv = NULL;
free(temp);
}
break;
default:
int status;
waitpid(pid, &status, 0);
break;
} // end switch
} // end for
}

More Related Content

Similar to -- C Program How might I modify this code to handle any number of comm.docx

Help Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfHelp Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdf
mohdjakirfb
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
raghukatagall2
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
gordienaysmythe
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
amol_chavan
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdf
maheshkumar12354
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Max Kleiner
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
Ahmed Mekkawy
 
Components of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdfComponents of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdf
alamodeindia1
 
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
ajay1317
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
adampcarr67227
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
DIPESH30
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6
Techvilla
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdpPradeep Kumar TS
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
GowthamRaju15
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
TECHWORLDwithphotoed
 
Lập trình C
Lập trình CLập trình C
Lập trình C
Viet NguyenHoang
 

Similar to -- C Program How might I modify this code to handle any number of comm.docx (20)

Help Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfHelp Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdf
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdf
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Components of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdfComponents of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdf
 
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
3D-DRESD Lorenzo Pavesi
3D-DRESD Lorenzo Pavesi3D-DRESD Lorenzo Pavesi
3D-DRESD Lorenzo Pavesi
 

More from hoggardbennie

( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx
( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx
( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx
hoggardbennie
 
(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx
(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx
(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx
hoggardbennie
 
1 52 - 14 () - 11 If the interest rate is 10- per year compounde.docx
1  52 - 14  () -    11  If the interest rate is 10- per year compounde.docx1  52 - 14  () -    11  If the interest rate is 10- per year compounde.docx
1 52 - 14 () - 11 If the interest rate is 10- per year compounde.docx
hoggardbennie
 
-Mosquitoes appear to use odors to help them distinguish what is nearb.docx
-Mosquitoes appear to use odors to help them distinguish what is nearb.docx-Mosquitoes appear to use odors to help them distinguish what is nearb.docx
-Mosquitoes appear to use odors to help them distinguish what is nearb.docx
hoggardbennie
 
-template- (I assume that all of the text below goes in the body of th.docx
-template- (I assume that all of the text below goes in the body of th.docx-template- (I assume that all of the text below goes in the body of th.docx
-template- (I assume that all of the text below goes in the body of th.docx
hoggardbennie
 
--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx
--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx
--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx
hoggardbennie
 
-f- Derive- A(BC).docx
-f- Derive- A(BC).docx-f- Derive- A(BC).docx
-f- Derive- A(BC).docx
hoggardbennie
 
-Emotions are now often seen as central to certain organisational role.docx
-Emotions are now often seen as central to certain organisational role.docx-Emotions are now often seen as central to certain organisational role.docx
-Emotions are now often seen as central to certain organisational role.docx
hoggardbennie
 
- What is osmosis and the three types of osmosis- Explain- - Which typ.docx
- What is osmosis and the three types of osmosis- Explain- - Which typ.docx- What is osmosis and the three types of osmosis- Explain- - Which typ.docx
- What is osmosis and the three types of osmosis- Explain- - Which typ.docx
hoggardbennie
 
- Toss a coin three times- Let X- Heads - Tails- Find the distributio.docx
- Toss a coin three times- Let X- Heads -  Tails- Find the distributio.docx- Toss a coin three times- Let X- Heads -  Tails- Find the distributio.docx
- Toss a coin three times- Let X- Heads - Tails- Find the distributio.docx
hoggardbennie
 
(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx
(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx
(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx
hoggardbennie
 
- Let X be a random variable has a normal distribution with -30 and -1.docx
- Let X be a random variable has a normal distribution with -30 and -1.docx- Let X be a random variable has a normal distribution with -30 and -1.docx
- Let X be a random variable has a normal distribution with -30 and -1.docx
hoggardbennie
 
- A patient with emphysema is admitted to your unit- The patient is s.docx
-  A patient with emphysema is admitted to your unit- The patient is s.docx-  A patient with emphysema is admitted to your unit- The patient is s.docx
- A patient with emphysema is admitted to your unit- The patient is s.docx
hoggardbennie
 
(X-Y)-(1-1)limxyxy.docx
(X-Y)-(1-1)limxyxy.docx(X-Y)-(1-1)limxyxy.docx
(X-Y)-(1-1)limxyxy.docx
hoggardbennie
 
(shady) conditions will most likely have a-fewer chloroplasts than sun.docx
(shady) conditions will most likely have a-fewer chloroplasts than sun.docx(shady) conditions will most likely have a-fewer chloroplasts than sun.docx
(shady) conditions will most likely have a-fewer chloroplasts than sun.docx
hoggardbennie
 
(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx
(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx
(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx
hoggardbennie
 
(ABC)B.docx
(ABC)B.docx(ABC)B.docx
(ABC)B.docx
hoggardbennie
 
(a) Are these data categorical or quantitative- categorical quantitati.docx
(a) Are these data categorical or quantitative- categorical quantitati.docx(a) Are these data categorical or quantitative- categorical quantitati.docx
(a) Are these data categorical or quantitative- categorical quantitati.docx
hoggardbennie
 
(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx
(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx
(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx
hoggardbennie
 
#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx
#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx
#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx
hoggardbennie
 

More from hoggardbennie (20)

( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx
( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx
( 1 point) Generate two random numbers between 0 and 1 and take X to b.docx
 
(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx
(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx
(4) Ni is and D indebendent everts- Yes- bocine T(p)-F(0M) No- beause.docx
 
1 52 - 14 () - 11 If the interest rate is 10- per year compounde.docx
1  52 - 14  () -    11  If the interest rate is 10- per year compounde.docx1  52 - 14  () -    11  If the interest rate is 10- per year compounde.docx
1 52 - 14 () - 11 If the interest rate is 10- per year compounde.docx
 
-Mosquitoes appear to use odors to help them distinguish what is nearb.docx
-Mosquitoes appear to use odors to help them distinguish what is nearb.docx-Mosquitoes appear to use odors to help them distinguish what is nearb.docx
-Mosquitoes appear to use odors to help them distinguish what is nearb.docx
 
-template- (I assume that all of the text below goes in the body of th.docx
-template- (I assume that all of the text below goes in the body of th.docx-template- (I assume that all of the text below goes in the body of th.docx
-template- (I assume that all of the text below goes in the body of th.docx
 
--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx
--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx
--- - Class to demonstrate Generic Pair class -author john1819 - -- pu.docx
 
-f- Derive- A(BC).docx
-f- Derive- A(BC).docx-f- Derive- A(BC).docx
-f- Derive- A(BC).docx
 
-Emotions are now often seen as central to certain organisational role.docx
-Emotions are now often seen as central to certain organisational role.docx-Emotions are now often seen as central to certain organisational role.docx
-Emotions are now often seen as central to certain organisational role.docx
 
- What is osmosis and the three types of osmosis- Explain- - Which typ.docx
- What is osmosis and the three types of osmosis- Explain- - Which typ.docx- What is osmosis and the three types of osmosis- Explain- - Which typ.docx
- What is osmosis and the three types of osmosis- Explain- - Which typ.docx
 
- Toss a coin three times- Let X- Heads - Tails- Find the distributio.docx
- Toss a coin three times- Let X- Heads -  Tails- Find the distributio.docx- Toss a coin three times- Let X- Heads -  Tails- Find the distributio.docx
- Toss a coin three times- Let X- Heads - Tails- Find the distributio.docx
 
(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx
(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx
(1) In the tubular flowers of foxgloves- wild-type coloration is red-.docx
 
- Let X be a random variable has a normal distribution with -30 and -1.docx
- Let X be a random variable has a normal distribution with -30 and -1.docx- Let X be a random variable has a normal distribution with -30 and -1.docx
- Let X be a random variable has a normal distribution with -30 and -1.docx
 
- A patient with emphysema is admitted to your unit- The patient is s.docx
-  A patient with emphysema is admitted to your unit- The patient is s.docx-  A patient with emphysema is admitted to your unit- The patient is s.docx
- A patient with emphysema is admitted to your unit- The patient is s.docx
 
(X-Y)-(1-1)limxyxy.docx
(X-Y)-(1-1)limxyxy.docx(X-Y)-(1-1)limxyxy.docx
(X-Y)-(1-1)limxyxy.docx
 
(shady) conditions will most likely have a-fewer chloroplasts than sun.docx
(shady) conditions will most likely have a-fewer chloroplasts than sun.docx(shady) conditions will most likely have a-fewer chloroplasts than sun.docx
(shady) conditions will most likely have a-fewer chloroplasts than sun.docx
 
(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx
(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx
(i) Werck the icon to vere the trpssictons) More info Requirements 1-.docx
 
(ABC)B.docx
(ABC)B.docx(ABC)B.docx
(ABC)B.docx
 
(a) Are these data categorical or quantitative- categorical quantitati.docx
(a) Are these data categorical or quantitative- categorical quantitati.docx(a) Are these data categorical or quantitative- categorical quantitati.docx
(a) Are these data categorical or quantitative- categorical quantitati.docx
 
(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx
(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx
(a) The vector xRN has L different entries- For example- L-4 for x--3-.docx
 
#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx
#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx
#include -iostream- #include -vector- #include -Date-h- #include -Pers.docx
 

Recently uploaded

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
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
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
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
 
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
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
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
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
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
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
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
 
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)

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
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
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
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
 
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
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
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.
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
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
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
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
 
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
 

-- C Program How might I modify this code to handle any number of comm.docx

  • 1. // C Program How might I modify this code to handle any number of commands piped together? such as ls | wc or ls | wc -l | ps -elf etc.. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> // keeps track of cmd parameters typedef struct param { char *param; struct param *next; } pt; // parses individual commands typedef struct cmd_list { char *cmd; int param_count; pt *param_list; char * input_file_name; char *output_file_name; struct cmd_list *next; } cl; // keeps track of main commands that are piped
  • 2. typedef struct list { cl *head; cl *tail; int count; } pt; void execommands(clt *cmds) { clt *cmd = cmds; for (int i = 1; i < cmds->count-1; i++) { int pipes[2] = {-1, -1}; pid_t pid = -1; clt *temp = NULL; pipe(pipes); pid = fork(); switch (pid) { case -1: perror("fork failed"); break; case 0: { char **rhp_argv = NULL; char *rhp = NULL; pt *temp = NULL; int i = 1;
  • 3. int flag_count = 0; char flag_string[100] = ""; temp = cmd->param_list; if (dup2(pipes[STDIN_FILENO], STDIN_FILENO) < 0) { perror("child process failed dup2"); _exit(EXIT_FAILURE); } close(pipes[STDIN_FILENO]); close(pipes[STDOUT_FILENO]); rhp = cmd->cmd; if(0 != cmd->param_count) { rhp_argv = (char **) calloc((cmd->param_count), sizeof(char *)); rhp_argv[0] = rhp; while(temp != NULL) { if (flag_count > 0 && temp->param[0] == '-') memmove(temp->param, temp->param + 1, strlen(temp->param)); } strcat(flag_string, temp->param) flag_count++; temp = temp->next } rhp_argv[i] = strdup(flag_string);
  • 4. i++; } else { rhp_argv = (char **) calloc(2, sizeof(char *)); rhp_argv[0] = rhp; } execvp(rhp_argv[0], rhp_argv); perror("child cannot exec program"); _exit(EXIT_FAILURE); if(cmd->param_count != 0) { for(int celm = 0; celm < flag_count; i++) { free(rhp_argv[i]); rhp_argv[i] = NULL; } } free(rhp_argv); rhp_argv = NULL; free(temp); } break; default: int status; waitpid(pid, &status, 0);
  • 5. break; } // end switch } // end for }