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

If you ever used script command in Unix, then you know what it does. If not, her

ID: 3586037 • Letter: I

Question

If you ever used script command in Unix, then you know what it does. If not, here what [man script] says "the script command makes a record of everything printed on your screen. The record is written to a log file. The script command forks and creates a sub-shell, according to the value of SSHELL, and records the text from this session. The script ends when the forked shell exits or when CTRL-D is typed." For example, it is executed as follows and it logs everything on the screen in logfile.txt > script logfile.txt Script started, file is logfile.txt al.txt bl.txt prog.c > exit exit Script done, file is logfile.txt Suppose we like to have a simple implementation of script.c as follows: script program (parent) creates a child process which excel a shell and interacts with the user. But for parent to do its job, it needs to get everything that child shell prints on the standard output through parent own standard input. Hope you see how a pipe ill be useful here: childl1]-pipe->l0lparent. Accordingly, the parent writes everything that it gets through the pipe to both its standard output and the logfile Now you are asked to complete the following program so you can create the necessary pipe, child process and connect them as explained in the above scenario. You can ignore most of the error checking to make your solution clear, but you need to close all unnecessary file descriptors and check what read write etc return. /your simple implementation of script.c #include #include #include #include int main (int argc, char argv) int mypipe [2]: int log fd, childpid, numread, numwrite: char buf; 5pt - create pipe and child process * if (childpid 0) child * 5pt - child sets up the pipe* execl ("/bin/bash", "shell", NULL) perror ("cannot start shel1") return l; /* continue in the next page /

Explanation / Answer

#include<fcnt1.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char* argv[])
{
int my_pipe[2];
int log_fd, childpid, numread, numwrite;
char buf;

pipe(my_pipe);
  
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if (childpid==0)
   {
       close(my_pipe[0]);

printf("scripting started. file is %s", argv[1])

       dup2(1, my_pipe[1]);
   execl("bin/bash","shell",NULL);
   perror("Cannot start Shell");
   return 1;
   }
  
   log_fd=open(argv[1], O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
   close(my_pipe[1]);
   while(1)
   {
       numread=read(my_pipe[0],buf,1);
      
       write(log_fd,numread,1);
       printf(numread);
   }
   close(log_fd);
   fprintf(stderr,"mycript finished.... ");
   waitpid(childpid);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote