knetfile.c
[samtools.git] / knetfile.c
index 1e2c042701de0fec88212f563ff4862da6fcad93..d3e628371270fffe07f44de0af31e30c7479a959 100644 (file)
@@ -1,6 +1,7 @@
 /* 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>
@@ -90,7 +89,7 @@ static int socket_connect(const char *host, const char *port)
 
        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;
@@ -234,7 +233,10 @@ static int kftp_get_response(knetFile *ftp)
 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;
 }
 
@@ -413,7 +415,10 @@ int khttp_connect_file(knetFile *fp)
        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)