#include #include #include #include #include #include #include #include #include #include char buffer[1024*128]; int pipe_ends[2]; int read_end; int write_end; void handle_signal(int signal){ char* message = "Caught the signal!\n"; write(2, message, strlen(message)); } void* run_system_in_other_thread(void* cmd){ system(cmd); // Into the pipe here kill(getpid(), 13); close(write_end); close(read_end); return 0; } char* getoutput(char* cmd){ int backup_stdout = dup(1); pipe(pipe_ends); read_end = pipe_ends[0]; write_end = pipe_ends[1]; dup2(write_end, 1); pthread_t T; // Our reference to our thread, so we can kill it or whatever pthread_create(&T, 0, run_system_in_other_thread, cmd); size_t totallen = 0; while(1){ size_t readlen = read(read_end, buffer + totallen, 1024*128 - totallen); // Out of the pipe here totallen += readlen; if(readlen < 1) break; } buffer[totallen] = 0; dup2(backup_stdout, 1); return buffer; // return "fish"; // ['f', 'i', 's', 'h', 0] } int main(){ struct sigaction handler; handler.sa_handler = handle_signal; sigaction(13, &handler, 0); // A char* is not the same as a char char a_letter = 'a'; char *text = getoutput("last"); printf("Size of char: %d\nSize of char*: %d\n", sizeof(a_letter), sizeof(text)); printf("Length of text: %d\n", strlen(text)); puts(text); printf("Address of text: %lx (%lu)\n", text, text); return 0; }