Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <sys/types.h> #include <sys/wait.h> #include <errno.h> #include <stdio.

ID: 3853683 • Letter: #

Question

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>


char *getinput(char *buffer, size_t buflen) {
printf("$$ ");
return fgets(buffer, buflen, stdin);
}

int main(int argc, char **argv) {
char buf[1024];
char command[10], opr[2], filename[20],chr;
pid_t pid;
int status,i;
char *target_filename="temp.txt";
FILE *source_file, *target_file;

while (getinput(buf, sizeof(buf))) {
buf[strlen(buf) - 1] = '';
/* Place your code to check "exit". If so, then exit */
if(strcmp(buf,"exit")==0){
printf("Exiting... ");
exit(0);
}

if((pid=fork()) == -1) {
fprintf(stderr, "shell: can't fork: %s ", strerror(errno));
continue;
} else if (pid == 0) {
/* child process to do each command
– place your code here to handle read, write, append */
if(strstr(buf,"read")!=NULL){
printf("Read command ");
for(i=0;i<strlen(buf);i++){
if(buf[i]=='<')
break;
}
strncpy(command,buf,i-1);
strncpy(filename,buf+(i+1),strlen(buf)-(i+1));
source_file=fopen(filename,"r");
target_file=fopen(target_filename,"w");
if(!source_file){
fprintf(stderr,"File not found! ");
}
else{
printf("Reading file %s ",filename);
chr=fgetc(source_file);
while(chr!=EOF){
fputc(chr,target_file);
chr=fgetc(source_file);
}
printf("File reading complete! Check %s for output. ",target_filename);
}
fclose(source_file);
fclose(target_file);
}
else if(strstr(buf,"write")!=NULL){
printf("Write command ");
for(i=0;i<strlen(buf);i++){
if(buf[i]=='>')
break;
}
strncpy(command,buf,i-1);
strncpy(filename,buf+(i+1),strlen(buf)-(i+1));
source_file=fopen(target_filename,"r");
target_file=fopen(filename,"w");
if(!source_file){
fprintf(stderr,"File not found! ");
}
else{
printf("Writing to file %s ",filename);
chr=fgetc(source_file);
while(chr!=EOF){
fputc(chr,target_file);
chr=fgetc(source_file);
}
printf("File writing complete! Check %s for output. ",filename);
}
fclose(source_file);
fclose(target_file);
}
else if(strstr(buf,"append")!=NULL){
printf("Append command ");
for(i=0;i<strlen(buf);i++){
if(buf[i]=='>')
break;
}
strncpy(command,buf,i-1);
strncpy(filename,buf+(i+2),strlen(buf)-(i+2));
printf("%s ",filename);
source_file=fopen(target_filename,"r");
target_file=fopen(filename,"a");
if(!source_file){
fprintf(stderr,"File not found! ");
}
else{
printf("Appending to file %s ",filename);
chr=fgetc(source_file);
while(chr!=EOF){
fputc(chr,target_file);
chr=fgetc(source_file);
}
printf("File appending complete! Check %s for output. ",filename);
}
fclose(source_file);
fclose(target_file);
}

exit(EX_OK);
}

if ((pid=waitpid(pid, &status, 0)) < 0)
fprintf(stderr, "shell: waitpid error: %s ",
strerror(errno));
}
exit(EX_OK);
}

I have posted the code and I just need help with task 5. TIA!

Design and implement C/C++program (myfle.c) in a loop: (1) to prompt for a command, (2) to read a command (from keyboard as you type), (3) to echo the command, (4) to fork a child process to process command while the parent process is waiting for child process to complete this part, and then (5) the parent is back to the command prompt for next command. The commands are: read, write, and append, and exit. The following tasks (that is, test cases) are to be implemented. Task1. When the command is "exit", then the program terminates with exit(0). Task2. The "read" command will do a file-redirection for a file to be read and to be written to a temporary file. For example, "read outl.c" will read the save temp file "temp.txt" and write it to the file ("outl.c"). Task4. The "append" command has a file-redirection for a file (temp.txt) to be appended to a file. For example, "append > out2.c" will read the save temp file "temp.txt" and write it to the file ("out2.c"). Task5. You may have two commands with a pipe. For example, "read hello2.c" will read bella.sfile and write it to hello2.c via temp.txt file. Task6. Create a Makefile to build the executable for the codes in this part.

Explanation / Answer

Hi Please let me know if you need more information:-

-------------------------------------------------------------------------

Seems main part in this program is to extract the command.

-------------------------------------------------------------------------------------

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char *getinput(char *buffer, size_t buflen) {
   printf("$$ ");
   return fgets(buffer, buflen, stdin);
}

int main(int argc, char **argv) {
   char buf[1024];
   char command[10], opr[2], filename[20], chr;
   pid_t pid;
   int status, i;
   char *target_filename = "temp.txt";
   FILE *source_file, *target_file;

   while (getinput(buf, sizeof(buf))) {
       buf[strlen(buf) - 1] = '';
       /* Place your code to check "exit". If so, then exit */
       if (strcmp(buf, "exit") == 0) {
           printf("Exiting... ");
           exit(0);
       }
       if ((pid = fork()) == -1) {
           fprintf(stderr, "shell: can't fork: %s ", strerror(errno));
           continue;
       } else if (pid == 0) {
           /* child process to do each command
           – place your code here to handle read, write, append */
           if (strstr(buf, "read") != NULL) {
               printf("Read command ");
               for (i = 0; i < strlen(buf); i++) {
                   if (buf[i] == '<')
                       break;
               }
               strncpy(command, buf, i - 1);
               strncpy(filename, buf + (i + 1), strlen(buf) - (i + 1));
               source_file = fopen(filename, "r");
               target_file = fopen(target_filename, "w");
               if (!source_file) {
                   fprintf(stderr, "File not found! ");
               } else {
                   printf("Reading file %s ", filename);
                   chr = fgetc(source_file);
                   while (chr != EOF) {
                       fputc(chr, target_file);
                       chr = fgetc(source_file);
                   }
                   printf("File reading complete! Check %s for output. ",
                           target_filename);
               }
               fclose(source_file);
               fclose(target_file);
           } else if (strstr(buf, "write") != NULL) {
               printf("Write command ");
               for (i = 0; i < strlen(buf); i++) {
                   if (buf[i] == '>')
                       break;
               }
               strncpy(command, buf, i - 1);
               strncpy(filename, buf + (i + 1), strlen(buf) - (i + 1));
               source_file = fopen(target_filename, "r");
               target_file = fopen(filename, "w");
               if (!source_file) {
                   fprintf(stderr, "File not found! ");
               } else {
                   printf("Writing to file %s ", filename);
                   chr = fgetc(source_file);
                   while (chr != EOF) {
                       fputc(chr, target_file);
                       chr = fgetc(source_file);
                   }
                   printf("File writing complete! Check %s for output. ",
                           filename);
               }
               fclose(source_file);
               fclose(target_file);
           } else if (strstr(buf, "append") != NULL) {
               printf("Append command ");
               for (i = 0; i < strlen(buf); i++) {
                   if (buf[i] == '>')
                       break;
               }
               strncpy(command, buf, i - 1);
               strncpy(filename, buf + (i + 2), strlen(buf) - (i + 2));
               printf("%s ", filename);
               source_file = fopen(target_filename, "r");
               target_file = fopen(filename, "a");
               if (!source_file) {
                   fprintf(stderr, "File not found! ");
               } else {
                   printf("Appending to file %s ", filename);
                   chr = fgetc(source_file);
                   while (chr != EOF) {
                       fputc(chr, target_file);
                       chr = fgetc(source_file);
                   }
                   printf("File appending complete! Check %s for output. ",
                           filename);
               }
               fclose(source_file);
               fclose(target_file);
           } else if (strstr(buf, "|write") != NULL) {
               printf("pipe command ");
               /*Extracting the Commands from Input - START*/
               char *ret;
               char leftCommand[1024];
               char rightCommand[1024];
               char a[30];
               char b[40];
               ret = strstr(buf, "|");
               printf("%s ", ret);
               int size = strlen(buf);
               strncpy(leftCommand, ret + 1, strlen(ret));
               strncpy(rightCommand, buf, size - strlen(ret));
               rightCommand[strlen(rightCommand)] = '';
               printf("The substring is: %s,%s ", leftCommand, rightCommand);
               /*Extracting the Commands from Input - END*/

               printf("Read command ");
               for (i = 0; i < strlen(leftCommand); i++) {
                   if (leftCommand[i] == '<')
                       break;
               }
               strncpy(command, leftCommand, i - 1);
               strncpy(filename, leftCommand + (i + 1),
                       strlen(leftCommand) - (i + 1));
               source_file = fopen(filename, "r");
               target_file = fopen(target_filename, "w");
               if (!source_file) {
                   fprintf(stderr, "File not found! ");
               } else {
                   printf("Reading file %s ", filename);
                   chr = fgetc(source_file);
                   while (chr != EOF) {
                       fputc(chr, target_file);
                       chr = fgetc(source_file);
                   }
                   printf("File reading complete! Check %s for output. ",
                           target_filename);
               }
               fclose(source_file);
               fclose(target_file);

               printf("Write command ");
               for (i = 0; i < strlen(rightCommand); i++) {
                   if (rightCommand[i] == '>')
                       break;
               }
               strncpy(command, rightCommand, i - 1);
               strncpy(filename, rightCommand + (i + 1),
                       strlen(rightCommand) - (i + 1));
               source_file = fopen(target_filename, "r");
               target_file = fopen(filename, "w");
               if (!source_file) {
                   fprintf(stderr, "File not found! ");
               } else {
                   printf("Writing to file %s ", filename);
                   chr = fgetc(source_file);
                   while (chr != EOF) {
                       fputc(chr, target_file);
                       chr = fgetc(source_file);
                   }
                   printf("File writing complete! Check %s for output. ",
                           filename);
               }
               fclose(source_file);
               fclose(target_file);
           }
           exit (EX_OK);
       }
       if ((pid = waitpid(pid, &status, 0)) < 0)
           fprintf(stderr, "shell: waitpid error: %s ", strerror(errno));
   }
   exit (EX_OK);
}

-----------------------------------------------------------

Thanks