Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify psh2.c to exit when the user types the command ** '93exit'94 or when the program sees end of file psht.c: /** prompting shell version

Modify psh2.c to exit when the user types the command\ ** \'93exit\'94 or when the program sees end of file

psht.c:

/** prompting shell version 2\ **\ ** Solves the `one-shot' problem of version 1\ ** Uses execvp(), but fork()s first so that the\ ** shell waits around to perform another command\ ** New problem: shell catches signals. Run vi, press ^c.\ **/\ \ #include \ #include \ \ #define MAXARGS 20 /* cmdline args */\ #define ARGLEN 100 /* token length */\ \ main()\ \{\ char *arglist[MAXARGS+1]; /* an array of ptrs */\ int numargs; /* index into array */\ char argbuf[ARGLEN]; /* read stuff here */\ char *makestring(); /* malloc etc */\ \ numargs = 0;\ while ( numargs < MAXARGS )\ \{ \ printf("Arg[%d]? ", numargs);\ if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\ ' )\ arglist[numargs++] = makestring(argbuf);\ else\ \{\ if ( numargs > 0 )\{ /* any args? */\ arglist[numargs]=NULL; /* close list */\ execute( arglist ); /* do it */\ numargs = 0; /* and reset */\ \}\ \}\ \}\ return 0;\ \}\ \ execute( char *arglist[] )\ /*\ * use fork and execvp and wait to do it\ */\ \{\ int pid,exitstatus; /* of child */\ \ pid = fork(); /* make new process */\ switch( pid )\{\ case -1: \ perror("fork failed");\ exit(1);\ case 0:\ execvp(arglist[0], arglist); /* do it */\ perror("execvp failed");\ exit(1);\ default:\ while( wait(&exitstatus) != pid )\ ;\ printf("child exited with status %d,%d\ ",\ exitstatus>>8, exitstatus&0377);\ \}\ \}\ char *makestring( char *buf )\ /*\ * trim off newline and create storage for the string\ */\ \{\ char *cp, *malloc();\ \ buf[strlen(buf)-1] = '\\0'; /* trim newline */\ cp = malloc( strlen(buf)+1 ); /* get memory */\ if ( cp == NULL )\{ /* or die */\ fprintf(stderr,"no memory\ ");\ exit(1);\ \}\ strcpy(cp, buf); /* copy chars */\ return cp; /* return ptr */\ \}}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

What are the best practices for managing a large software project?

Answered: 1 week ago

Question

How does clustering in unsupervised learning help in data analysis?

Answered: 1 week ago