foo
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sat, 21 Jan 2023 18:40:22 +0000 (20:40 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sat, 21 Jan 2023 18:40:22 +0000 (20:40 +0200)
src/ftp.c
src/ftp.h
src/main.c

index 04a8ae4..ee39c42 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -621,15 +621,94 @@ static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
+static void free_dirlist(struct ftp_dirent *list)
+{
+       struct ftp_dirent *tmp;
+
+       while(list) {
+               tmp = list;
+               list = list->next;
+
+               free(tmp->name);
+               free(tmp);
+       }
+}
+
+#define SKIP_FIELD(p) \
+       do { \
+               while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \
+               while(*(p) && *(p) != '\n' && isspace(*(p))) (p)++; \
+       } while(0)
+
+static int parse_dirent(struct ftp_dirent *ent, const char *line)
+{
+       int len;
+       const char *ptr = line;
+       const char *end;
+
+       if(!(end = strchr(line, '\r')) && !(end = strchr(line, '\n'))) {
+               return -1;
+       }
+
+       if(line[0] == 'd') {
+               ent->type = FTP_DIR;
+       } else {
+               ent->type = FTP_FILE;
+       }
+
+       SKIP_FIELD(ptr);                /* skip mode */
+       SKIP_FIELD(ptr);                /* skip links */
+       SKIP_FIELD(ptr);                /* skip owner */
+       SKIP_FIELD(ptr);                /* skip group */
+
+       if(ent->type == FTP_FILE) {
+               ent->size = atoi(ptr);
+       }
+       SKIP_FIELD(ptr);                /* skip size */
+       SKIP_FIELD(ptr);                /* skip month */
+       SKIP_FIELD(ptr);                /* skip day */
+       SKIP_FIELD(ptr);                /* skip year */
+
+       if(ptr >= end) return -1;
+
+       len = end - ptr;
+       ent->name = malloc(len + 1);
+       memcpy(ent->name, ptr, len);
+       ent->name[len] = 0;
+
+       infomsg("name: %s\n", ent->name);
+       return 0;
+}
+
 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
 {
        struct recvbuf *rbuf = cls;
 
        if(sz == 0) {
                /* EOF condition, we got the whole list, update directory entries */
-               /* TODO */
-               rbuf->buf[rbuf->size] = 0;
-               infomsg("%s\n", rbuf->buf);
+               char *ptr = rbuf->buf;
+               char *end = rbuf->buf + rbuf->size;
+               struct ftp_dirent *tail = 0;
+               struct ftp_dirent *ent;
+
+               free_dirlist(ftp->dent_rem);
+               ftp->dent_rem = 0;
+
+               while(ptr < end) {
+                       ent = malloc_nf(sizeof *ent);
+                       if(parse_dirent(ent, ptr) != -1) {
+                               ent->next = 0;
+                               if(!tail) {
+                                       ftp->dent_rem = tail = ent;
+                               } else {
+                                       tail->next = ent;
+                                       tail = ent;
+                               }
+                       }
+                       while(*ptr && *ptr != '\n' && *ptr != '\r') ptr++;
+                       while(*ptr && (*ptr == '\r' || *ptr == '\n')) ptr++;
+               }
+               ftp->modified |= FTP_MOD_REMDIR;
 
                free(rbuf->buf);
                free(rbuf);
index e8bcf10..34013c6 100644 (file)
--- a/src/ftp.h
+++ b/src/ftp.h
@@ -20,6 +20,11 @@ enum {
        FTP_CONN_ACT
 };
 
+enum {
+       FTP_MOD_REMDIR  = 0x100,
+       FTP_MOD_LOCDIR  = 0x200
+};
+
 struct ftp_op {
        int op;
        char *arg;
@@ -30,6 +35,9 @@ struct ftp_op {
 struct ftp_dirent {
        char *name;
        int type;
+       long size;
+
+       struct ftp_dirent *next;
 };
 
 struct ftp {
index 89ab3e2..4db0dc3 100644 (file)
@@ -108,6 +108,7 @@ int main(int argc, char **argv)
 
 void updateui(void)
 {
+       struct ftp_dirent *ent;
        unsigned int upd = 0;
 
        if(ftp->curdir_rem && strcmp(tui_get_title(uilist), ftp->curdir_rem) != 0) {
@@ -115,6 +116,17 @@ void updateui(void)
                upd |= 1;
        }
 
+       if(ftp->modified & FTP_MOD_REMDIR) {
+               tui_clear_list(uilist);
+
+               ent = ftp->curdir_rem;
+               while(ent) {
+                       tui_add_list_item(uilist, ent->name);
+                       ent = ent->next;
+               }
+               upd |= 1;
+       }
+
        if(upd & 1) {
                tui_draw(uilist);
        }