From 8d2460e9f61d63dd14ee95784b6f5d1fd1219395 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 24 Oct 2021 01:34:48 -0400 Subject: Spawn new shell to run commands in --- rvsh.c | 64 ++++++++++++++-------------------------------------------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/rvsh.c b/rvsh.c index 423e66c..db2ddd5 100644 --- a/rvsh.c +++ b/rvsh.c @@ -123,46 +123,7 @@ void put_stream_rev(FILE *stream) { } } -/*void run_reverse_command(char *command) {*/ -/*pid_t bash_pid;*/ -/*union pipe bash_input, bash_output;*/ -/*FILE *bash_input_f, *bash_output_f;*/ - -/*pipe(bash_input.filedes);*/ -/*pipe(bash_output.filedes);*/ - -/*bash_pid = fork();*/ -/*if (bash_pid == -1) {*/ -/*exit(EXIT_FAILURE);*/ -/*}*/ - -/*if (bash_pid == 0) {*/ -/*close(bash_input.write_end);*/ -/*close(bash_output.read_end);*/ - -/*dup2(bash_input.read_end, STDIN_FILENO);*/ -/*dup2(bash_output.write_end, STDOUT_FILENO);*/ -/*[>close(STDERR_FILENO);<]*/ - -/*execlp("bash", "bash", "-c", command, NULL);*/ -/*}*/ - -/*close(bash_input.read_end);*/ -/*close(bash_output.write_end);*/ - -/*bash_input_f = fdopen(bash_input.write_end, "w");*/ -/*bash_output_f = fdopen(bash_output.read_end, "r");*/ - -/*put_stream_rev(bash_output_f);*/ - -/*fclose(bash_input_f);*/ -/*fclose(bash_output_f);*/ -/*}*/ - -int main(void) { - char *prompt; - char *line; - +void run_reverse_command(char *command) { pid_t bash_pid; union pipe bash_input, bash_output; FILE *bash_input_f, *bash_output_f; @@ -170,9 +131,6 @@ int main(void) { pipe(bash_input.filedes); pipe(bash_output.filedes); - bash_output_f = fdopen(bash_output.read_end, "r"); - bash_input_f = fdopen(bash_input.write_end, "w"); - bash_pid = fork(); if (bash_pid == -1) { exit(EXIT_FAILURE); @@ -186,14 +144,25 @@ int main(void) { dup2(bash_output.write_end, STDOUT_FILENO); /*close(STDERR_FILENO);*/ - execlp("bash", "bash", NULL); + execlp("bash", "bash", "-c", command, NULL); } close(bash_input.read_end); close(bash_output.write_end); + bash_input_f = fdopen(bash_input.write_end, "w"); + bash_output_f = fdopen(bash_output.read_end, "r"); + put_stream_rev(bash_output_f); + fclose(bash_input_f); + fclose(bash_output_f); +} + +int main(void) { + char *prompt; + char *line; + using_history(); while ((line = readline(strrev(prompt = get_bash_prompt()))) != NULL) { add_history(line); @@ -201,16 +170,11 @@ int main(void) { strrev(line); /*puts(line);*/ - /*run_reverse_command(line);*/ - - fputs(line, bash_input_f); + run_reverse_command(line); free(line); } - fclose(bash_input_f); - fclose(bash_output_f); - if (errno != 0) { perror("End"); } -- cgit v1.2.3