fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #include <netdb.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10.  
  11. enum {
  12. NPOSTS = 2,
  13. TIMEOUT = 5
  14. };
  15.  
  16. FILE *dial(const char *host, const char *port);
  17.  
  18. char **tokenise(const char *s, const char *delim);
  19.  
  20. size_t count_tokens(const char *s, const char *delim);
  21.  
  22. char *nth_token(char *to, size_t n, const char *s, const char *delim, size_t i);
  23.  
  24. void free_tokens(char **a);
  25.  
  26. static char buffer[2000000];
  27.  
  28. void post(const char *thread, const char *name, const char *email,
  29. const char *comment)
  30. {
  31. FILE *iop;
  32.  
  33. if (!(iop = dial("dis.4chan.org", "80"))) {
  34. fputs("error: unable to connect to server\n", stderr);
  35. return;
  36. }
  37. sprintf(buffer, "bbs=prog&id=%s&shiichan=proper2&"
  38. "kotehan=%s&meiru=%s&com=%s&email=%%27", thread, name, email,
  39. comment);
  40. fprintf(iop,
  41. "POST /post HTTP/1.1\r\n"
  42. "Host: dis.4chan.org\r\n"
  43. "Connection: close\r\n"
  44. "Content-Type: application/x-www-form-urlencoded\r\n"
  45. "Content-Length: %lu\r\n\r\n%s",
  46. (unsigned long int) strlen(buffer), buffer);
  47. fflush(iop);
  48. getc(iop);
  49. fclose(iop);
  50. puts(thread);
  51. }
  52.  
  53. char *get(const char *page)
  54. {
  55. FILE *iop;
  56. size_t n;
  57.  
  58. puts("Connecting to server ...");
  59. if (!(iop = dial("dis.4chan.org", "80"))) {
  60. fputs("error: unable to connect to server\n", stderr);
  61. exit(EXIT_FAILURE);
  62. }
  63. fprintf(iop,
  64. "GET %s HTTP/1.1\r\n"
  65. "Host: dis.4chan.org\r\n"
  66. "Connection: close\r\n\r\n", page);
  67. fflush(iop);
  68. puts("Downloading thread list ...");
  69. n = fread(buffer, 1, sizeof buffer, iop);
  70. buffer[n] = '\0';
  71. fclose(iop);
  72. return buffer;
  73. }
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77. char buf[1024];
  78. size_t n, i;
  79. char **a;
  80.  
  81. srand(time(0));
  82. if (!(a = tokenise(get("/prog/subject.txt"), "\n"))) {
  83. fputs("memory error\n", stderr);
  84. return EXIT_FAILURE;
  85. }
  86. for (n = i = 0; (a[n] = a[i]); i++)
  87. if (count_tokens(a[i], "<>") == 7)
  88. n++;
  89. puts("Starting ...");
  90. for (i = 0;;) {
  91. post(nth_token(buf, sizeof buf, a[rand() % n],
  92. "<>", 3), "James+Gosling", "",
  93. "GAWWZMACS+FLABBERGASTS+MY+AUDIENCE");
  94. if (++i == NPOSTS)
  95. break;
  96. sleep(TIMEOUT);
  97. }
  98. free_tokens(a);
  99. return 0;
  100. }
  101.  
  102. /* -------------------------------------------------------------------------- */
  103.  
  104. char *nth_token(char *to, size_t n, const char *s, const char *delim, size_t i)
  105. {
  106. size_t dsize = strlen(delim);
  107. char *save = to;
  108.  
  109. while (i--) {
  110. if (!(s = strstr(s, delim)))
  111. return 0;
  112. s += dsize;
  113. }
  114. while (--n && (*to = *s) && strncmp(s++, delim, dsize))
  115. to++;
  116. *to = '\0';
  117. return save;
  118. }
  119.  
  120. size_t count_tokens(const char *s, const char *delim)
  121. {
  122. size_t dsize = strlen(delim), n = 0;
  123.  
  124. while (*s) {
  125. n++;
  126. while (*s && strncmp(s, delim, dsize))
  127. s++;
  128. if (!*s)
  129. break;
  130. s += dsize;
  131. }
  132. return n;
  133. }
  134.  
  135. void free_tokens(char **a)
  136. {
  137. free(*--a);
  138. free(a);
  139. }
  140.  
  141. static char **make_tokens(char **to, size_t n, char *s, const char *delim)
  142. {
  143. char **save = to, **endp = to + n - 1;
  144. size_t dsize = strlen(delim);
  145.  
  146. while (*s) {
  147. if (to < endp)
  148. *to++ = s;
  149. while (*s && strncmp(s, delim, dsize))
  150. s++;
  151. if (!*s)
  152. break;
  153. *s = '\0';
  154. s += dsize;
  155. }
  156. *to = 0;
  157. return save;
  158. }
  159.  
  160. char **tokenise(const char *s, const char *delim)
  161. {
  162. size_t n = count_tokens(s, delim) + 1;
  163. char **r;
  164.  
  165. if (!(r = malloc((n + 1) * sizeof *r)))
  166. return 0;
  167. if (!(*r = malloc(strlen(s) + 1))) {
  168. free(r);
  169. return 0;
  170. }
  171. return make_tokens(r + 1, n, strcpy(*r, s), delim);
  172. }
  173.  
  174. FILE *dial(const char *host, const char *port)
  175. {
  176. struct addrinfo hints;
  177. struct addrinfo *info, *ip;
  178. int fd;
  179.  
  180. memset(&hints, 0, sizeof hints);
  181. hints.ai_socktype = SOCK_STREAM;
  182. hints.ai_family = AF_UNSPEC;
  183. if (getaddrinfo(host, port, &hints, &info))
  184. return 0;
  185. for (ip = info; ip; ip = ip->ai_next) {
  186. if ((fd = socket(ip->ai_family, ip->ai_socktype,
  187. ip->ai_protocol)) == -1)
  188. continue;
  189. if (!connect(fd, ip->ai_addr, ip->ai_addrlen))
  190. break;
  191. close(fd);
  192. }
  193. freeaddrinfo(info);
  194. return !ip ? 0 : fdopen(fd, "r+");
  195. }
Runtime error #stdin #stdout 0s 4028KB
stdin
Standard input is empty
stdout
Connecting to server ...