Question: // 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!