#include #include #include #include #include char* getoutput(const char *command){ int pipe_ends[2]; if(pipe(pipe_ends)) return 0; size_t read_so_far = 0; ssize_t readlen; size_t space_left = 250; char *buffer = (char*)malloc(250); if(fork()) { // Then this is the parent process close(pipe_ends[1]); while(1){ readlen = read(pipe_ends[0], buffer + read_so_far, space_left); if(readlen <= 0) break; read_so_far += readlen; space_left -= readlen; if(!space_left) { buffer = (char*)realloc(buffer, read_so_far + 250); space_left += 250; } } wait(0); } else { // Child process dup2(pipe_ends[1], 1); execl("/bin/sh", "sh", "-c", command); perror("exec"); return 0; } return buffer; } char **split(char *tosplit, size_t *line_count){ size_t length = strlen(tosplit); char **lines = (char**)malloc(100 * sizeof(char*)); size_t line = 0; size_t lines_space_left = 100; lines[0] = tosplit; for(size_t i = 0; i < length; i++){ if(tosplit[i] == '\n'){ line++; tosplit[i] = 0; lines[line] = &tosplit[i] + 1; // If we have an extra one, revise this lines_space_left -= 1; } if(!lines_space_left){ lines = (char**)realloc(lines, (line + 100) * sizeof(char*)); lines_space_left += 100; } } *line_count = line; return lines; }