SlideShare a Scribd company logo
1 of 5
// 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.pdfmohdjakirfb
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introductionraghukatagall2
 
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 .docxgordienaysmythe
 
Introduction to c
Introduction to cIntroduction to c
Introduction to camol_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.pdfmaheshkumar12354
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Max Kleiner
 
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.pdfalamodeindia1
 
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.pdfajay1317
 
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.docxadampcarr67227
 
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.docxDIPESH30
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6Techvilla
 
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.pptGowthamRaju15
 

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.docxhoggardbennie
 
(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.docxhoggardbennie
 
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.docxhoggardbennie
 
-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.docxhoggardbennie
 
-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.docxhoggardbennie
 
--- - 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.docxhoggardbennie
 
-f- Derive- A(BC).docx
-f- Derive- A(BC).docx-f- Derive- A(BC).docx
-f- Derive- A(BC).docxhoggardbennie
 
-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.docxhoggardbennie
 
- 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.docxhoggardbennie
 
- 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.docxhoggardbennie
 
(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-.docxhoggardbennie
 
- 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.docxhoggardbennie
 
- 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.docxhoggardbennie
 
(X-Y)-(1-1)limxyxy.docx
(X-Y)-(1-1)limxyxy.docx(X-Y)-(1-1)limxyxy.docx
(X-Y)-(1-1)limxyxy.docxhoggardbennie
 
(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.docxhoggardbennie
 
(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-.docxhoggardbennie
 
(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.docxhoggardbennie
 
(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-.docxhoggardbennie
 
#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.docxhoggardbennie
 

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

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

-- 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 }