]> git.mdlowis.com Git - archive/afm.git/commitdiff
reimplment ls with fewer segfaults
authora bellenir <a@bellenir.com>
Wed, 23 Jul 2014 23:19:04 +0000 (23:19 +0000)
committera bellenir <a@bellenir.com>
Wed, 23 Jul 2014 23:19:04 +0000 (23:19 +0000)
source/input.c
source/workdir.c

index 09da1158a44f82b492801b2a72e40ce68dff33dd..26979c6af206f7b10908bf672e1ee26de29e2151 100644 (file)
@@ -11,9 +11,9 @@ void input_handle_key(char ch) {
                   break;
         case 'q': state_set_running(false);
                   break;
-        //case 'j': workdir_next(); break;
-        //case 'k': workdir_prev(); break;
-        //case 'e': workdir_cd(); break;
+        case 'j': workdir_next(state_get_focused_frame()->workdir); break;
+        case 'k': workdir_prev(state_get_focused_frame()->workdir); break;
+        //case 'e': workdir_cd(state_get_focused_frame()->workdir); break;
         case 'n': screen_open();
                   break;
         case 'c': screen_close();
index 742d8aa0f2b2069e35368f4950ccc7ee63fbcc59..80d54e44a2177075d67f9838a225f6cc8f654b71 100644 (file)
@@ -28,6 +28,7 @@ WorkDir_T* workdir_new(char* path){
        WorkDir_T* wd = mem_allocate(sizeof(WorkDir_T), &workdir_free);
        wd->idx = 0;
        wd->path = path;
+       wd->vfiles = vec_new(0);
        workdir_ls(wd);
        wd->top_index = 0;
        return wd;
@@ -104,26 +105,31 @@ void workdir_cd(WorkDir_T* wd) {
 }
 
 void workdir_ls(WorkDir_T* wd){
-    int i=0;
-    char* dotdot = mem_allocate(sizeof(char)*3, NULL);
-    char cmd[1028] = "ls "; //TODO: suck less
+    char* dotdot = mem_allocate(sizeof(char) * 3, NULL);
+    char* cmd = mem_allocate(sizeof(char) * (4+(strlen(wd->path))), NULL);
     size_t len = 0; //unused. reflects sized allocated for buffer (filename) by getline
     ssize_t read;
-    char* filename=0;
+    char* filename = 0;
     FILE* ls;
+    //free old file vector
     if(wd->vfiles) mem_release(wd->vfiles);
+    //open new ls pipe
+    strcpy(cmd, "ls ");
+    strcat(cmd, wd->path);
+    ls = popen(cmd, "r");
     strcpy(dotdot, "..");
+    //initialize new file vector
     wd->vfiles = vec_new(1, dotdot); /* TODO: check if path = / */
-    strcpy(&cmd[3], wd->path);
-    ls = popen(cmd, "r");
-    i = 1;
     while ((read = getline(&filename, &len, ls)) != -1){
         char* lol = mem_allocate(read*sizeof(char), NULL);
         filename[read-1]=0; //remove ending newline
         strcpy(lol, filename);
         vec_push_back(wd->vfiles, lol);
-        i++;
-        if(i>1022) break;
+        free(filename);
+               filename = 0;
     }
+    //mem_release(dotdot); #dont free, because there's a bug(?) in vectors and reference counting
+    //reference counter is not incremented for added items, so releasinghere will free the memory
+    mem_release(cmd);
 }