From ee9523989e3bf4279cd4f4f26641b5d2f852f0ec Mon Sep 17 00:00:00 2001 From: Piotr Marendowski Date: Wed, 13 Mar 2024 20:19:22 +0100 Subject: [PATCH 1/5] Delete config.def.h and edit Makefile --- Makefile | 11 ++++------- ccc.c | 6 ++---- config.def.h | 0 config.h | 3 +++ 4 files changed, 9 insertions(+), 11 deletions(-) delete mode 100644 config.def.h diff --git a/Makefile b/Makefile index 26b7600..5153c07 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,6 @@ SRC = ccc.c util.c file.c LDFLAGS = $(shell pkg-config --libs ncurses) CFLAGS = -march=native -mtune=native -O3 -pipe -O3 -s -std=c99 -W -pedantic $(shell pkg-config --cflags ncurses) -Wall -Wextra # -Werror CC=cc -CONF = config.h -DEFCONF = config.def.h PREFIX ?= /usr/local BINDIR = $(PREFIX)/bin @@ -18,13 +16,10 @@ MANDIR = $(PREFIX)/share/man/man1 .PHONY: all install uninstall clean -all: $(TARGET) - -$(TARGET): $(CONF) $(SRC) +$(TARGET): $(SRC) $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@ -$(CONF): - cp -v $(DEFCONF) $(CONF) +all: $(TARGET) install: $(TARGET) mkdir -p $(DESTDIR)$(BINDIR) @@ -40,3 +35,5 @@ uninstall: clean: $(RM) $(TARGET) + +ccc.o: $(CONF) diff --git a/ccc.c b/ccc.c index b2f8bb9..8546ffb 100644 --- a/ccc.c +++ b/ccc.c @@ -10,10 +10,7 @@ #include "file.h" #include "util.h" - -#define ESC 0x1B /* \e or \033 */ -#define PH 1 /* panel height */ -#define JUMP_NUM 14 /* how long ctrl + u/d jump are */ +#include "config.h" typedef struct { WINDOW *window; @@ -93,6 +90,7 @@ int main(int argc, char** argv) } ch = getch(); switch (ch) { + /* quit */ case 'q': endwin(); diff --git a/config.def.h b/config.def.h deleted file mode 100644 index e69de29..0000000 diff --git a/config.h b/config.h index e69de29..28e9164 100644 --- a/config.h +++ b/config.h @@ -0,0 +1,3 @@ +#define ESC 0x1B /* \e or \033 */ +#define PH 1 /* panel height */ +#define JUMP_NUM 14 /* how long ctrl + u/d jump are */ From 74d5bda85736b6a3e6da02ef7e818a4f6d840e78 Mon Sep 17 00:00:00 2001 From: night0721 Date: Wed, 13 Mar 2024 19:37:26 +0000 Subject: [PATCH 2/5] fix on preview scrolling and error handling when no permission to stat prevent preview if it is on directory --- ccc.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ccc.c b/ccc.c index b2f8bb9..c79eec2 100644 --- a/ccc.c +++ b/ccc.c @@ -5,6 +5,7 @@ #include #include /* directories etc. */ #include +#include #include #include @@ -306,7 +307,12 @@ long long get_directory_size(const char *path) long add_file_stat(char *filepath) { struct stat file_stat; - stat(filepath, &file_stat); + if (stat(filepath, &file_stat) == -1) { + /* can't be triggered ? */ + if (errno == EACCES) { + return add_file(filepath, "", ""); + } + } /* get last modified time */ char *time = memalloc(20 * sizeof(char)); @@ -409,6 +415,7 @@ void highlight_current_line() wrefresh(panel); /* show file content every time cursor changes */ show_file_content(); + wrefresh(preview_content); } /* @@ -416,11 +423,12 @@ void highlight_current_line() */ void show_file_content() { - FILE *file = fopen(get_filepath((long) current_selection), "rb"); + wclear(preview_content); + file *current_file = get_file((long) current_selection); + if (strncmp(current_file->type, "DIR", 3) == 0) return; + FILE *file = fopen(current_file->path, "rb"); if (file) { - wclear(preview_content); draw_border_title(preview_border, true); - fseek(file, 0, SEEK_END); long length = ftell(file); /* check if file isn't empty */ @@ -429,7 +437,6 @@ void show_file_content() char *buffer = memalloc(length * sizeof(char)); fread(buffer, 1, length, file); mvwprintw(preview_content, 0, 0, "%s", buffer); - wrefresh(preview_content); free(buffer); } else { wclear(preview_content); @@ -486,7 +493,6 @@ void init_windows() draw_border_title(preview_border, false); scrollok(directory_content, true); - scrollok(preview_content, true); /* window location y, x */ windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 }; windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 }; From c6e65fb0dc5226eb95273df42e19a2f656d59ec6 Mon Sep 17 00:00:00 2001 From: night0721 Date: Wed, 13 Mar 2024 19:46:25 +0000 Subject: [PATCH 3/5] add keybinding to commit to know it is ctrl u/d --- ccc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ccc.c b/ccc.c index df27eee..ea0c91f 100644 --- a/ccc.c +++ b/ccc.c @@ -139,7 +139,7 @@ int main(int argc, char** argv) break; */ - /* jump up */ + /* jump up (ctrl u)*/ case '\x15': if ((current_selection - JUMP_NUM) > 0) current_selection -= JUMP_NUM; @@ -157,7 +157,7 @@ int main(int argc, char** argv) highlight_current_line(); break; - /* jump down */ + /* jump down (ctrl d)*/ case '\x04': if ((current_selection + JUMP_NUM) < (files_len() - 1)) current_selection += JUMP_NUM; From 5c3c0bf07f5ece8600af72bfaa1374952811213e Mon Sep 17 00:00:00 2001 From: night0721 Date: Wed, 13 Mar 2024 23:51:05 +0000 Subject: [PATCH 4/5] use nftw rather than calculating apparent size manually --- Makefile | 3 ++- ccc.c | 49 ++++++++++++------------------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 5153c07..3d0cff6 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,10 @@ SRC = ccc.c util.c file.c # Flags LDFLAGS = $(shell pkg-config --libs ncurses) -CFLAGS = -march=native -mtune=native -O3 -pipe -O3 -s -std=c99 -W -pedantic $(shell pkg-config --cflags ncurses) -Wall -Wextra # -Werror +CFLAGS = -march=native -mtune=native -O3 -pipe -O3 -s -std=c99 -pedantic $(shell pkg-config --cflags ncurses) -Wall # -Wextra -Werror CC=cc PREFIX ?= /usr/local +CONF = config.h BINDIR = $(PREFIX)/bin MANDIR = $(PREFIX)/share/man/man1 diff --git a/ccc.c b/ccc.c index ea0c91f..9875cf0 100644 --- a/ccc.c +++ b/ccc.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE 600 #include #include #include @@ -6,6 +7,7 @@ #include /* directories etc. */ #include #include +#include #include #include @@ -22,7 +24,7 @@ typedef struct { /* functions' definitions */ void list_files(const char *path); -long long get_directory_size(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); void highlight_current_line(); void show_file_content(); @@ -42,6 +44,8 @@ WINDOW *preview_border; WINDOW *preview_content; WINDOW *panel; +unsigned long total_dir_size; + int main(int argc, char** argv) { if (argc > 1 && strcmp(argv[1], "-h") == 0) @@ -260,42 +264,10 @@ void list_files(const char *path) } } -long long get_directory_size(const char *path) +int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - DIR *dp; - struct dirent *ep; - struct stat statbuf; - long long total_size = 0; - - if ((dp = opendir(path)) != NULL) { - while ((ep = readdir(dp)) != NULL) { - if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) { - continue; - } - /* build full path of entry */ - char full_path[PATH_MAX]; - snprintf(full_path, sizeof(full_path), "%s/%s", path, ep->d_name); - - if (lstat(full_path, &statbuf) == -1) { - perror("lstat"); - closedir(dp); - return -1; - } - /* recursively calculate its size if it is directory */ - if (S_ISDIR(statbuf.st_mode)) { - total_size += get_directory_size(full_path); - } else { - /* else add the size of the file to the total */ - total_size += statbuf.st_size; - } - } - closedir(dp); - } else { - perror("ccc"); - return -1; - } - - return total_size; + total_dir_size += sb->st_size; + return 0; } /* @@ -321,7 +293,10 @@ long add_file_stat(char *filepath) /* get file size */ double bytes = file_stat.st_size; if (S_ISDIR(file_stat.st_mode)) { - bytes = (double) get_directory_size(filepath); + /* at most 15 fd opened */ + total_dir_size = 0; + nftw(filepath, &get_directory_size, 15, FTW_PHYS); + bytes = total_dir_size; } /* max 25 chars due to long, space, suffix and nul */ char *size = memalloc(25 * sizeof(char)); From 071961befaf5855ffe3f82d1d39527f199410622 Mon Sep 17 00:00:00 2001 From: night0721 Date: Wed, 13 Mar 2024 23:59:08 +0000 Subject: [PATCH 5/5] better file size format --- ccc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ccc.c b/ccc.c index 9875cf0..a8bbf59 100644 --- a/ccc.c +++ b/ccc.c @@ -301,12 +301,13 @@ long add_file_stat(char *filepath) /* max 25 chars due to long, space, suffix and nul */ char *size = memalloc(25 * sizeof(char)); int unit = 0; - const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"}; + const char* units[] = {" B", "KiB", "MiB", "GiB", "TiB", "PiB"}; while (bytes > 1024) { bytes /= 1024; unit++; } - sprintf(size, "%.*f%s", unit, bytes, units[unit]); + /* 4 sig fig, limiting characters to have better format */ + sprintf(size, "%-6.4g %-3s", bytes, units[unit]); /* get file type */ char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */