Fix various compile warnings
[splice.git] / splice-fromnet.c
index d50014b7f13ab6f9d6c1a4dd3054d454bb418b71..7493e5b0308a871e7cc5df24819afa99edb7ee7a 100644 (file)
 
 #include "splice.h"
 
+static unsigned int splice_size = SPLICE_SIZE;
+static unsigned int splice_flags;
+static int wait_for_poll;
+
 static int usage(char *name)
 {
-       fprintf(stderr, "%s: port\n", name);
+       fprintf(stderr, "%s: [-s splice size] [-w wait for poll] [-n non-blocking] port\n", name);
        return 1;
 }
 
 static int splice_from_net(int fd)
 {
        while (1) {
-               struct pollfd pfd = {
-                       .fd = fd,
-                       .events = POLLIN,
-               };
                int ret;
 
-               ret = poll(&pfd, 1, -1);
-               if (ret < 0)
-                       return error("poll");
-               else if (!ret)
-                       continue;
+               if (wait_for_poll) {
+                       struct pollfd pfd = {
+                               .fd = fd,
+                               .events = POLLIN,
+                       };
 
-               if (!(pfd.revents & POLLIN))
-                       continue;
+                       ret = poll(&pfd, 1, -1);
+                       if (ret < 0)
+                               return error("poll");
+                       else if (!ret)
+                               continue;
+
+                       if (!(pfd.revents & POLLIN))
+                               continue;
+               }
 
-               ret = ssplice(fd, NULL, STDOUT_FILENO, NULL, SPLICE_SIZE, 0);
+               ret = ssplice(fd, NULL, STDOUT_FILENO, NULL, splice_size, 0);
 
                if (ret < 0)
                        return error("splice");
@@ -78,11 +85,38 @@ static int get_connect(int fd, struct sockaddr_in *addr)
        return connfd;
 }
 
+static int parse_options(int argc, char *argv[])
+{
+       int c, index = 1;
+
+       while ((c = getopt(argc, argv, "s:w:n")) != -1) {
+               switch (c) {
+               case 's':
+                       splice_size = atoi(optarg);
+                       index++;
+                       break;
+               case 'w':
+                       wait_for_poll = atoi(optarg);
+                       index++;
+                       break;
+               case 'n':
+                       splice_flags |= SPLICE_F_NONBLOCK;
+                       index++;
+                       break;
+               default:
+                       return -1;
+               }
+       }
+
+       return index;
+}
+
+
 int main(int argc, char *argv[])
 {
        struct sockaddr_in addr;
        unsigned short port;
-       int connfd, opt, fd;
+       int connfd, opt, fd, index;
 
        if (argc < 2)
                return usage(argv[0]);
@@ -90,7 +124,11 @@ int main(int argc, char *argv[])
        if (check_output_pipe())
                return usage(argv[0]);
 
-       port = atoi(argv[1]);
+       index = parse_options(argc, argv);
+       if (index == -1 || index + 1 > argc)
+               return usage(argv[0]);
+
+       port = atoi(argv[index]);
 
        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (fd < 0)