diff --git a/ccc.c b/ccc.c index 09d80a9..533aa2b 100644 --- a/ccc.c +++ b/ccc.c @@ -26,9 +26,9 @@ typedef struct { /* functions' definitions */ void change_dir(const char *buf); int mkdir_p(const char *destdir); -void list_files(const char *path); +void populate_files(const char *path); int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf); -long add_file_stat(char *filename); +long add_file_stat(char *filepath, int type); void highlight_current_line(); void show_file_content(); void edit_file(); @@ -87,7 +87,7 @@ int main(int argc, char** argv) cwd = memalloc(PATH_MAX * sizeof(char)); getcwd(cwd, PATH_MAX); - list_files(cwd); + populate_files(cwd); highlight_current_line(); /* set window name */ @@ -233,7 +233,7 @@ int main(int argc, char** argv) /* mark files by space */ case 32: - ; + add_file_stat(get_filepath(current_selection), 1); break; /* escape */ @@ -264,7 +264,7 @@ void change_dir(const char *buf) { strcpy(cwd, buf); clear_files(); - list_files(cwd); + populate_files(cwd); current_selection = 0; highlight_current_line(); } @@ -318,17 +318,16 @@ int mkdir_p(const char *destdir) } /* - * Read the provided directory and list all files to window 0 + * Read the provided directory and add all files in directory to linked list * ep->d_name -> filename */ -void list_files(const char *path) +void populate_files(const char *path) { DIR *dp; struct dirent *ep; draw_border_title(directory_border, true); if ((dp = opendir(path)) != NULL) { - int count = 0; /* clear directory window to ready for printing */ wclear(directory_content); @@ -346,12 +345,7 @@ void list_files(const char *path) strcat(filename, "/"); strcat(filename, ep->d_name); /* add file name */ - long index = add_file_stat(filename); - char *line = get_line(index); - - mvwprintw(directory_content, count, 0, "%s", line); - free(line); - count++; + add_file_stat(filename, 0); } free(filename); } @@ -371,8 +365,9 @@ int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, s /* * Get file's last modified time, size, type * Add that file into list + * ftype: 0->dir files, 0->marked */ -long add_file_stat(char *filepath) +long add_file_stat(char *filepath, int ftype) { struct stat file_stat; if (stat(filepath, &file_stat) == -1) { @@ -387,7 +382,6 @@ long add_file_stat(char *filepath) /* format last modified time to a string */ strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); - /* get file size */ double bytes = file_stat.st_size; if (S_ISDIR(file_stat.st_mode)) { @@ -426,6 +420,11 @@ long add_file_stat(char *filepath) } /* don't know how to handle socket, block device, character device and FIFO */ + if (ftype == 1) { + long index = add_marked(filepath, type); + free(type); + return index; + } char *total_stat = memalloc(45 * sizeof(char)); snprintf(total_stat, 45, "%-18s %-10s", time, size); total_stat[strlen(total_stat)] = '\0'; @@ -440,6 +439,7 @@ long add_file_stat(char *filepath) return index; } + /* * Highlight current line by reversing the color */ diff --git a/file.c b/file.c index b93fa0c..3070854 100644 --- a/file.c +++ b/file.c @@ -92,6 +92,31 @@ long add_file(char *filepath, char *stats, char *type) return index; } +long add_marked(char *filepath, char *type) +{ + file *current = marked; + file *new_file = memalloc(sizeof(file)); + char *buf = strdup(filepath); + char *buf2 = strdup(type); + if (buf == NULL || buf2 == NULL) { + perror("ccc"); + } + new_file->path = buf; + new_file->type = buf2; + new_file->next = NULL; + if (current == NULL) { + files = new_file; + return 0; + } + long index = 1; + while (current->next != NULL) { + current = current->next; + index++; + } + current->next = new_file; + return index; +} + file *get_file(long index) { file *current = files; diff --git a/file.h b/file.h index 51815dd..46d1a49 100644 --- a/file.h +++ b/file.h @@ -14,6 +14,7 @@ long marked_len(); void clear_files(); void clear_marked(); long add_file(char *filename, char *time, char *type); +long add_marked(char *filepath, char *type); file *get_file(long index); char *get_filepath(long index); char *get_line(long index);