Merge pull request #1 from PalanixYT/dirs
Sort dirs first, then files, Fix comments about linked lists, reduce duplication of strings, fix memory leaks
This commit is contained in:
commit
0bb433855d
2 changed files with 46 additions and 62 deletions
73
ccc.c
73
ccc.c
|
@ -32,7 +32,7 @@ void highlight_current_line();
|
||||||
void show_file_content();
|
void show_file_content();
|
||||||
void edit_file();
|
void edit_file();
|
||||||
void toggle_executable();
|
void toggle_executable();
|
||||||
void replace_home(char *str);
|
char *replace_home(char *str);
|
||||||
int write_last_d();
|
int write_last_d();
|
||||||
void create_file();
|
void create_file();
|
||||||
void delete_files();
|
void delete_files();
|
||||||
|
@ -55,6 +55,8 @@ char *p_cwd; /* previous cwd */
|
||||||
int half_width;
|
int half_width;
|
||||||
ArrayList *files;
|
ArrayList *files;
|
||||||
ArrayList *marked;
|
ArrayList *marked;
|
||||||
|
ArrayList *tmp1;
|
||||||
|
ArrayList *tmp2;
|
||||||
WINDOW *directory_border;
|
WINDOW *directory_border;
|
||||||
WINDOW *directory_content;
|
WINDOW *directory_content;
|
||||||
WINDOW *preview_border;
|
WINDOW *preview_border;
|
||||||
|
@ -128,7 +130,6 @@ int main(int argc, char** argv)
|
||||||
init_pair(8, COLOR_WHITE, -1); /* REG */
|
init_pair(8, COLOR_WHITE, -1); /* REG */
|
||||||
|
|
||||||
/* init files and marked arrays */
|
/* init files and marked arrays */
|
||||||
files = arraylist_init(100);
|
|
||||||
marked = arraylist_init(100);
|
marked = arraylist_init(100);
|
||||||
hashtable_init();
|
hashtable_init();
|
||||||
|
|
||||||
|
@ -157,8 +158,7 @@ int main(int argc, char** argv)
|
||||||
/* prompt user so error message can be shown to user */
|
/* prompt user so error message can be shown to user */
|
||||||
getch();
|
getch();
|
||||||
}
|
}
|
||||||
endwin();
|
goto cleanup;
|
||||||
return 0;
|
|
||||||
|
|
||||||
/* reload using z */
|
/* reload using z */
|
||||||
case 'z':
|
case 'z':
|
||||||
|
@ -387,6 +387,7 @@ int main(int argc, char** argv)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cleanup:
|
||||||
free(argv_cp);
|
free(argv_cp);
|
||||||
arraylist_free(files);
|
arraylist_free(files);
|
||||||
arraylist_free(marked);
|
arraylist_free(marked);
|
||||||
|
@ -448,17 +449,10 @@ char *check_trash_dir()
|
||||||
*/
|
*/
|
||||||
void change_dir(const char *buf, int selection, int ftype)
|
void change_dir(const char *buf, int selection, int ftype)
|
||||||
{
|
{
|
||||||
char *buf_dup;
|
if (cwd != buf)
|
||||||
if (buf == p_cwd) {
|
strcpy(cwd, buf);
|
||||||
buf_dup = estrdup(p_cwd);
|
if (ftype == 0)
|
||||||
} else {
|
|
||||||
buf_dup = (char *) buf;
|
|
||||||
}
|
|
||||||
strcpy(cwd, buf_dup);
|
|
||||||
if (ftype != 2) {
|
|
||||||
arraylist_free(files);
|
arraylist_free(files);
|
||||||
files = arraylist_init(100);
|
|
||||||
}
|
|
||||||
current_selection = selection;
|
current_selection = selection;
|
||||||
populate_files(cwd, ftype);
|
populate_files(cwd, ftype);
|
||||||
}
|
}
|
||||||
|
@ -513,7 +507,7 @@ void mkdir_p(const char *destdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read the provided directory and add all files in directory to linked list
|
* Read the provided directory and add all files in directory to an Arraylist
|
||||||
* ftype: normal files = 0, marked = 1, marking ALL = 2
|
* ftype: normal files = 0, marked = 1, marking ALL = 2
|
||||||
*/
|
*/
|
||||||
void populate_files(const char *path, int ftype)
|
void populate_files(const char *path, int ftype)
|
||||||
|
@ -524,24 +518,35 @@ void populate_files(const char *path, int ftype)
|
||||||
if ((dp = opendir(path)) != NULL) {
|
if ((dp = opendir(path)) != NULL) {
|
||||||
/* clear directory window to ready for printing */
|
/* clear directory window to ready for printing */
|
||||||
wclear(directory_content);
|
wclear(directory_content);
|
||||||
|
if (ftype == 0) {
|
||||||
|
tmp1 = arraylist_init(10);
|
||||||
|
tmp2 = arraylist_init(10);
|
||||||
|
}
|
||||||
|
|
||||||
while ((ep = readdir(dp)) != NULL) {
|
while ((ep = readdir(dp)) != NULL) {
|
||||||
char *path = memalloc(PATH_MAX * sizeof(char));
|
char *filename = estrdup(ep->d_name);
|
||||||
char *filename = memalloc(PATH_MAX * sizeof(char));
|
|
||||||
/* copy filename */
|
|
||||||
strcpy(filename, ep->d_name);
|
|
||||||
|
|
||||||
/* use strncmp to filter out dotfiles */
|
/* use strncmp to filter out dotfiles */
|
||||||
if ((!show_hidden && strncmp(filename, ".", 1) && strncmp(filename, "..", 2)) || (show_hidden && strcmp(filename, ".") && strcmp(filename, ".."))) {
|
if ((!show_hidden && strncmp(filename, ".", 1) && strncmp(filename, "..", 2)) || (show_hidden && strcmp(filename, ".") && strcmp(filename, ".."))) {
|
||||||
/* construct full file path */
|
/* construct full file path */
|
||||||
|
char *path = memalloc((strlen(cwd) + strlen(filename) + 2) * sizeof(char));
|
||||||
strcpy(path, cwd);
|
strcpy(path, cwd);
|
||||||
strcat(path, "/");
|
strcat(path, "/");
|
||||||
strcat(path, filename); /* add filename */
|
strcat(path, filename); /* add filename */
|
||||||
|
|
||||||
add_file_stat(filename, path, ftype);
|
add_file_stat(filename, path, ftype);
|
||||||
}
|
}
|
||||||
free(filename);
|
else free(filename);
|
||||||
free(path);
|
}
|
||||||
|
if (ftype == 0) {
|
||||||
|
files = arraylist_init(tmp1->length + tmp2->length);
|
||||||
|
files->length = tmp1->length + tmp2->length;
|
||||||
|
memcpy(files->items, tmp1->items, tmp1->length * sizeof(file));
|
||||||
|
memcpy(files->items + tmp1->length, tmp2->items, tmp2->length * sizeof(file));
|
||||||
|
free(tmp1->items);
|
||||||
|
free(tmp2->items);
|
||||||
|
free(tmp1);
|
||||||
|
free(tmp2);
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
wrefresh(directory_content);
|
wrefresh(directory_content);
|
||||||
|
@ -627,7 +632,6 @@ void add_file_stat(char *filename, char *path, int ftype)
|
||||||
bool force = ftype == 2 ? true : false;
|
bool force = ftype == 2 ? true : false;
|
||||||
arraylist_add(marked, filename, path, NULL, type, icon_str, 8, true, force);
|
arraylist_add(marked, filename, path, NULL, type, icon_str, 8, true, force);
|
||||||
/* free type and return without allocating more stuff */
|
/* free type and return without allocating more stuff */
|
||||||
free(type);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -675,14 +679,15 @@ void add_file_stat(char *filename, char *path, int ftype)
|
||||||
char *total_stat = memalloc(stat_size);
|
char *total_stat = memalloc(stat_size);
|
||||||
snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size);
|
snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size);
|
||||||
|
|
||||||
arraylist_add(files, filename, path, total_stat, type, icon_str, color, false, false);
|
/* DIR if color is 5 */
|
||||||
|
if (color == 5)
|
||||||
|
arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, false, false);
|
||||||
|
else
|
||||||
|
arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, false, false);
|
||||||
|
|
||||||
free(time);
|
free(time);
|
||||||
free(size);
|
free(size);
|
||||||
free(total_stat);
|
|
||||||
free(type);
|
|
||||||
free(mode_str);
|
free(mode_str);
|
||||||
free(icon_str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -887,24 +892,24 @@ void toggle_executable()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void replace_home(char *str)
|
char *replace_home(char *str)
|
||||||
{
|
{
|
||||||
char *home = getenv("HOME");
|
char *home = getenv("HOME");
|
||||||
if (home == NULL) {
|
if (home == NULL) {
|
||||||
wpprintw("$HOME is not defined");
|
wpprintw("$HOME is not defined");
|
||||||
return;
|
return str;
|
||||||
}
|
}
|
||||||
char *after_tilde = estrdup(str + 1);
|
char *newstr = memalloc((strlen(str) + strlen(home)) * sizeof(char));
|
||||||
/* replace ~ with home */
|
/* replace ~ with home */
|
||||||
snprintf(str, PATH_MAX, "%s%s", home, after_tilde);
|
snprintf(newstr, strlen(str) + strlen(home), "%s%s", home, str + 1);
|
||||||
free(after_tilde);
|
free(str);
|
||||||
|
return newstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int write_last_d()
|
int write_last_d()
|
||||||
{
|
{
|
||||||
#ifdef LAST_D
|
#ifdef LAST_D
|
||||||
char *last_d = memalloc(PATH_MAX * sizeof(char));
|
char *last_d = estrdup(LAST_D);
|
||||||
strcpy(last_d, LAST_D);
|
|
||||||
#else
|
#else
|
||||||
char *last_d = getenv("CCC_LAST_D");
|
char *last_d = getenv("CCC_LAST_D");
|
||||||
#endif
|
#endif
|
||||||
|
@ -913,7 +918,7 @@ int write_last_d()
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
if (last_d[0] == '~') {
|
if (last_d[0] == '~') {
|
||||||
replace_home(last_d);
|
last_d = replace_home(last_d);
|
||||||
}
|
}
|
||||||
char *last_ddup = estrdup(last_d);
|
char *last_ddup = estrdup(last_d);
|
||||||
|
|
||||||
|
|
35
file.c
35
file.c
|
@ -33,7 +33,7 @@ void arraylist_free(ArrayList *list)
|
||||||
}
|
}
|
||||||
|
|
||||||
free(list->items);
|
free(list->items);
|
||||||
list->length = 0;
|
free(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -77,25 +77,8 @@ void arraylist_remove(ArrayList *list, long index)
|
||||||
*/
|
*/
|
||||||
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *type, wchar_t *icon, int color, bool marked, bool force)
|
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *type, wchar_t *icon, int color, bool marked, bool force)
|
||||||
{
|
{
|
||||||
char *name_cp = NULL;
|
|
||||||
char *path_cp = NULL;
|
|
||||||
char *type_cp = NULL;
|
|
||||||
char *stats_cp = NULL;
|
|
||||||
wchar_t *icon_cp = NULL;
|
|
||||||
|
|
||||||
if (name != NULL)
|
|
||||||
name_cp = estrdup(name);
|
|
||||||
if (path != NULL)
|
|
||||||
path_cp = estrdup(path);
|
|
||||||
if (type != NULL)
|
|
||||||
type_cp = estrdup(type);
|
|
||||||
if (stats != NULL)
|
|
||||||
stats_cp = estrdup(stats);
|
|
||||||
if (icon != NULL)
|
|
||||||
icon_cp = ewcsdup(icon);
|
|
||||||
|
|
||||||
/* name, path, stats, type, icon, color */
|
/* name, path, stats, type, icon, color */
|
||||||
file new_file = { name_cp, path_cp, type_cp, stats_cp, icon_cp, color };
|
file new_file = { name, path, type, stats, icon, color };
|
||||||
|
|
||||||
if (list->capacity != list->length) {
|
if (list->capacity != list->length) {
|
||||||
if (marked) {
|
if (marked) {
|
||||||
|
@ -130,15 +113,11 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *t
|
||||||
char *get_line(ArrayList *list, long index, bool detail, bool icons)
|
char *get_line(ArrayList *list, long index, bool detail, bool icons)
|
||||||
{
|
{
|
||||||
file file = list->items[index];
|
file file = list->items[index];
|
||||||
char *name = estrdup(file.name);
|
|
||||||
wchar_t *icon = ewcsdup(file.icon);
|
|
||||||
|
|
||||||
size_t name_len = strlen(name);
|
size_t name_len = strlen(file.name);
|
||||||
char *stats = NULL;
|
|
||||||
size_t length;
|
size_t length;
|
||||||
if (detail) {
|
if (detail) {
|
||||||
stats = estrdup(file.stats);
|
length = name_len + strlen(file.stats) + 7; /* 4 for icon, 2 for space and 1 for null */
|
||||||
length = name_len + strlen(stats) + 7; /* 4 for icon, 2 for space and 1 for null */
|
|
||||||
} else {
|
} else {
|
||||||
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
|
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
|
||||||
}
|
}
|
||||||
|
@ -146,17 +125,17 @@ char *get_line(ArrayList *list, long index, bool detail, bool icons)
|
||||||
char *line = memalloc(length * sizeof(char));
|
char *line = memalloc(length * sizeof(char));
|
||||||
line[0] = '\0';
|
line[0] = '\0';
|
||||||
if (detail) {
|
if (detail) {
|
||||||
strcat(line, stats);
|
strcat(line, file.stats);
|
||||||
strcat(line, " ");
|
strcat(line, " ");
|
||||||
}
|
}
|
||||||
if (icons) {
|
if (icons) {
|
||||||
char *tmp = memalloc(8 * sizeof(char));
|
char *tmp = memalloc(8 * sizeof(char));
|
||||||
snprintf(tmp, 8, "%ls", icon);
|
snprintf(tmp, 8, "%ls", file.icon);
|
||||||
strcat(line, tmp);
|
strcat(line, tmp);
|
||||||
strcat(line, " ");
|
strcat(line, " ");
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
strcat(line, name);
|
strcat(line, file.name);
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue