Browse Source

Fixed bugs in pipes and string library

master
alistair 4 years ago
parent
commit
46284680e1
  1. 43
      main.c
  2. 1
      run.c
  3. 11
      util.c

43
main.c

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#include "run.h"
#include "main.h"
#include "util.h"
#include <err.h>
#include <dirent.h>
#include <string.h>
@ -17,6 +18,8 @@ int run_command(State *state, int argc, char *argv[]); @@ -17,6 +18,8 @@ int run_command(State *state, int argc, char *argv[]);
int get_array_element(ArrayElement **ret, GrowingArray* array, char* key);
int del_array_element(GrowingArray* array, char* key);
const int GLOB_DEBUG;
GrowingArray get_growing_array(size_t num_elements, size_t size_elem) {
GrowingArray arr;
arr.inner = calloc(num_elements, size_elem);
@ -206,8 +209,11 @@ void shutdown(State *state) { @@ -206,8 +209,11 @@ void shutdown(State *state) {
}
int run_pipes(State *state, int num_args, char *args []) {
char *joined;
char *joined = 0;
int err = join_string(&joined, num_args, args, " ");
if (err) {
errx(1, "no");
}
char * separated_by_pipe;
int pipe_count = 0;
@ -216,6 +222,7 @@ int run_pipes(State *state, int num_args, char *args []) { @@ -216,6 +222,7 @@ int run_pipes(State *state, int num_args, char *args []) {
pipe_count++;
}
}
int num_commands = pipe_count + 1;
char **sep_commands = calloc(pipe_count + 1, sizeof(char *));
@ -229,22 +236,13 @@ int run_pipes(State *state, int num_args, char *args []) { @@ -229,22 +236,13 @@ int run_pipes(State *state, int num_args, char *args []) {
i++;
} while ((piece = (char *)strtok(NULL, "|")) != 0);
if (num_commands == 1) {
return ER_NOT_EXISTS;
}
/*int pipein[2];*/
/*int pipeout[2];*/
/*memset(pipein, 0, sizeof(int) * 2);*/
/*memset(pipeout, 0, sizeof(int) * 2);*/
/*pipe(pipein);*/
/*pipe(pipeout);*/
// 0 is read, 1 is write
int *pipes = calloc(pipe_count * 2, sizeof(int));
for (int i = 0; i < num_commands; i++) {
for (int i = 0; i < pipe_count; i++) {
pipe(pipes + (2 * i));
}
@ -253,14 +251,6 @@ int run_pipes(State *state, int num_args, char *args []) { @@ -253,14 +251,6 @@ int run_pipes(State *state, int num_args, char *args []) {
int in;
int out;
/*dup2(pipes[pipe_count*2+1], 1);*/
/*dup2(pipes[0], 0);*/
// read = 0
// write = 1
//int split_string(char **sep_string[], int *num, char *string, char delim) {
for (int i = 0; i < num_commands; i++) {
char **command;
int words;
@ -279,13 +269,22 @@ int run_pipes(State *state, int num_args, char *args []) { @@ -279,13 +269,22 @@ int run_pipes(State *state, int num_args, char *args []) {
printf("in %d out: %d exec: ", in, out);
for (int i = 0; i < words; i++) {
printf("%s ", command[i]);
}
printf("\n");
execute(in, out, command, &child);
}
int status;
while (!wait(&status));
free(pipes);
for (int i = 0; i < num_commands; i++) {
free(sep_commands[i]);
}
free(sep_commands);
free(joined);
return ER_SUCCESS;
}
@ -389,6 +388,7 @@ int check_builtins(State *state, int num_args, char *args[]) { @@ -389,6 +388,7 @@ int check_builtins(State *state, int num_args, char *args[]) {
char **split;
int num;
err = split_string(&split, &num, joined, '=');
printf("%s\n", joined);
if (num == 2) {
char * key = split[0];
char * value = split[1];
@ -567,6 +567,9 @@ int repl(State* state, FILE *input) { @@ -567,6 +567,9 @@ int repl(State* state, FILE *input) {
split_string(&sep_string, &num_words, line, ' ');
if (num_words == 0)
continue;
// int err = check_pipes(state, num_words, sep_string);
int err = run_pipes(state, num_words, sep_string);

1
run.c

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
#include "run.h"
#include <sys/wait.h>
struct Job {
bool done;

11
util.c

@ -65,7 +65,7 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) { @@ -65,7 +65,7 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) {
length += len;
if (sep_stirng[i+1] == 0) {
if (i != (arr_len - 1) && sep_stirng[i+1] == 0) {
*joined = new_string;
return ER_SUCCESS;
} else {
@ -75,6 +75,8 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) { @@ -75,6 +75,8 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) {
length += delim_len;
}
}
*joined = new_string;
return ER_SUCCESS;
return ER_FAILURE;
}
@ -117,6 +119,7 @@ int strip_char(char *string, char del) { @@ -117,6 +119,7 @@ int strip_char(char *string, char del) {
*/
int split_string(char **sep_string[], int *num, char *string, char delim) {
char **word_list = calloc(10, sizeof(char*));
int word_list_size = 10;
int word_size;
char *word = calloc((word_size=INITIAL_WORD_SIZE), sizeof(char));
@ -124,6 +127,12 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { @@ -124,6 +127,12 @@ int split_string(char **sep_string[], int *num, char *string, char delim) {
int word_length = 0;
for (int i = 0; true; i++) {
if (word_list_size - 1 == num_words) {
word_list_size += 1;
word_list = reallocarray(word_list, word_list_size, sizeof(char*));
}
if (string[i] == '\0') {
if (word_length) {
word_list[num_words] = word;

Loading…
Cancel
Save