Question: Solution for Ch.8 #include #include #include #include #include #include #include #include #include #include #include #include #include #define BUFSIZE 128 #define oops( m, x ) {
Solution for Ch.8
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFSIZE 128
#define oops( m, x ) { perror(m); exit(x); }
/*
A. The user types a.out
B. The shell creates a new process to run
the program.
C. The shell loads the program from disk
into the process.
D. The program runs in its process until done
*/
/*
while( ! end_of_input )
get command
execute command
wait for command to finish
*/
const int MAX_CHARS = 255;
const int MAX_ARGS = 20;
void get(char*);
void execute( char* );
char** parse( char*);
//int cd(char* directory);
int main(int ac, char *args[]) //ac stores number of arguments, args stores actual arguments
{
static int skip = 0;
char* command[MAX_ARGS+1];
char commandment[MAX_CHARS];
signal(SIGINT, SIG_IGN); // Ignore Ctrl-C
while(1)
{
get(commandment);
execute(commandment);
}
}
void execute(char* arglist)
/*
use fork and execvp and wait to do it
*/
{
char** split = parse(arglist);
int pid, exitstatus; // of child
int len;
int i = 0;
char buf[BUFSIZ]; // for reading end
int fd, apipe[2]; ;
char null = '\0';
pid = fork(); // make new process
char IORedirection = '\0';
switch( pid )
{
case -1:
perror( "fork failed" );
exit(1);
case 0:
while( split[++i] != '\0' )
{
if ( split[i][0] == '' || split[i][0] == '|' )
{
IORedirection = split[i][0];
break;
}
}
if ( IORedirection == '
{
close(0);
fd = open(split[i+1], O_RDONLY );
if ( fd == -1 )
{
printf("Error: File could not be opened!");
exit(1);
}
split[i] = '\0';
}
else if (IORedirection == '>' )
{
close(1);
fd = open( split[i+1], O_WRONLY | O_CREAT, 0744 );
if ( fd == -1 )
{
printf("Error: File could not be opened!");
exit(1);
}
split[i] = '\0';
}
if (IORedirection == '|' )
{
// get a pipe
if ( pipe ( apipe ) == -1 )
{
perror( "could not make pipe" );
exit(1);
}
printf( "Got a pipe! It has file descriptors: ( %d %d ) ",
apipe[0], apipe[1] );
split[i] = '\0';
/*
// read from stdin, write into pipe, read from pipe, print
while( fgets( buf, BUFSIZ, stdin ) )
{
len = strlen( buf );
// send down pipe
if ( write( apipe[1], buf, len ) != len )
{
perror( "writing to pipe" );
break;
}
// wipe
for ( i = 0; i
buf[i] = 'X';
// read from pipe
len = read( apipe[0], buf, BUFSIZ );
if ( len == -1 )
{
perror( "reading from pipe" );
break;
}
// send to stdout
if ( write( 1, buf, len ) != len )
{
perror( "writing to stdout" );
break;
}
}
*/
}
execvp( split[0], split ); // do it
// perror( "execvp failed" );
exit(1);
default:
while( wait( &exitstatus ) != pid );
printf( "child exited with status %d, %d ",
exitstatus>>8, exitstatus & 0377 );
}
}
char** parse( char* input)
{
int i = 0;
char** split = malloc( 256 );
// fgets reads the , so overwrite it
// input[strlen(input)-1] = '\0';
// get the first token
split[i] = strtok( input, " " );
// get the rest of them
while( split[++i] = strtok(NULL, " ") );
return split;
}
void get(char* commandment)
{
char *cd = "cd";
printf("$ "); // Prompt for command
fgets( commandment, MAX_CHARS, stdin );
commandment[strlen(commandment)-1] = '\0';
if ( strcmp( commandment, "exit" ) == 0 )
exit(1);
if ( strstr( commandment, cd ) != NULL )
{
char *directory = strtok(commandment," ");
directory = strtok(NULL," ");
if(chdir(directory) == -1) {
perror("Cannot change directory");
}
else
printf("Directory changed ");
}
}
Course Evaluation Student Support Services Hints and Technical Info Midterm Final a Search or enter website name Apple Disney ESPN Yahoo! Student Courses Online Storage E-Portfolio Organizations & Clubs ample Code Attached Files: ch10SampleCode.zip (4.921 KB) HW10 Due Tuesday, May 30th, 11:59 PM Late submissions not accepted. For this assignment, you will add support for i/o redirection and piping to the shell you wrote for the CH08 homework. That is to say, and I. Feel free to use the solution from Chapter 8 to get you started. For example, your shell should now support commands like this: ls -l file.txt cat file.txt I wc file.txt sort file.txt Reminder Recursion is a thing. Course Evaluation Student Support Services Hints and Technical Info Midterm Final a Search or enter website name Apple Disney ESPN Yahoo! Student Courses Online Storage E-Portfolio Organizations & Clubs ample Code Attached Files: ch10SampleCode.zip (4.921 KB) HW10 Due Tuesday, May 30th, 11:59 PM Late submissions not accepted. For this assignment, you will add support for i/o redirection and piping to the shell you wrote for the CH08 homework. That is to say, and I. Feel free to use the solution from Chapter 8 to get you started. For example, your shell should now support commands like this: ls -l file.txt cat file.txt I wc file.txt sort file.txt Reminder Recursion is a thing
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
