#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, ...);
{
struct ftp_op *fop;
- if(!ftp->qhead) {
+ if(!ftp->busy && !ftp->qhead) {
exec_op(ftp, op, arg);
return 0;
}
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;
}
return -1;
}
+ ftp->busy = 1;
+
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
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;
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;
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;
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:
memcpy(ent->name, ptr, len);
ent->name[len] = 0;
- infomsg("name: %s\n", ent->name);
return 0;
}
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;