commit
8199ec3ecb
4 changed files with 40 additions and 59 deletions
14
Makefile
14
Makefile
|
@ -7,24 +7,20 @@ SRC = ccc.c util.c file.c
|
||||||
|
|
||||||
# Flags
|
# Flags
|
||||||
LDFLAGS = $(shell pkg-config --libs ncurses)
|
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
|
CC=cc
|
||||||
CONF = config.h
|
|
||||||
DEFCONF = config.def.h
|
|
||||||
PREFIX ?= /usr/local
|
PREFIX ?= /usr/local
|
||||||
|
CONF = config.h
|
||||||
|
|
||||||
BINDIR = $(PREFIX)/bin
|
BINDIR = $(PREFIX)/bin
|
||||||
MANDIR = $(PREFIX)/share/man/man1
|
MANDIR = $(PREFIX)/share/man/man1
|
||||||
|
|
||||||
.PHONY: all install uninstall clean
|
.PHONY: all install uninstall clean
|
||||||
|
|
||||||
all: $(TARGET)
|
$(TARGET): $(SRC)
|
||||||
|
|
||||||
$(TARGET): $(CONF) $(SRC)
|
|
||||||
$(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@
|
$(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@
|
||||||
|
|
||||||
$(CONF):
|
all: $(TARGET)
|
||||||
cp -v $(DEFCONF) $(CONF)
|
|
||||||
|
|
||||||
install: $(TARGET)
|
install: $(TARGET)
|
||||||
mkdir -p $(DESTDIR)$(BINDIR)
|
mkdir -p $(DESTDIR)$(BINDIR)
|
||||||
|
@ -40,3 +36,5 @@ uninstall:
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) $(TARGET)
|
$(RM) $(TARGET)
|
||||||
|
|
||||||
|
ccc.o: $(CONF)
|
||||||
|
|
82
ccc.c
82
ccc.c
|
@ -1,3 +1,4 @@
|
||||||
|
#define _XOPEN_SOURCE 600
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -5,15 +6,14 @@
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
#include <dirent.h> /* directories etc. */
|
#include <dirent.h> /* directories etc. */
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <ftw.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "config.h"
|
||||||
#define ESC 0x1B /* \e or \033 */
|
|
||||||
#define PH 1 /* panel height */
|
|
||||||
#define JUMP_NUM 14 /* how long ctrl + u/d jump are */
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
WINDOW *window;
|
WINDOW *window;
|
||||||
|
@ -24,7 +24,7 @@ typedef struct {
|
||||||
|
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void list_files(const char *path);
|
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);
|
long add_file_stat(char *filename);
|
||||||
void highlight_current_line();
|
void highlight_current_line();
|
||||||
void show_file_content();
|
void show_file_content();
|
||||||
|
@ -44,6 +44,8 @@ WINDOW *preview_border;
|
||||||
WINDOW *preview_content;
|
WINDOW *preview_content;
|
||||||
WINDOW *panel;
|
WINDOW *panel;
|
||||||
|
|
||||||
|
unsigned long total_dir_size;
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc > 1 && strcmp(argv[1], "-h") == 0)
|
if (argc > 1 && strcmp(argv[1], "-h") == 0)
|
||||||
|
@ -93,6 +95,7 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
ch = getch();
|
ch = getch();
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
|
|
||||||
/* quit */
|
/* quit */
|
||||||
case 'q':
|
case 'q':
|
||||||
endwin();
|
endwin();
|
||||||
|
@ -140,7 +143,7 @@ int main(int argc, char** argv)
|
||||||
break;
|
break;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* jump up */
|
/* jump up (ctrl u)*/
|
||||||
case '\x15':
|
case '\x15':
|
||||||
if ((current_selection - JUMP_NUM) > 0)
|
if ((current_selection - JUMP_NUM) > 0)
|
||||||
current_selection -= JUMP_NUM;
|
current_selection -= JUMP_NUM;
|
||||||
|
@ -158,7 +161,7 @@ int main(int argc, char** argv)
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* jump down */
|
/* jump down (ctrl d)*/
|
||||||
case '\x04':
|
case '\x04':
|
||||||
if ((current_selection + JUMP_NUM) < (files_len() - 1))
|
if ((current_selection + JUMP_NUM) < (files_len() - 1))
|
||||||
current_selection += JUMP_NUM;
|
current_selection += JUMP_NUM;
|
||||||
|
@ -261,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;
|
total_dir_size += sb->st_size;
|
||||||
struct dirent *ep;
|
return 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -306,7 +277,12 @@ long long get_directory_size(const char *path)
|
||||||
long add_file_stat(char *filepath)
|
long add_file_stat(char *filepath)
|
||||||
{
|
{
|
||||||
struct stat file_stat;
|
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 */
|
/* get last modified time */
|
||||||
char *time = memalloc(20 * sizeof(char));
|
char *time = memalloc(20 * sizeof(char));
|
||||||
|
@ -317,17 +293,21 @@ long add_file_stat(char *filepath)
|
||||||
/* get file size */
|
/* get file size */
|
||||||
double bytes = file_stat.st_size;
|
double bytes = file_stat.st_size;
|
||||||
if (S_ISDIR(file_stat.st_mode)) {
|
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 */
|
/* max 25 chars due to long, space, suffix and nul */
|
||||||
char *size = memalloc(25 * sizeof(char));
|
char *size = memalloc(25 * sizeof(char));
|
||||||
int unit = 0;
|
int unit = 0;
|
||||||
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
const char* units[] = {" B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
||||||
while (bytes > 1024) {
|
while (bytes > 1024) {
|
||||||
bytes /= 1024;
|
bytes /= 1024;
|
||||||
unit++;
|
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 */
|
/* get file type */
|
||||||
char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */
|
char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */
|
||||||
|
@ -409,6 +389,7 @@ void highlight_current_line()
|
||||||
wrefresh(panel);
|
wrefresh(panel);
|
||||||
/* show file content every time cursor changes */
|
/* show file content every time cursor changes */
|
||||||
show_file_content();
|
show_file_content();
|
||||||
|
wrefresh(preview_content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -416,11 +397,12 @@ void highlight_current_line()
|
||||||
*/
|
*/
|
||||||
void show_file_content()
|
void show_file_content()
|
||||||
{
|
{
|
||||||
FILE *file = fopen(get_filepath((long) current_selection), "rb");
|
|
||||||
if (file) {
|
|
||||||
wclear(preview_content);
|
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) {
|
||||||
draw_border_title(preview_border, true);
|
draw_border_title(preview_border, true);
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
long length = ftell(file);
|
long length = ftell(file);
|
||||||
/* check if file isn't empty */
|
/* check if file isn't empty */
|
||||||
|
@ -429,7 +411,6 @@ void show_file_content()
|
||||||
char *buffer = memalloc(length * sizeof(char));
|
char *buffer = memalloc(length * sizeof(char));
|
||||||
fread(buffer, 1, length, file);
|
fread(buffer, 1, length, file);
|
||||||
mvwprintw(preview_content, 0, 0, "%s", buffer);
|
mvwprintw(preview_content, 0, 0, "%s", buffer);
|
||||||
wrefresh(preview_content);
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
} else {
|
} else {
|
||||||
wclear(preview_content);
|
wclear(preview_content);
|
||||||
|
@ -486,7 +467,6 @@ void init_windows()
|
||||||
draw_border_title(preview_border, false);
|
draw_border_title(preview_border, false);
|
||||||
|
|
||||||
scrollok(directory_content, true);
|
scrollok(directory_content, true);
|
||||||
scrollok(preview_content, true);
|
|
||||||
/* window location y, x */
|
/* window location y, x */
|
||||||
windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
||||||
windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
||||||
|
|
3
config.h
3
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 */
|
Loading…
Reference in a new issue