commit 28f1dab416f6ebc2a1faff0232af8505807eba1b
parent 1b31e362714dffa38730568e810aaebdc221ce65
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 20 Dec 2020 18:37:17 -0800
Poll for response
Diffstat:
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/main.c b/main.c
@@ -24,6 +24,7 @@
#include <errno.h>
#include <limits.h>
#include <netdb.h>
+#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -31,11 +32,11 @@
#define LEN(X) (sizeof(X) / sizeof(*X))
-#define PROTO_MAX 16
#define BUFSIZE 4096
#define ARGV_MAX 16
#define ARG_SEP ":\t "
+static int timeout = 5 * 1000;
static char *gphfmt[] = { "gophmt", NULL };
int gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port);
@@ -193,8 +194,20 @@ fetch(int sock, const char *path)
{
FILE *fp;
char buf[BUFSIZE];
- ssize_t bytes;
int error;
+ ssize_t bytes;
+ struct pollfd pfd;
+
+ pfd.fd = sock;
+ pfd.events = POLLRDNORM;
+ switch (poll(&pfd, 1, timeout)) {
+ case -1:
+ warn("poll");
+ return 1;
+ case 0:
+ warn("timeout");
+ return 1;
+ }
fp = wfopen(path, "w+");
if (fp == NULL)