diff options
author | Joern Engel <joern@logfs.org> | 2011-12-22 17:01:06 -0800 |
---|---|---|
committer | Joern Engel <joern@logfs.org> | 2011-12-22 17:01:06 -0800 |
commit | 4ef015343068951dcaee0cd8c50a83841710f84f (patch) | |
tree | b782e39165be3e17158cf3cfcfc669003abc4287 | |
parent | 0597b18d8b043a473731cf99c7b1c01470ba3858 (diff) | |
download | cancd-4ef015343068951dcaee0cd8c50a83841710f84f.tar.gz |
Factor out do_write
A good candidate for a libc function, if ever there was one.
Signed-off-by: Joern Engel <joern@logfs.org>
-rw-r--r-- | cancd.c | 29 |
1 files changed, 17 insertions, 12 deletions
@@ -279,9 +279,24 @@ static int open_socket() return 0; } +static int do_write(int fd, const void *buf, size_t count) +{ + ssize_t ret; + + do { + ret = write(fd, buf, count); + if (ret < 0 && errno == EINTR) + continue; + if (ret < 0) + return ret; + count -= ret; + } while (count); + return 0; +} + static void do_output(const char *buf, int len, struct sockaddr_in *addr, socklen_t socklen) { - int fd, rc, tot; + int fd, rc; char *name, *tmp, *dir; name = get_path(&addr->sin_addr); @@ -305,17 +320,7 @@ static void do_output(const char *buf, int len, struct sockaddr_in *addr, sockle if (fd < 0) syslog(LOG_ERR, "Unable to open \"%s\": %s", name, strerror(errno)); else { - tot = 0; - while (tot < len) { - rc = write(fd, buf + tot, len - tot); - if (rc < 0) { - if (errno == EINTR) - continue; - syslog(LOG_ERR, "Error writing to \"%s\": %s", name, strerror(errno)); - break; - } - tot += rc; - } + do_write(fd, buf, len); close(fd); } free(name); |