/* The MIT License
- Copyright (c) 2008 Genome Research Ltd (GRL).
+ Copyright (c) 2008 by Genome Research Ltd (GRL).
+ 2010 by Attractive Chaos <attractor@live.co.uk>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
SOFTWARE.
*/
-/* Contact: Heng Li <lh3@sanger.ac.uk> */
-
/* Probably I will not do socket programming in the next few years and
therefore I decide to heavily annotate this file, for Linux and
- Windows as well. -lh3 */
+ Windows as well. -ac */
#include <time.h>
#include <stdio.h>
int on = 1, fd;
struct linger lng = { 0, 0 };
- struct addrinfo hints, *res;
+ struct addrinfo hints, *res = 0;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get)
{
if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing
- netwrite(ftp->ctrl_fd, cmd, strlen(cmd));
+ if (netwrite(ftp->ctrl_fd, cmd, strlen(cmd)) < 0) {
+ fprintf(stderr, "[%s] error sending command to socket.\n", __func__);
+ return -1;
+ }
return is_get? kftp_get_response(ftp) : 0;
}
l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
l += sprintf(buf + l, "\r\n");
- netwrite(fp->fd, buf, l);
+ if (netwrite(fp->fd, buf, l) < 0) {
+ fprintf(stderr, "[%s] fail write GET line.\n", __func__);
+ return -1;
+ }
l = 0;
while (netread(fp->fd, buf + l, 1)) { // read HTTP header; FIXME: bad efficiency
if (buf[l] == '\n' && l >= 3)
if (fp->type == KNF_TYPE_LOCAL) { // on Windows, the following block is necessary; not on UNIX
off_t rest = len, curr;
while (rest) {
- curr = read(fp->fd, buf + l, rest);
+ do {
+ curr = read(fp->fd, buf + l, rest);
+ } while (curr < 0 && EINTR == errno);
+ if (curr < 0) return -1;
if (curr == 0) break;
l += curr; rest -= curr;
}