head = "#include #include #include #include #include #include #include char *s = " tail = <<'EOF'.sub('REPLACE_ME', head.inspect) #define send_str(sock, str) (send((sock), (str), strlen((str)), 0)) #define send_chr(sock, chr) (send((sock), (chr), 1, 0)) void send_escaped(int sock, char *s) { send_str(sock, "\""); while (*s) { switch (*s) { case '\n': send_str(sock, "\\n"); break; case '\\': case '"': send_str(sock, "\\"); default: send_chr(sock, s); } s++; } send_str(sock, "\";"); } void handle_connection(int sock) { send_str(sock, REPLACE_ME); send_escaped(sock, s); send_str(sock, s); } void die(char *err) { perror(err); exit(1); } int main() { int fd; int sock; pid_t pid; struct sockaddr_in server; struct sockaddr_in client; socklen_t client_size = sizeof(client); bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(3000); server.sin_addr.s_addr = htonl(INADDR_ANY); if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { die("socket()"); } if (bind(fd, (struct sockaddr *)&server, sizeof(server)) < 0) { die("bind()"); } if (listen(fd, 128) < 0) { die("listen()"); } while (1) { sock = accept(fd, (struct sockaddr *)&client, &client_size); if (sock != -1) { if ((pid = fork()) == -1) { die("fork()"); } else if (pid == 0) { handle_connection(sock); shutdown(sock, SHUT_RDWR); exit(0); } else { close(sock); } } else { perror("accept()"); } waitpid(-1, NULL, WNOHANG); } return 0; } EOF print head + (tail.inspect + ";") + tail