Compare commits

...

10 commits

6 changed files with 65 additions and 52 deletions

View file

@ -1,5 +1,4 @@
.POSIX: .POSIX:
.SUFFIXES:
VERSION = 1.0 VERSION = 1.0
TARGET = ccc TARGET = ccc
@ -8,18 +7,21 @@ PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1 MANDIR = $(PREFIX)/share/man/man1
CFLAGS = -Os -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 CFLAGS += -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600
SRC = ccc.c util.c file.c icons.c SRC = ccc.c util.c file.c icons.c
OBJS = $(SRC:.c=.o)
$(TARGET): $(SRC) config.h .c.o:
$(CC) $(SRC) -o $@ $(CFLAGS) $(LDFLAGS) $(CC) -o $@ $(CFLAGS) -c $<
$(TARGET): $(OBJS) config.h
$(CC) -o $@ $(OBJS) $(LIBS)
dist: dist:
mkdir -p $(TARGET)-$(VERSION) mkdir -p $(TARGET)-$(VERSION)
cp -R README.md $(MANPAGE) $(TARGET) $(TARGET)-$(VERSION) cp -R README.md $(MANPAGE) $(TARGET) $(TARGET)-$(VERSION)
tar -cf $(TARGET)-$(VERSION).tar $(TARGET)-$(VERSION) tar -czf $(TARGET)-$(VERSION).tar.gz $(TARGET)-$(VERSION)
gzip $(TARGET)-$(VERSION).tar
rm -rf $(TARGET)-$(VERSION) rm -rf $(TARGET)-$(VERSION)
install: $(TARGET) install: $(TARGET)
@ -31,11 +33,11 @@ install: $(TARGET)
chmod 644 $(DESTDIR)$(MANDIR)/$(MANPAGE) chmod 644 $(DESTDIR)$(MANDIR)/$(MANPAGE)
uninstall: uninstall:
rm $(DESTDIR)$(BINDIR)/$(TARGET) rm -f $(DESTDIR)$(BINDIR)/$(TARGET)
rm $(DESTDIR)$(MANDIR)/$(MANPAGE) rm -f $(DESTDIR)$(MANDIR)/$(MANPAGE)
clean: clean:
rm $(TARGET) rm -f $(TARGET) *.o
all: $(TARGET) all: $(TARGET)

53
ccc.c
View file

@ -196,7 +196,7 @@ int main(int argc, char **argv)
while (1) { while (1) {
list_files(); list_files();
keybinding(); keybinding();
} }
return 0; return 0;
} }
@ -220,6 +220,7 @@ void handle_sigwinch(int ignore)
void cleanup(void) void cleanup(void)
{ {
hashtable_free();
if (files->length != 0) { if (files->length != 0) {
arraylist_free(files); arraylist_free(files);
} }
@ -408,13 +409,12 @@ void add_file_stat(char *filename, char *path, int ftype)
{ {
struct stat file_stat; struct stat file_stat;
if (stat(path, &file_stat) == -1) { if (stat(path, &file_stat) == -1) {
/* can't be triggered? */ perror("stat()");
if (errno == EACCES) return;
arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, 0, 0);
} }
int type; int type = 0;
char icon_str[5]; char icon_str[8] = {0};
filename[strlen(filename)] = '\0'; filename[strlen(filename)] = '\0';
/* handle file without extension /* handle file without extension
@ -426,17 +426,20 @@ void add_file_stat(char *filename, char *path, int ftype)
} }
/* add file extension */ /* add file extension */
icon *ext_icon = hashtable_search(ext ? ext : filename); icon *ext_icon = hashtable_search(ext ? ext : filename);
if (!ext_icon) if (!ext_icon) {
memcpy(icon_str, "", 4); char ch[] = "";
else memcpy(icon_str, ch, sizeof(ch));
memcpy(icon_str, ext_icon->icon, 4); } else {
strncpy(icon_str, ext_icon->icon, sizeof(icon_str));
}
int color = DEF_COLOR; int color = DEF_COLOR;
if (S_ISDIR(file_stat.st_mode)) { if (S_ISDIR(file_stat.st_mode)) {
type = DRY; /* dir */ type = DRY; /* dir */
color = DIR_COLOR; color = DIR_COLOR;
memcpy(icon_str, "󰉋", 4); char ch[] = "󰉋";
memcpy(icon_str, ch, sizeof(ch));
} else if (S_ISREG(file_stat.st_mode)) { } else if (S_ISREG(file_stat.st_mode)) {
type = REG; /* regular file */ type = REG; /* regular file */
color = REG_COLOR; color = REG_COLOR;
@ -536,24 +539,32 @@ void add_file_stat(char *filename, char *path, int ftype)
*/ */
void show_file_content(void) void show_file_content(void)
{ {
if (sel_file >= files->length) {
return;
}
file current_file = files->items[sel_file]; file current_file = files->items[sel_file];
move_cursor(1, half_width); move_cursor(1, half_width);
if (current_file.type == DRY) { if (current_file.type == DRY) {
ArrayList *files_visit; ArrayList *files_visit = NULL;
populate_files(current_file.name, 0, &files_visit); populate_files(current_file.name, 0, &files_visit);
if (!files_visit)
return;
for (long i = 0; i < files_visit->length && i < rows - 1; i++) { for (long i = 0; i < files_visit->length && i < rows - 1; i++) {
char *line = get_line(files_visit, i, 0, show_icons); char *line = get_line(files_visit, i, 0, show_icons);
int color = files_visit->items[i].color; int color = files_visit->items[i].color;
move_cursor(i + 1, half_width); move_cursor(i + 1, half_width);
bprintf("\033[K\033[%dm%s\033[m\n", color, line); bprintf("\033[K\033[%dm%s\033[m\n", color, line);
free(line);
} }
arraylist_free(files_visit); arraylist_free(files_visit);
return; return;
} }
FILE *file = fopen(current_file.path, "r"); FILE *file = fopen(current_file.path, "r");
if (!file) { if (!file) {
bprintf("Unable to read %s", current_file.name); /* bprintf("Unable to read %s", current_file.name ? current_file.name : "unknown"); */
bprintf("Unable to read unknown");
return; return;
} }
@ -565,6 +576,7 @@ void show_file_content(void)
return; return;
} }
} }
fclose(file);
int pipe_fd[2]; int pipe_fd[2];
if (pipe(pipe_fd) == -1) { if (pipe(pipe_fd) == -1) {
perror("pipe"); perror("pipe");
@ -642,7 +654,7 @@ void list_files(void)
/* calculate range of files to show */ /* calculate range of files to show */
long range = files->length; long range = files->length;
/* not highlight if no files in directory */ /* not highlight if no files in directory */
if (range == 0 && errno == 0) { if (range == 0) {
for (int i = 0; i < rows - 1; i++) { for (int i = 0; i < rows - 1; i++) {
move_cursor(i + 1, 1); move_cursor(i + 1, 1);
bprintf("\033[K"); bprintf("\033[K");
@ -835,6 +847,9 @@ void nav_back(const Arg *arg)
void nav_enter(const Arg *arg) void nav_enter(const Arg *arg)
{ {
if (sel_file >= files->length) {
return;
}
file c_file = files->items[sel_file]; file c_file = files->items[sel_file];
/* Check if it is directory or a regular file */ /* Check if it is directory or a regular file */
if (c_file.type == DRY) { if (c_file.type == DRY) {
@ -1419,12 +1434,8 @@ int get_window_size(int *row, int *col)
*/ */
void bprintf(const char *fmt, ...) void bprintf(const char *fmt, ...)
{ {
char buffer[1024];
va_list args; va_list args;
va_start(args, fmt);
va_start(args, fmt); vprintf(fmt, args);
vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args);
va_end(args);
write(STDOUT_FILENO, buffer, strlen(buffer));
} }

35
file.c
View file

@ -69,7 +69,7 @@ void arraylist_remove(ArrayList *list, long index)
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force) void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force)
{ {
file new_file = { name, path, type, stats, color }; file new_file = { name, path, type, stats, color };
strcpy(new_file.icon, icon); strncpy(new_file.icon, icon, sizeof(new_file.icon) / sizeof(new_file.icon[0]));
if (list->capacity != list->length) { if (list->capacity != list->length) {
if (marked) { if (marked) {
@ -105,25 +105,18 @@ char *get_line(ArrayList *list, long index, int detail, int icons)
{ {
file f = list->items[index]; file f = list->items[index];
size_t length = strlen(f.name) + 1; size_t length = strlen(f.name) + 1;
if (detail) { length += detail ? strlen(f.stats) + 1 : 0;
length += strlen(f.stats) + 1; /* 1 for space */ length += icons ? strlen(f.icon) + 1 : 0;
}
if (icons) {
length += 5; /* 4 for icon, 1 for space */
}
char *line = memalloc(length); char *line = memalloc(length);
line[0] = '\0';
if (detail) { snprintf(line, length, "%s%s%s%s%s",
strcat(line, f.stats); detail ? f.stats : "",
strcat(line, " "); detail ? " " : "",
} icons ? f.icon : "",
if (icons) { icons ? " " : "",
strcat(line, f.icon); f.name);
strcat(line, " ");
} return line;
strcat(line, f.name);
line[length] = '\0';
return line;
} }

2
file.h
View file

@ -19,7 +19,7 @@ typedef struct {
int type; int type;
char *stats; char *stats;
int color; int color;
char icon[5]; char icon[8];
} file; } file;
typedef struct { typedef struct {

View file

@ -187,3 +187,9 @@ icon *hashtable_search(char *name)
return NULL; return NULL;
} }
void hashtable_free(void)
{
for (int i = 0; i < TABLE_SIZE; i++)
free(hash_table[i]);
}

View file

@ -14,5 +14,6 @@ void hashtable_init(void);
void hashtable_print(void); void hashtable_print(void);
int hashtable_add(icon *p); int hashtable_add(icon *p);
icon *hashtable_search(char *name); icon *hashtable_search(char *name);
void hashtable_free(void);
#endif #endif