186 lines
5.6 KiB
C
186 lines
5.6 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include <curses.h>
|
|
#include <dirent.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <pthread.h>
|
|
#include "defines.h"
|
|
#include "sorting.h"
|
|
|
|
extern unsigned int settings;
|
|
extern unsigned int file_modifiers;
|
|
extern unsigned int color_count;
|
|
extern color *colors;
|
|
|
|
char* concat(const char *s1, const char *s2){
|
|
const size_t len1 = strlen(s1);
|
|
const size_t len2 = strlen(s2);
|
|
char *result = malloc(len1 + len2 + 1);
|
|
memcpy(result, s1, len1);
|
|
memcpy(result + len1, s2, len2 + 1);
|
|
return result;
|
|
}
|
|
|
|
unsigned long get_dir_size(char *path){
|
|
DIR *dir = opendir(path);
|
|
unsigned long entry_count = 0;
|
|
if (dir) {
|
|
struct dirent *entry;
|
|
while ((entry=readdir(dir))) {
|
|
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
|
entry_count++;
|
|
}
|
|
}
|
|
}
|
|
closedir(dir);
|
|
return entry_count;
|
|
|
|
}
|
|
|
|
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
|
|
struct dirent **entry;
|
|
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
|
scandir(path, &entry, NULL, alphasort);
|
|
} else {
|
|
scandir(path, &entry, skip_hidden_files, alphasort);
|
|
}
|
|
|
|
|
|
|
|
unsigned long i = 0;
|
|
for (i = 0; i < *dir_file_count; i++ ) {
|
|
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
|
} else {
|
|
dir_content[i].file_name_width = strlen(entry[i]->d_name);
|
|
dir_content[i].file_name = malloc(dir_content[i].file_name_width + 1);
|
|
strcpy(dir_content[i].file_name, entry[i]->d_name);
|
|
|
|
struct stat *file;
|
|
file = malloc(sizeof(struct stat));
|
|
memset(file, ' ', sizeof(struct stat));
|
|
|
|
/* using the full path allows using the same function for all windows */
|
|
unsigned long path_len = strlen(path);
|
|
char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
|
|
memcpy(full_path, path, strlen(path));
|
|
memcpy(full_path + path_len, "/", sizeof("/"));
|
|
memcpy(full_path + path_len + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
|
|
|
lstat(full_path, file);
|
|
|
|
dir_content[i].file_size_bytes = file->st_size;
|
|
|
|
if (S_ISDIR(file->st_mode)) {
|
|
dir_content[i].file_type = FILE_TYPE_DIR;
|
|
dir_content[i].color_pair = COLOR_DIR;
|
|
dir_content[i].file_size_bytes = get_dir_size(full_path);
|
|
} else if (file->st_mode & S_IXUSR) {
|
|
dir_content[i].file_type = FILE_TYPE_EXEC;
|
|
dir_content[i].color_pair = COLOR_EXEC;
|
|
} else if (S_ISBLK(file->st_mode)) {
|
|
dir_content[i].file_type = FILE_TYPE_BLOCK;
|
|
dir_content[i].color_pair = COLOR_BLOCK;
|
|
/*} else if (S_ISCHR(file->st_mode)) {
|
|
dir_content[i].file_type = COLOR_CHARDEV; */
|
|
} else if (S_ISLNK(file->st_mode)) {
|
|
dir_content[i].file_type = FILE_TYPE_SYMLINK;
|
|
dir_content[i].color_pair = COLOR_SYMLINK;
|
|
dir_content[i].file_size_bytes = get_dir_size(full_path);
|
|
} else if (S_ISFIFO(file->st_mode)) {
|
|
dir_content[i].file_type = FILE_TYPE_FIFO;
|
|
dir_content[i].color_pair = COLOR_FIFO;
|
|
} else if (S_ISSOCK(file->st_mode)) {
|
|
dir_content[i].file_type = FILE_TYPE_SOCK;
|
|
dir_content[i].color_pair = COLOR_SOCK;
|
|
} else if (S_ISREG(file->st_mode)) {
|
|
dir_content[i].file_type = FILE_TYPE_REGULAR;
|
|
dir_content[i].color_pair = COLOR_REGULAR;
|
|
unsigned long j = 0;
|
|
char *extension = strrchr(entry[i]->d_name, '.');
|
|
if (extension) {
|
|
for (j = 0; j < color_count; j++) {
|
|
if (!strcmp(colors[j].file_extension, extension)) {
|
|
dir_content[i].color_pair = colors[j].color_pair;
|
|
}
|
|
}
|
|
} else {
|
|
}
|
|
} else {
|
|
dir_content[i].file_type = COLOR_REGULAR;
|
|
dir_content[i].color_pair = COLOR_REGULAR;
|
|
unsigned long j = 0;
|
|
char *extension = strrchr(entry[i]->d_name, '.');
|
|
if (extension) {
|
|
for (j = 0; j < color_count; j++) {
|
|
if (!strcmp(colors[j].file_extension, extension)) {
|
|
dir_content[i].color_pair = colors[j].color_pair;
|
|
}
|
|
}
|
|
} else {
|
|
}
|
|
}
|
|
free(full_path);
|
|
free(file);
|
|
}
|
|
}
|
|
qsort(dir_content, *dir_file_count, sizeof(file), sort_natural);
|
|
|
|
for (i = 0; i < *dir_file_count; i++) {
|
|
free(entry[i]);
|
|
}
|
|
free(entry);
|
|
|
|
}
|
|
|
|
void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content){
|
|
|
|
char *hover_bg = malloc(*line_width+1);
|
|
memset(hover_bg, ' ', *line_width);
|
|
hover_bg[*line_width] = '\0';
|
|
|
|
unsigned long i = 0;
|
|
unsigned long j = 0;
|
|
|
|
unsigned long offset_front = 2;
|
|
if (*dir_file_count > 9) {
|
|
offset_front = (snprintf(NULL, 0, "%ld", *dir_file_count)) + 1;
|
|
}
|
|
for (i = 0; i < *dir_file_count; i++) {
|
|
|
|
unsigned long offset_back = *line_width - (snprintf(NULL,0,"%ld",dir_content[i].file_size_bytes) + 1);
|
|
unsigned long allowed_width = *line_width+1;
|
|
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
|
|
|
|
if (dir_content[i].status & FILE_STATUS_HOVER) {
|
|
wattron(win, A_REVERSE);
|
|
mvwprintw(win, i, 0, "%s", hover_bg);
|
|
mvwprintw(win, i, 0, "%ld", i);
|
|
|
|
for (j = 0; j < dir_content[i].file_name_width ;j++) {
|
|
if (j >= offset_back-1) {
|
|
mvwaddch(win, i, j, '~');
|
|
break;
|
|
}
|
|
mvwaddch(win, i, offset_front+j, dir_content[i].file_name[j]);
|
|
}
|
|
mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size_bytes);
|
|
wattroff(win, A_REVERSE);
|
|
} else {
|
|
mvwprintw(win, i, 0, "%ld", i);
|
|
for (j = 0; j < dir_content[i].file_name_width ;j++) {
|
|
if (j >= offset_back-1) {
|
|
mvwaddch(win, i, j, '~');
|
|
break;
|
|
}
|
|
mvwaddch(win, i, offset_front+j, dir_content[i].file_name[j]);
|
|
}
|
|
mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size_bytes);
|
|
}
|
|
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
|
}
|
|
free(hover_bg);
|
|
}
|
|
|