Maha S.

asked • 03/18/25

explain thus code

#include <stdio.h>

#include <sys/types.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/wait.h>


#define MAX 1024


void fileRedir(char *redirection, char *file)

{

int fdout;


if (!strcmp(redirection, "<"))

{

if ((fdout = open(file, O_CREAT | O_RDONLY)) < 0)

{

perror(file);

exit(1);

}

dup2(fdout, 0); // Redirect input

}

else if (!strcmp(redirection, ">"))

{

if ((fdout = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0)

{

perror(file);

exit(1);

}

dup2(fdout, 1); // Redirect output

}

}


void parse(char *line, char **argv)

{

while (*line != '\0') { /* If not the end of line */

while (*line == ' ' || *line == '\t' || *line == '\n')

*line++ = '\0'; /* Replace white spaces with 0 */

*argv++ = line; /* Save the argument position */

while (*line != '\0' && *line != ' ' && *line != '\t' && *line != '\n')

line++; /* Skip the argument */

}

*argv = '\0'; /* Mark the end of argument list */

}


void execute(char **argv)

{

pid_t pid;

int status;


if ((pid = fork()) < 0) { /* Fork a child process */

printf("*** ERROR: forking child process failed\n");

exit(1);

}

else if (pid == 0) { /* In the child process: */

if (execvp(*argv, argv) < 0) { /* Execute the command */

printf("*** ERROR: exec failed\n");

exit(1);

}

}

else { /* In the parent process */

while (wait(&status) != pid); /* Wait for child process to finish */

}

}


void main(void)

{

char line[MAX]; /* The input line */

char *argv[64]; /* The command line arguments */

char *redirection = NULL;

char *file = NULL;


while (1) { /* Repeat until done */

printf("Shell -> "); /* Display a prompt */

if (fgets(line, MAX, stdin) != 0) {

line[strcspn(line, "\n")] = '\0'; /* Remove the trailing newline */

printf("\n");


// Check if the command is "exit" to terminate the shell

if (strcmp(line, "exit") == 0) {

printf("Exiting shell...\n");

break; /* Exit the loop */

}


// Check for redirection operators (< or >)

if ((redirection = strstr(line, "<")) != NULL) {

*redirection = '\0'; // Split the command and the file name

redirection = "<";

file = redirection + 1;

while (*file == ' ' || *file == '\t') file++; // Skip any spaces

}

else if ((redirection = strstr(line, ">")) != NULL) {

*redirection = '\0'; // Split the command and the file name

redirection = ">";

file = redirection + 1;

while (*file == ' ' || *file == '\t') file++; // Skip any spaces

}


parse(line, argv); /* Parse the line */

// If redirection is found, call fileRedir() before executing

if (redirection != NULL) {

fileRedir(redirection, file); /* Handle file redirection */

}


execute(argv); /* Execute the command */

}

}

}

Daniel B.

tutor
It is a shell interpreter. But upon casual reading it seems to have a bug in the handling of the variable redirection.
Report

03/18/25

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.