summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2021-10-24 01:34:48 -0400
committerRobby Zambito <contact@robbyzambito.me>2021-10-24 01:34:48 -0400
commit8d2460e9f61d63dd14ee95784b6f5d1fd1219395 (patch)
tree1eefdaab6d371b937e1b2d5733647613b03e23d9
parent7380a987dd77c2129d0add9c8ff5238640052724 (diff)
Spawn new shell to run commands in
-rw-r--r--rvsh.c64
1 files 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");
}