Last active
October 9, 2015 08:44
-
-
Save mlen/91d3094437be59a98de1 to your computer and use it in GitHub Desktop.
quine generator in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
head = "#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
char *s = "\n#define send_str(sock, str) (send((sock), (str), strlen((str)), 0))\n#define send_chr(sock, chr) (send((sock), (chr), 1, 0))\n\nvoid send_escaped(int sock, char *s) {\n send_str(sock, \"\\\"\");\n while (*s) {\n switch (*s) {\n case '\\n':\n send_str(sock, \"\\\\n\");\n break;\n case '\\\\':\n case '\"':\n send_str(sock, \"\\\\\");\n default:\n send_chr(sock, s);\n }\n s++;\n }\n send_str(sock, \"\\\";\");\n}\n\nvoid handle_connection(int sock) {\n send_str(sock, \"#include <stdlib.h>\\n#include <string.h>\\n#include <stdio.h>\\n#include <unistd.h>\\n#include <sys/socket.h>\\n#include <sys/types.h>\\n#include <netinet/in.h>\\n\\nchar *s = \");\n send_escaped(sock, s);\n send_str(sock, s);\n}\n\nvoid die(char *err) {\n perror(err);\n exit(1);\n}\n\nint main() {\n int fd;\n int sock;\n pid_t pid;\n struct sockaddr_in server;\n struct sockaddr_in client;\n socklen_t client_size = sizeof(client);\n\n bzero(&server, sizeof(server));\n server.sin_family = AF_INET;\n server.sin_port = htons(3000);\n server.sin_addr.s_addr = htonl(INADDR_ANY);\n\n if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {\n die(\"socket()\");\n }\n\n if (bind(fd, (struct sockaddr *)&server, sizeof(server)) < 0) {\n die(\"bind()\");\n }\n\n if (listen(fd, 128) < 0) {\n die(\"listen()\");\n }\n\n while (1) {\n sock = accept(fd, (struct sockaddr *)&client, &client_size);\n if (sock != -1) {\n\n if ((pid = fork()) == -1) {\n die(\"fork()\");\n } else if (pid == 0) {\n handle_connection(sock);\n shutdown(sock, SHUT_RDWR);\n exit(0);\n } else {\n close(sock);\n }\n } else {\n perror(\"accept()\");\n }\n\n waitpid(-1, NULL, WNOHANG);\n }\n\n return 0;\n}\n"; | |
#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, "#include <stdlib.h>\n#include <string.h>\n#include <stdio.h>\n#include <unistd.h>\n#include <sys/socket.h>\n#include <sys/types.h>\n#include <netinet/in.h>\n\nchar *s = "); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment