diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2022-11-06 13:18:47 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2022-11-06 22:33:19 +0200 |
commit | 1edc8b0525af34ad1030213a90086cd941056b8a (patch) | |
tree | 520bb986376af88e4df5dcb6dd167df597f3f538 /client.c | |
parent | d1a20f427a13bdeaaa84f51095f3e92767bed388 (diff) | |
download | tcp-1edc8b0525af34ad1030213a90086cd941056b8a.tar.gz |
Support FreeBSD
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 30 |
1 files changed, 27 insertions, 3 deletions
@@ -10,12 +10,15 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/sendfile.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> +#ifdef HAVE_SYS_SENDFILE_H +#include <sys/sendfile.h> +#endif + #include "utils.h" const char *progname = NULL; @@ -29,10 +32,31 @@ usage () printf ("Default port is %s\n", port); } +static ssize_t +transmit (int fd, int sfd, size_t size) +{ +#if HAVE_SENDFILE +#if HAVE_STRUCT_SF_HDTR_HDR_CNT + int rc; + off_t sbytes; + rc = sendfile (sfd, fd, 0, size, (struct sf_hdtr *) NULL, &sbytes, 0); + if (rc < 0) + return rc; + return sbytes; +#else + int rc; + off_t dummy = 0; /* specially for Solaris (it crashes if sendfile's 3rd arg is NULL). */ + rc = sendfile (fd, sfd, &dummy, size); + return rc; +#endif +#else /* no HAVE_SENDFILE */ +#error need sendfile() +#endif +} + static void send_file (int fd, const char *filename) { - off_t dummy = 0; /* specially for Solaris (it crashes if sendfile's 3rd arg is NULL). */ int sfd = open (filename, O_RDONLY); if (sfd < 0) fatal ("cannot read `%s'", filename); @@ -47,7 +71,7 @@ send_file (int fd, const char *filename) rc = write (fd, name, filename_len); /* filename\0payload */ if (rc != filename_len) fatal ("failed to write filename"); - rc = sendfile (fd, sfd, &dummy, st.st_size); + rc = transmit (fd, sfd, st.st_size); if (rc < 0) fatal ("failed to send file `%s'", filename); else if (rc != st.st_size) |