#include <arpa/inet.h>
#include <string.h>
#include <sys/time.h>
-#include <sys/stat.h>
#include <errno.h>
#include "splice.h"
-static struct timeval start_time;
-static unsigned long long kb_sent;
-
-static unsigned long mtime_since(struct timeval *s, struct timeval *e)
-{
- double sec, usec;
-
- sec = e->tv_sec - s->tv_sec;
- usec = e->tv_usec - s->tv_usec;
- if (sec > 0 && usec < 0) {
- sec--;
- usec += 1000000;
- }
-
- sec *= (double) 1000;
- usec /= (double) 1000;
-
- return sec + usec;
-}
-
-static unsigned long mtime_since_now(struct timeval *s)
-{
- struct timeval t;
-
- gettimeofday(&t, NULL);
- return mtime_since(s, &t);
-}
-
-void show_rate(int sig)
+static int usage(char *name)
{
- unsigned long msecs = mtime_since_now(&start_time);
-
- if (msecs)
- printf("Throughput: %LuMiB/sec (%Lu KiB in %lu msecs)\n", kb_sent / msecs, kb_sent, msecs);
- else
- printf("Transferred %Lu KiB\n", kb_sent);
+ fprintf(stderr, "%s: target port\n", name);
+ return 1;
}
int main(int argc, char *argv[])
struct sockaddr_in addr;
unsigned short port;
int fd, ret;
- struct stat sb;
- if (argc < 3) {
- printf("%s: target port\n", argv[0]);
- return 1;
- }
+ if (argc < 3)
+ return usage(argv[0]);
- if (fstat(STDIN_FILENO, &sb) < 0)
- return error("stat");
- if (!S_ISFIFO(sb.st_mode)) {
- fprintf(stderr, "stdin must be a pipe\n");
- return 1;
- }
+ if (check_input_pipe())
+ return usage(argv[0]);
port = atoi(argv[2]);
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
return error("connect");
- signal(SIGINT, show_rate);
- gettimeofday(&start_time, NULL);
-
do {
ret = splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, SPLICE_F_NONBLOCK);
if (ret < 0) {
return error("splice");
} else if (!ret)
break;
-
- kb_sent += ret >> 10;
} while (1);
- show_rate(0);
close(fd);
return 0;
}