#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#ifdef __unix__
#include <unistd.h>
-#define closesocket(s) close(s)
+#include <fcntl.h>
+#define closesocket close
+#define fcntlsocket fcntl
#endif
+static int handle_control(struct ftp *ftp);
+static int handle_data(struct ftp *ftp, int s);
+static void proc_control(struct ftp *ftp, const char *buf);
+
struct ftp *ftp_alloc(void)
{
struct ftp *ftp;
- if(!(ftp = malloc(sizeof *ftp))) {
+ if(!(ftp = calloc(1, sizeof *ftp))) {
return 0;
}
ftp->ctl = ftp->data = -1;
void ftp_free(struct ftp *ftp)
{
if(!ftp) return;
+
if(ftp->ctl >= 0) {
ftp_close(ftp);
}
ftp->ctl = -1;
return -1;
}
+
+ fcntlsocket(ftp->ctl, F_SETFL, fcntlsocket(ftp->ctl, F_GETFL) | O_NONBLOCK);
+ ftp->num_crecv = 0;
return 0;
}
{
if(ftp->ctl >= 0) {
closesocket(ftp->ctl);
+ ftp->ctl = -1;
+ ftp->num_crecv = 0;
}
if(ftp->data >= 0) {
closesocket(ftp->data);
+ ftp->data = -1;
+ ftp->num_drecv = 0;
+ }
+}
+
+int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize)
+{
+ if(ftp->ctl >= 0 && maxsize) {
+ *sockv++ = ftp->ctl;
+ maxsize--;
}
+ return 1;
+}
+
+int ftp_pending(struct ftp *ftp)
+{
+ int maxfd = -1;
+ fd_set rdset;
+ struct timeval tv = {0, 0};
+
+ FD_ZERO(&rdset);
+ if(ftp->ctl >= 0) {
+ FD_SET(ftp->ctl, &rdset);
+ maxfd = ftp->ctl;
+ }
+ if(ftp->data >= 0) {
+ FD_SET(ftp->data, &rdset);
+ if(ftp->data > maxfd) maxfd = ftp->data;
+ }
+ if(maxfd == -1) return 0;
+
+ return select(maxfd + 1, &rdset, 0, 0, &tv) > 0 ? 1 : 0;
+}
+
+int ftp_handle(struct ftp *ftp, int s)
+{
+ if(s == ftp->ctl) {
+ return handle_control(ftp);
+ }
+ if(s == ftp->data) {
+ return handle_data(ftp, s);
+ }
+ return -1;
+}
+
+static int handle_control(struct ftp *ftp)
+{
+ int i, sz, rd;
+ char *buf, *start, *end;
+
+ while((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;
+ /* assume EWOULDBLOCK, try again next time */
+ return 0;
+ }
+ if(rd == 0) {
+ ftp_close(ftp);
+ return -1;
+ }
+
+ end = start + rd;
+ buf = ftp->crecv;
+ for(i=0; i<rd; i++) {
+ 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);
+ }
+ }
+ return 0;
+}
+
+static int handle_data(struct ftp *ftp, int s)
+{
+ return -1;
+}
+
+static void proc_control(struct ftp *ftp, const char *buf)
+{
}
int ftp_update(struct ftp *ftp)
struct ftp {
int ctl, data; /* sockets */
+ char crecv[256];
+ int num_crecv;
+ char drecv[256];
+ int num_drecv;
+
char *cwd;
struct ftp_dirent *dent;
int num_dent;
int ftp_connect(struct ftp *ftp, const char *host, int port);
void ftp_close(struct ftp *ftp);
+int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize);
+int ftp_pending(struct ftp *ftp);
+
+int ftp_handle(struct ftp *ftp, int s);
+
int ftp_update(struct ftp *ftp);
int ftp_chdir(struct ftp *ftp, const char *dirname);
+#include <signal.h>
+#include <errno.h>
+#include <sys/select.h>
#include "tgfx.h"
#include "input.h"
#include "tui.h"
#include "ftp.h"
+int proc_input(void);
+int keypress(int key);
+
struct ftp *ftp;
struct tui_widget *uilist;
int main(void)
{
- union event ev;
+ int i, numsock, maxfd;
+ int ftpsock[16];
+ fd_set rdset;
+ struct timeval tv;
if(!(ftp = ftp_alloc())) {
return 1;
tui_add_list_item(uilist, "another item");
tui_add_list_item(uilist, "foo");
- tg_setcursor(0, 24);
+ tg_setcursor(0, 23);
tui_draw(uilist);
- while(wait_input(&ev)) {
+ for(;;) {
+ FD_ZERO(&rdset);
+ maxfd = 0;
+
+ numsock = ftp_sockets(ftp, ftpsock, sizeof ftpsock);
+ for(i=0; i<numsock; i++) {
+ FD_SET(ftpsock[i], &rdset);
+ if(ftpsock[i] > maxfd) maxfd = ftpsock[i];
+ }
+
+#ifdef __unix__
+ FD_SET(0, &rdset);
+ tv.tv_sec = 120;
+ tv.tv_usec = 0;
+#else
+ tv.tv_sec = tv.tv_usec = 0;
+#endif
+
+ if(select(maxfd + 1, &rdset, 0, 0, &tv) == -1 && errno == EINTR) {
+ continue;
+ }
+
+#ifdef __unix__
+ if(FD_ISSET(0, &rdset)) {
+ if(proc_input() == -1) {
+ break;
+ }
+ }
+#endif
+
+ for(i=0; i<numsock; i++) {
+ if(FD_ISSET(ftpsock[i], &rdset)) {
+ ftp_handle(ftp, ftpsock[i]);
+ }
+ }
+ }
+
+ tg_cleanup();
+ cleanup_input();
+ ftp_close(ftp);
+ ftp_free(ftp);
+ return 0;
+}
+
+int proc_input(void)
+{
+ union event ev;
+
+ while(poll_input(&ev)) {
switch(ev.type) {
case EV_KEY:
- if(ev.key.key == 27) goto done;
+ if(keypress(ev.key.key) == -1) {
+ return -1;
+ }
break;
default:
break;
}
}
+ return 0;
+}
-done:
- tg_cleanup();
-
- cleanup_input();
+int keypress(int key)
+{
+ switch(key) {
+ case 27:
+ case 'q':
+ return -1;
- ftp_free(ftp);
+ default:
+ break;
+ }
return 0;
}