From f63e82b5b6342cd68c6456edec1f8cde30e3772b Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Thu, 19 Oct 2017 16:59:23 -0400 Subject: [PATCH] Update examples to be remotely legible What are coding standards. --- Examples/LowerHalf.c | 16 ++-- Examples/PipeExecDemo.c | 86 +++++++++--------- Examples/PipeScience.c | 196 ++++++++++++++++++++-------------------- Examples/Up.c | 13 +-- 4 files changed, 158 insertions(+), 153 deletions(-) diff --git a/Examples/LowerHalf.c b/Examples/LowerHalf.c index dc262c7..58695f1 100644 --- a/Examples/LowerHalf.c +++ b/Examples/LowerHalf.c @@ -1,15 +1,17 @@ -#include <stdio.h> // Steven's Figure 15.14 "UpCase" -#include "apue.h" // modified to lower the last half -int main() -{ int c; +#include <stdio.h> // Steven's Figure 15.14 "UpCase" +#include "apue.h" // modified to lower the last half - while (( c = getchar()) != EOF) - { if ( islower(c)) + +int main() { + int c; + + while (( c = getchar()) != EOF) { + if ( islower(c)) c = toupper(c); if (c >= 'M') c = tolower(c); if (putchar(c) == EOF) err_sys("output error in upcase\n"); - if (c == '\n') + if (c == '\n') fflush(stdout); } exit(0); diff --git a/Examples/PipeExecDemo.c b/Examples/PipeExecDemo.c index c32eedf..1c8b078 100644 --- a/Examples/PipeExecDemo.c +++ b/Examples/PipeExecDemo.c @@ -1,42 +1,44 @@ - -#include <stdio.h> -#include "apue.h" // A demo of simple pipe -#include <unistd.h> // from child to parent -#include <sys/types.h> // They tire of it, and exec -#include <sys/wait.h> // into ls and upper ? , - // respectively -int main() -{int fd[2]; // an int array to be used by the pipe system call - pid_t pid; // the universal process id variable - int j; // the universal integer - char buf[5]; // a modest buffer - if (pipe(fd)== -1) err_sys("pipe error"); - if((pid=fork())<0)err_sys("forked badly"); - - if (pid == 0) // child - {close(fd[0]); // won't be reading pipe - - write(fd[1],"some characters",15);//some piped letters - fd[1]=dup2(fd[1],STDOUT_FILENO); // this process' Standard Out - write(STDOUT_FILENO," after dup2 stuff",021); - - - write(STDOUT_FILENO,"after stuff",5); - execlp("/bin/ls", "ls","-l",(void *)0); - write(STDOUT_FILENO,"ls failed\n",10); - // is now piped - // and finally child becomes ls - } - - if (pid > 0 ) // Parent - {close(fd[1]); // no write to pipe here - fd[0] = dup2(fd[0], STDIN_FILENO); - write (STDOUT_FILENO, "parent",6); // message to stdout -// This turns parent into Upcaser - execlp("./Upcase","Upcase",NULL); // your code might have something like - // execvp(argvp[place], &argvp[place]) where - // you have modified argvp array with NULL - // in appropriate places (like where the | is) - } -} - + +#include <stdio.h> +#include "apue.h" // A demo of simple pipe +#include <unistd.h> // from child to parent +#include <sys/types.h> // They tire of it, and exec +#include <sys/wait.h> // into ls and upper ? , respectively + + +int main() +{ + int fd[2]; // an int array to be used by the pipe system call + pid_t pid; // the universal process id variable + int j; // the universal integer + char buf[5]; // a modest buffer + if (pipe(fd)== -1) err_sys("pipe error"); + if((pid=fork())<0) err_sys("forked badly"); + + if (pid == 0) { // child + close(fd[0]); // won't be reading pipe + + write(fd[1],"some characters",15);//some piped letters + fd[1]=dup2(fd[1],STDOUT_FILENO); // this process' Standard Out + write(STDOUT_FILENO," after dup2 stuff",021); + + + write(STDOUT_FILENO,"after stuff",5); + execlp("/bin/ls", "ls","-l",(void *)0); + write(STDOUT_FILENO,"ls failed\n",10); + // is now piped + // and finally child becomes ls + } + + if (pid > 0 ) { // Parent + close(fd[1]); // no write to pipe here + fd[0] = dup2(fd[0], STDIN_FILENO); + write (STDOUT_FILENO, "parent",6); // message to stdout + // This turns parent into Upcaser + execlp("./Upcase","Upcase",NULL); // your code might have something like + // execvp(argvp[place], &argvp[place]) where + // you have modified argvp array with NULL + // in appropriate places (like where the | is) + } +} + diff --git a/Examples/PipeScience.c b/Examples/PipeScience.c index 02153e3..d07cb48 100644 --- a/Examples/PipeScience.c +++ b/Examples/PipeScience.c @@ -1,98 +1,98 @@ -// PipeScience to demo general scheme for managing multiple pipes -// for shell assignment -// last modified 11/3/15 -/* Have a parent create a child (RightMost) which creates a -child (M) which creates a child (LeftMost) , all piped , and -ultimately implements the command - - ls -l | Upper | Lower - -where those are local programs that play with character streams - -Each process will have a left and right pipe, fdl and fdr, -(if you know what I mean) but only M uses them both. - -*/ -#include <wait.h> -#include "apue.h" // for error routines - -int main ( ) -{ pid_t pid; - int Status; // for wait() - int fdl[2];int fdr[2]; // Pipe File Descriptors - - // 0 for reading, 1 for writing ? - write(STDOUT_FILENO, "Top Level\n",10); // just a landmark - - pid = fork(); // First fork a copy of 'shell' - if (pid == 0 ) // first child named "RM" for Rightmost - - { write(1,"RM\n",3); - pipe(fdl); // Pipe to share between M and RM - pid = fork(); // and now it is - - if (pid >0 ) //RM is new parent; everybody is a parent sometime - // RM's child is "M" for Middle - // And M and RM share a pipe - // Here we are still in RM - // (you need to keep track of context) - { close(fdl[1]); // do the plumbing - // RM does not write to "left" - fdl[0]= dup2(fdl[0],STDIN_FILENO); - // But reads from left as stdin - write (1, "Ready to Exec\n",13); - if (((execl("./Lower","Lower",NULL))!=0)) - write(1,"Lower fail\n",15); - // and transmogrifies - } // exit RM - - if (pid == 0) // M , the child - // M's world -- first make fdl <== fdr copy N.B. **** - { write (1,"M\n",2); - fdr[1]=fdl[1]; // making - // M's right <==> RM's left - fdr[1]=dup2(fdr[1],STDOUT_FILENO); - // and M's stdout is the pipe out - fdr[0]=fdl[0];close(fdr[0]); - // for symmetry - pipe(fdl); // Science Q ? We can pipe and repipe - // fdl because they are just integers ? Yup - - pid = fork(); // M hatches LM (leftmost) - - if (pid > 0) // Still M's world - { close(fdl[1]); - // No write on left - // but read from pipe as from stdin - fdl[0]= dup2(fdl[0],STDIN_FILENO);//MIDDLE - // and transmogrify - write (1, "Ready to Exec in M\n",19); - if ((execl("Upper","Upper",(char *)0)!=0)) - write(1,"Upper fail\n",12); - - } // exit M -/* UNTIL HERE FOR MIDDLE PROCESS */ - - if (pid == 0) // LM's world - // LM has fdl and fdr but uses only one of these - // and M has messed with fdl - // so for symmetry .... - { write(1,"LM\n",3); - fdr[0] = fdl[0]; - fdr[1] = fdl[1]; close(fdr[0]); - fdr[1] = dup2(fdr[1],STDOUT_FILENO); - // and LM has no tricks on stdin because - // LM will not fork and no redirection '>' - if ((execlp("ls","./ls","-l", (char *) 0)!=0)) - write(1,"ls fail\n",8); - // transmogrify - } // exit LM - - } // which was within M from up there at **** - -} // back to the top level - waitpid(pid,&Status,0); - write(1,"Top Level Again\n",16); -} // end of the show, unless we have user input drive us again. - // and perhaps with more pipes than this one, eh ? - +// PipeScience to demo general scheme for managing multiple pipes +// for shell assignment +// last modified 11/3/15 +/* Have a parent create a child (RightMost) which creates a +child (M) which creates a child (LeftMost) , all piped , and +ultimately implements the command + + ls -l | Upper | Lower + +where those are local programs that play with character streams + +Each process will have a left and right pipe, fdl and fdr, +(if you know what I mean) but only M uses them both. +*/ + +#include <wait.h> +#include "apue.h" // for error routines + + +int main() { + pid_t pid; + int Status; // for wait() + int fdl[2];int fdr[2]; // Pipe File Descriptors + + // 0 for reading, 1 for writing ? + write(STDOUT_FILENO, "Top Level\n",10); // just a landmark + + pid = fork(); // First fork a copy of 'shell' + if (pid == 0 ) { // first child named "RM" for Rightmost + + write(1,"RM\n",3); + pipe(fdl); // Pipe to share between M and RM + pid = fork(); // and now it is + + if (pid >0 ) { //RM is new parent; everybody is a parent sometime + // RM's child is "M" for Middle + // And M and RM share a pipe + // Here we are still in RM + // (you need to keep track of context) + close(fdl[1]); // do the plumbing + // RM does not write to "left" + fdl[0]= dup2(fdl[0],STDIN_FILENO); + // But reads from left as stdin + write (1, "Ready to Exec\n",13); + if (((execl("./Lower","Lower",NULL))!=0)) + write(1,"Lower fail\n",15); + // and transmogrifies + } // exit RM + + if (pid == 0) { // M , the child + // M's world -- first make fdl <== fdr copy N.B. **** + write (1,"M\n",2); + fdr[1]=fdl[1]; // making + // M's right <==> RM's left + fdr[1]=dup2(fdr[1],STDOUT_FILENO); + // and M's stdout is the pipe out + fdr[0]=fdl[0];close(fdr[0]); + // for symmetry + pipe(fdl); // Science Q ? We can pipe and repipe + // fdl because they are just integers ? Yup + + pid = fork(); // M hatches LM (leftmost) + + if (pid > 0) { // Still M's world + close(fdl[1]); + // No write on left + // but read from pipe as from stdin + fdl[0]= dup2(fdl[0],STDIN_FILENO);//MIDDLE + // and transmogrify + write (1, "Ready to Exec in M\n",19); + if ((execl("Upper","Upper",(char *)0)!=0)) + write(1,"Upper fail\n",12); + + } // exit M + /* UNTIL HERE FOR MIDDLE PROCESS */ + + if (pid == 0) { // LM's world + // LM has fdl and fdr but uses only one of these + // and M has messed with fdl + // so for symmetry .... + write(1,"LM\n",3); + fdr[0] = fdl[0]; + fdr[1] = fdl[1]; close(fdr[0]); + fdr[1] = dup2(fdr[1],STDOUT_FILENO); + // and LM has no tricks on stdin because + // LM will not fork and no redirection '>' + if ((execlp("ls","./ls","-l", (char *) 0)!=0)) + write(1,"ls fail\n",8); + // transmogrify + } // exit LM + + } // which was within M from up there at **** + + } // back to the top level + waitpid(pid,&Status,0); + write(1,"Top Level Again\n",16); +} // end of the show, unless we have user input drive us again. + // and perhaps with more pipes than this one, eh ? diff --git a/Examples/Up.c b/Examples/Up.c index c8f842d..cbec338 100644 --- a/Examples/Up.c +++ b/Examples/Up.c @@ -1,16 +1,17 @@ -#include <stdio.h> // Steven's Figure 15.14 "UpCase" +#include <stdio.h> // Steven's Figure 15.14 "UpCase" #include "apue.h" #include <ctype.h> -int main() -{ int c; - while (( c = getchar()) != EOF) - { if ( islower(c)) +int main() { + int c; + + while (( c = getchar()) != EOF) { + if ( islower(c)) c = toupper(c); if (putchar(c) == EOF) err_sys("output error in upcase\n"); - if (c == '\n') + if (c == '\n') fflush(stdout); } exit(0); -- GitLab