Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Input and Output Redirection Introduction In this laboratory you will add input and output redirection to the lab2b.c program from laboratory two. In that program

Input and Output Redirection

Introduction

In this laboratory you will add input and output redirection to the lab2b.c program from laboratory two. In that program we had a fork() and in the child process execve() was used to start another program. In this laboratory we will add redirection before the new program is started.

Copy Program

We need a program to start that does something with standard input and output. We will write a simple copy program, copy, for this. Start with the program from laboratory one and remove the statements that open the input and output files. Instead set fin to 0 and fout to 1, so the program reads from standard input and writes to standard output.

Lab four Program

Now that we have a program to start modify the lab2b.c program so it starts the copy program in the child process (you just need to change the file name in the execve call). Before executing the execve program open input for reading and output for writing. Then use dup2 to redirect inputto standard input and output to standard output. You will need some input data to test your program. I just copied copy.c to input, and then checked that output had the same content.

/****************************************** * * lab1.c * * A simple copy program that demostrates * basis system calls. * * Usage: lab1 infile outfile * *****************************************/

#include #include #include

#include #include #include #include #include

int main(int argc, char **argv) { int fin; int fout; int n; char buffer[512]; int ret;

struct stat buffer1, buffer2; if(argc != 3) { printf("Usage: lab1 infile outfile "); exit(1); }

fin = open(argv[1], O_RDONLY); if(fin < 0) { printf("Can't open input file: %s ",strerror(errno)); exit(1); }

fout = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); if(fout < 0) { printf("Can't open output file: %s ",strerror(errno)); exit(1); }

n=1; // Get the process started while(n > 0) { n = read(fin, buffer, 512); if(n < 0) { printf("Error on read: %s ",strerror(errno)); exit(1); } ret = write(fout, buffer, n); if(ret < 0) { printf("Erroc on write: %s ",strerror(errno)); exit(1); } } // Obtaining file information fstat(fin, &buffer1); fstat(fout, &buffer2);

// printf("Initial Permissions "); printf("Permissions of %s: %o ", argv[1], buffer1.st_mode); printf("Permissions of %s: %o ", argv[2], buffer2.st_mode);

// Performing chmod on the output file based on the permissions of the input file fchmod(fout, buffer1.st_mode);

//Obtaining file information again post chmod fstat(fin, &buffer1); fstat(fout, &buffer2);

printf("Post Copy Permissions "); printf("Permissions of %s: %o ", argv[1], buffer1.st_mode); printf("Permissions of %s: %o ", argv[2], buffer2.st_mode);

close(fin); close(fout); exit(0); } /****************************************** * * lab2a * * This program illustrates the use of the * execve system call. * *****************************************/

#include #include #include #include #include #include

extern char **environ;

int main(int argc, char **argv) { int pid; int ret; int status;

if((pid = fork())) { if(pid < 0) { printf("Fork error: %s ",strerror(errno)); exit(1); } printf("Wait: %d ", wait(&status)); } else { ret = execve("lab2a", argv, environ); if(ret < 0) { printf("Execve failed: %s ", strerror(errno)); exit(1); } }

exit(0); }

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

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions

Question

Answered: 1 week ago

Answered: 1 week ago