Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C Programming: I'm writing a small shell and I'm stuck on this part: The line of input shall be split into words, delimited by

In C Programming:

I'm writing a small shell and I'm stuck on this part:

The line of input shall be split into words, delimited by the characters in the IFS environment variable(getenv("IFS")), or " \t " (), if getenv("IFS") is unset (NULL).

A minimum of 512 words shall be supported.

Note: unset (NULL) is not the same as empty (). An empty IFS string is valid, and the entire command line would be interpreted as a single word.

Expansion

Each of the following tokens shall be expanded within each word:

Any occurrence of ~/ at the beginning of a word shall be replaced with the value of the HOME environment variable. The "/" shall be retained. (see GETENV(3))

Any occurrence of $$ within a word shall be replaced with the proccess ID of the smallsh process (see GETPID(3)).

Any occurrence of $? within a word shall be replaced with the exit status of the last foreground command (see waiting).

Any occurrence of $! within a word shall be replaced with the process ID of the most recent background process (see waiting).

If an expanded environment variable is unset, it shall be interpreted as an empty string (). This includes the PS1 variable as described above.

The $? parameter shall default to 0 (0).

The $! parameter shall default to an empty string () if no background process ID is available.

Expansion shall occur in the forward direction in a single pass, and expanded text shall not participate in further expansion token recognition (expansion is not recursive).

Here's what I have so far:

#include

#include

#include

#include

#include

#include

#include

#include

#define MAX_ARGS 512

#define MAX_LENGTH 2048

// function prototypes

void handle_sigint(int signo);

void handle_sigstp(int signo);

int main() {

// set up signal handlers for SIGINT and SIGTSTP

struct sigaction sigint_action = {0};

sigint_action.sa_handler = handle_sigint;

sigaction(SIGINT, &sigint_action, NULL);

struct sigaction sigtstp_action = {0};

sigtstp_action.sa_handler = handle_sigstp;

sigaction(SIGTSTP, &sigtstp_action, NULL);

char *input = NULL;

size_t input_size = 0;

char *args[MAX_ARGS];

char input_copy[MAX_LENGTH];

int num_args = 0;

pid_t child_pid;

int child_status;

char *prompt = (getenv("PS1") != NULL) ? getenv("PS1") : "";

while (1) {

// check for changes in the state of child processes

while ((child_pid = waitpid(-1, &child_status, WNOHANG)) > 0) {

if (WIFEXITED(child_status)) {

fprintf(stderr, "Child process %jd done. Exit status %d. ", (intmax_t) child_pid, WEXITSTATUS(child_status));

} else if (WIFSIGNALED(child_status)) {

fprintf(stderr, "Child process %jd done. Signaled %d. ", (intmax_t) child_pid, WTERMSIG(child_status));

} else if (WIFSTOPPED(child_status)) {

kill(child_pid, SIGCONT);

fprintf(stderr, "Child process %jd stopped. Continuing. ", (intmax_t) child_pid);

}

}

// print the prompt

printf("%s", "prompt");

// read a line of input

ssize_t num_chars = getline(&input, &input_size, stdin);

// check for errors or signals during input

if (num_chars == -1 && errno == EINTR) {

clearerr(stdin);

fprintf(stderr, " ");

// check for background processes before printing prompt and resuming input

continue;

}

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

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions