#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); } pipe_t spawn(wat_t what) { pid_t pid; pipe_t pipefd; pipe((int *) &pipefd); if ((pid = fork()) == 0) { dup2(pipefd.in, STDIN_FILENO); dup2(pipefd.out, STDOUT_FILENO); if (what == COMPRESS) { execl("tr", "tr", "a-z", "A-Z"); } else { execl("tr", "tr", "A-Z", "a-z"); } exit(1); } return pipefd; } int main(int argc, char **argv) { get_ssn(); pipe_t compress_pipes = spawn(COMPRESS); pipe_t decompress_pipes = spawn(DECOMPRESS); dup2(compress_pipes.in, 0); dup2(compress_pipes, 0); if (strstr(argv[0], "server") != NULL) { pipe_t server_pipe = { .in = decompress_pipes.in, .out = compress_pipes.in, }; server(server_pipe); } else { // TODO client } }