#include #include #include #include #include /* global because fuck you */ static char *ssn = NULL; #define SSN_LEN 11 typedef enum { COMPRESS, DECOMPRESS, } wat_t; typedef struct { int in; int out; } pipe_t; void get_ssn() { regex_t r; regcomp(&r, "[0-9]{3}-[0-9]{2}-[0-9]{4}", REG_EXTENDED | REG_NOSUB); if (!(ssn = getenv("SSN")) || regexec(&r, ssn, 0, NULL, 0)) { /* die here */ abort(); } } void server(pipe_t pipe) { write(pipe.out, "HI", 2); write(pipe.out, ssn, SSN_LEN); splice(pipe.out, NULL, pipe.in, NULL, 1024, 0); } pipe_t spawn(wat_t what) { pipe_t pipe_result, pipe_process_in, pipe_process_out; pid_t pid; pipe((int *) &pipe_process_in); pipe((int *) &pipe_process_out); pipe_result.in = pipe_process_in.in; pipe_result.out = pipe_process_out.out; if ((pid = fork()) == 0) { dup2(STDIN_FILENO, pipe_process_in.out); dup2(STDOUT_FILENO, pipe_process_out.in); if (what == COMPRESS) { execl("/bin/tr", "tr", "a-z", "A-Z"); } else { execl("/bin/tr", "tr", "A-Z", "a-z"); } exit(1); } return pipe_result; } int main(int argc, char **argv) { get_ssn(); fprintf(stderr, "starting\n"); pipe_t compress_pipes = spawn(COMPRESS); fprintf(stderr, "compress pipes: %d, %d\n", compress_pipes.in, compress_pipes.out); pipe_t decompress_pipes = spawn(DECOMPRESS); fprintf(stderr, "decompress pipes: %d, %d\n", decompress_pipes.in, decompress_pipes.out); dup2(compress_pipes.out, STDOUT_FILENO); dup2(decompress_pipes.in, STDIN_FILENO); if (strstr(argv[0], "server") != NULL) { pipe_t server_pipe = { .in = decompress_pipes.out, .out = compress_pipes.in, }; fprintf(stderr, "server pipe: %d, %d\n", server_pipe.in, server_pipe.out); server(server_pipe); } else { // TODO client } }