fixed bug in line-buffered receive routine
[oftp] / src / ftp.c
index ee39c42..ec6dbab 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -20,7 +20,7 @@
 #define fcntlsocket            fcntl
 #endif
 
-#define TIMEOUT        5
+#define TIMEOUT        15
 
 static int newconn(struct ftp *ftp);
 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
@@ -187,7 +187,7 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg)
 {
        struct ftp_op *fop;
 
-       if(!ftp->qhead) {
+       if(!ftp->busy && !ftp->qhead) {
                exec_op(ftp, op, arg);
                return 0;
        }
@@ -195,15 +195,23 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg)
        if(!(fop = malloc(sizeof *fop))) {
                return -1;
        }
-       if(!(fop->arg = strdup(arg))) {
-               free(fop);
-               return -1;
+       if(arg) {
+               if(!(fop->arg = strdup(arg))) {
+                       free(fop);
+                       return -1;
+               }
+       } else {
+               fop->arg = 0;
        }
        fop->op = op;
        fop->next = 0;
 
-       ftp->qtail->next = fop;
-       ftp->qtail = fop;
+       if(ftp->qhead) {
+               ftp->qtail->next = fop;
+               ftp->qtail = fop;
+       } else {
+               ftp->qhead = ftp->qtail = fop;
+       }
        return 0;
 }
 
@@ -330,6 +338,8 @@ static int sendcmd(struct ftp *ftp, const char *fmt, ...)
                return -1;
        }
 
+       ftp->busy = 1;
+
        va_start(ap, fmt);
        vsprintf(buf, fmt, ap);
        va_end(ap);
@@ -343,7 +353,13 @@ static int handle_control(struct ftp *ftp)
        int i, sz, rd;
        char *buf, *start, *end;
 
-       while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) {
+       for(;;) {
+               if((sz = sizeof ftp->crecv - ftp->num_crecv) <= 0) {
+                       /* discard buffer */
+                       warnmsg("discard buffer\n");
+                       sz = sizeof ftp->crecv;
+                       ftp->num_crecv = 0;
+               }
                start = ftp->crecv + ftp->num_crecv;
                if((rd = recv(ftp->ctl, start, sz, 0)) == -1) {
                        if(errno == EINTR) continue;
@@ -358,7 +374,9 @@ static int handle_control(struct ftp *ftp)
                end = start + rd;
                buf = ftp->crecv;
                for(i=0; i<rd; i++) {
-                       if(start[i] == '\n') {
+                       if(start[i] == '\r') {
+                               start[i] = 0;
+                       } else if(start[i] == '\n') {
                                start[i] = 0;
                                proc_control(ftp, buf);
                                buf = start + i + 1;
@@ -367,6 +385,8 @@ static int handle_control(struct ftp *ftp)
                if(buf != ftp->crecv && buf < end) {
                        ftp->num_crecv = end - buf;
                        memmove(ftp->crecv, buf, ftp->num_crecv);
+               } else {
+                       ftp->num_crecv = 0;
                }
        }
        return 0;
@@ -443,12 +463,14 @@ static void proc_control(struct ftp *ftp, const char *buf)
        if(ftp->cproc) {
                if(ftp->cproc(ftp, code, buf, ftp->cproc_cls) <= 0) {
                        ftp->cproc = 0;
+                       ftp->busy = 0;
 
                        /* execute next operation if there's one queued */
                        exec_queued(ftp);
                }
                return;
        }
+       ftp->busy = 0;
 
        switch(code) {
        case 220:
@@ -676,7 +698,6 @@ static int parse_dirent(struct ftp_dirent *ent, const char *line)
        memcpy(ent->name, ptr, len);
        ent->name[len] = 0;
 
-       infomsg("name: %s\n", ent->name);
        return 0;
 }
 
@@ -705,8 +726,8 @@ static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
                                        tail = ent;
                                }
                        }
-                       while(*ptr && *ptr != '\n' && *ptr != '\r') ptr++;
-                       while(*ptr && (*ptr == '\r' || *ptr == '\n')) ptr++;
+                       while(ptr < end && *ptr != '\n' && *ptr != '\r') ptr++;
+                       while(ptr < end && (*ptr == '\r' || *ptr == '\n')) ptr++;
                }
                ftp->modified |= FTP_MOD_REMDIR;