Modify the program ll.c so that it will load and execute an arbitrary program th
ID: 3628261 • Letter: M
Question
Modify the program ll.c so that it willload and execute an arbitrary program that is specified on the command line, and
limit the number of lines of output the program can write to stdout.
The program is invoked as follows:
./ll line-limit program-name arguement1 arguement2 ...
For example:
./ll 100 ls -l .
./ll 10 testprog
Here is ll.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <assert.h>
/* CpSc 322
Homework 1
Skeleton of a utility that limits the number of output lines
printed by a child process to the child's "stdout". The utility
is invoked as:
./ll linelimit programname arg1 arg2 arg3...
The completed version should
1) create a Unix pipe,
2) fork a child process
3) in the child process remap stdout (file descriptor 1) of the
pipe to the write end of the pipe,
4) in the child process load the program (execvp),
5) in the parent close the write end of the pipe,
6) associate the read end of the pipe with a stream using fdopen(),
7) in a loop fgets() lines from the stream, lineCounting lines. If the
number of lines exceeds the maximum kill the child process and
terminate.
8) otherwise on end-of-file on the stream end normally.
*/
/* Main Program */
int main(int argc, char *argv[]) {
int maxLines; /* Maximum number of output lines */
int lineCount = 0; /* Current line lineCount */
int printPipe[2]; /* Pipe file descriptors */
char buf[256]; /* fgets() input buffer */
FILE *infp; /* Parent's input stream from pipe */
int childPID; /* child process's pid */
char *pathPtr; /* Used to update PATH info */
char *newPathPtr; /* Used to update PATH info */
/* Check parameters */
if (argc < 3) {
printf("Usage: ./ll linelimit program argv1 argv2... ");
exit(1);
}
/* Add current directoy to PATH (just a convenience, won't need ./ prefix
on program name) */
pathPtr = getenv("PATH");
newPathPtr = (char *) malloc(strlen(pathPtr) + strlen("PATH=") +
strlen(":./") +1);
sprintf(newPathPtr, "PATH=%s:./", pathPtr);
putenv(newPathPtr);
/* Get and convert time limit */
if ((maxLines=atoi(argv[1])) <= 0) {
printf("Error: cannot convert maxLines (argv1) ");
exit(1);
}
/* Create a pipe */
/*** STUBBED ***/
/* Create a child process, remap stdout (fd 1) to write end of pipe, and
load program */
/*** STUBBED ***/
/*>>> PARENT CODE <<<*/
/* Close end of pipe parent doesn't use */
/*** STUBBED ***/
/* Map read end of pipe to input stream infp */
/*** STUBBED -- instead just fopen the file /etc/motd ***/
assert((infp = fopen("/etc/motd", "r")) != NULL); /* REPLACE THIS!!! */
/* Read data written by child to child's stdout (write end of
pipe) */
while (fgets(buf, sizeof(buf), infp) != NULL) {
/* Test for exceeding maximum lines -- kill child if necessary */
/*** STUBBED ***/
/* Pass child's write to parent's stdout */
fputs(buf, stdout);
}
exit(0);
}
You can add lines where it says STUBBED.
PLEASE HELP ME!!!!
Explanation / Answer
/* * ll.c */ /* Main Program */ #include #include #include int main(int argc, char *argv[]) { int maxLines; /* Maximum number of output lines */ int lineCount = 0; /* Current line lineCount */ int printPipe[2]; /* Pipe file descriptors */ char buf[256]; /* fgets() input buffer */ FILE *infp; /* Parent's input stream from pipe */ int childPID; /* child process's pid */ char *pathPtr; /* Used to update PATH info */ char *newPathPtr; /* Used to update PATH info */ /* Check parameters */ if (argc < 3) { printf("Usage: ./ll linelimit program argv1 argv2... "); exit(1); } /* Add current directory to PATH (just a convenience, won't need ./ prefix on program name) */ pathPtr = getenv("PATH"); newPathPtr = (char *) malloc(strlen(pathPtr) + strlen("PATH=") + strlen( ":./") + 1); sprintf(newPathPtr, "PATH=%s:./", pathPtr); putenv(newPathPtr); /* Get and convert time limit */ if ((maxLines = atoi(argv[1])) >> PARENT CODERelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.