Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// C Program This code is supposed to handle commands like ls -l -a or wc < test.txt or wc -c -l -w < file1.txt

// C Program

This code is supposed to handle commands like ls -l -a or wc < test.txt or wc -c -l -w < file1.txt > file2.txt, or who -HT >file.txt . For the most part this code can handle basic commands such as ls -l but I need help with the file redirection.

#include

#include

#include

#include

#include

#include

typedef struct param {

char *param;

struct param *next;

} pt;

typedef struct cmd_list {

char *cmd;

int param_count;

pt *param_list;

char * input_file_name;

char *output_file_name;

struct cmd_list *next;

} clt;

void execommands(clt *cmds) {

clt *cmd = cmds;

pid_t pid = fork();

switch(pid) {

case -1:

// error return break;

case 0: {

char ** rhp_argv = NULL;

char * rhp = NULL;

pt *temp = NULL;

int i = 1;

int flag_count = 0;

char flag_string[100] = "";

temp = cmd->param_list;

rhp = cmd->cmd;

// allocate memory for rhp_argv

if(0 != cmd->param_count)

rhp_argv = (char **) calloc((cmd->param_count+1), sizeof(char *));

// set first element to command rhp_argv[0] = rhp;

// add remaining parameters to rhp_argv

temp = cmd->param_list;

while(temp != NULL) {

strcat(flag_string, temp->param);

flag_count++;

temp = temp->next;

}

rhp_argv[i] = strdup(flag_string);

i++;

}

else {

rhp_argv = (char **) calloc(2, sizeof(char *));

rhp_argv[0] = rhp;

}

execvp(rhp_argv[0], rhp_argv);

// error exit

//free rhp_argv

} default:

{

int status;

waitpid(pid, &status, 0);

break;

}

}

}

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

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions