├── .gitignore ├── config.mk ├── include ├── var.h ├── main.h ├── type.h ├── info.h ├── commands.h ├── hash.h ├── opts.h ├── dir.h ├── io.h └── icons.h ├── Makefile ├── src ├── type.c ├── hash.c ├── info.c ├── var.c ├── commands.c ├── opts.c ├── dir.c ├── main.c └── io.c ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .*.swp 2 | cscroll 3 | src/*.o 4 | -------------------------------------------------------------------------------- /config.mk: -------------------------------------------------------------------------------- 1 | PREFIX = /usr/local 2 | 3 | ICONS ?= 1 4 | CFLAGS += -Iinclude -Wall -Wextra -pedantic -D_XOPEN_SOURCE=700 \ 5 | -DICONS=${ICONS} 6 | 7 | LDFLAGS = `pkg-config --libs ncursesw || pkg-config --libs ncurses` 8 | -------------------------------------------------------------------------------- /include/var.h: -------------------------------------------------------------------------------- 1 | #ifndef _VAR_H 2 | #define _VAR_H 3 | 4 | #include 5 | 6 | #define VAR_FALSE ((void*)0) 7 | #define VAR_TRUE ((void*)1) 8 | 9 | void var_init(void); 10 | bool var_set(char *, void *); 11 | void terminate_var(void); 12 | 13 | #endif /* _VAR_H */ 14 | -------------------------------------------------------------------------------- /include/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 3 | 4 | #define LAST_F (n_dir_entries > ((unsigned)LINES - 6) ? ((unsigned)LINES - 6) : n_dir_entries) 5 | 6 | 7 | void help(void); 8 | void sig_handler(int); 9 | 10 | 11 | extern size_t first_f, last_f, cursor; 12 | 13 | #endif /*_MAIN_H */ 14 | -------------------------------------------------------------------------------- /include/type.h: -------------------------------------------------------------------------------- 1 | #ifndef _TYPE_H 2 | #define _TYPE_H 3 | 4 | #include "dir.h" 5 | 6 | struct icon_pair { 7 | char * ext; 8 | char * icon; 9 | }; 10 | 11 | 12 | char * get_ext(char *); 13 | void lowers(char *); 14 | enum mime_type_t get_mime(char *); 15 | char * get_icon(struct dir_entry_t *); 16 | 17 | #endif /* _TYPE_H */ 18 | -------------------------------------------------------------------------------- /include/info.h: -------------------------------------------------------------------------------- 1 | #ifndef _INFO_H 2 | #define _INFO_H 3 | 4 | #define INFO_TIMEOUT 5 // 5 second timeout for info messages 5 | 6 | enum info_t { 7 | INFO_INFO, 8 | INFO_WARN, 9 | INFO_ERR, 10 | }; 11 | 12 | 13 | void info_init(void); 14 | void display_info(enum info_t, char *, ...); 15 | void refresh_info(void); 16 | void page_info(void); 17 | 18 | #endif /* _INFO_H */ 19 | -------------------------------------------------------------------------------- /include/commands.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMMANDS_H 2 | #define _COMMANDS_H 3 | 4 | void ext_open(char *); 5 | long lit_search(long, char *); 6 | long search_file(long, char *); 7 | void create_cuts(char *, char **); 8 | void free_cuts(void); 9 | void paste_cuts(char *); 10 | void run_cmd(char *); 11 | void set(char *); 12 | void unset(char *); 13 | void open_cur_file(void); 14 | 15 | extern bool cutting; 16 | extern char * cut_start_dir; 17 | extern char ** cuts; 18 | 19 | #endif /* _COMMANDS_H */ 20 | -------------------------------------------------------------------------------- /include/hash.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_H 2 | #define _HASH_H 3 | 4 | #include 5 | 6 | 7 | typedef struct BucketData { 8 | struct BucketData * next; 9 | char * key; 10 | void (*value)(void *); 11 | } bucket_data; 12 | 13 | typedef struct Bucket { 14 | uint32_t hash; 15 | bucket_data * data; 16 | } bucket; 17 | 18 | typedef struct Map { 19 | uint32_t length; 20 | bucket ** arr; // list of pointers to buckets 21 | } map; 22 | 23 | 24 | map * map_new(size_t n); 25 | void map_insert(map *, char *, void (*)(void *)); 26 | void map_nuke(map *); 27 | void (*map_index(map *, char *))(void *); 28 | 29 | #endif /* _HASH_H */ 30 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | include config.mk 4 | 5 | BIN = cscroll 6 | SRC = src/commands.c src/dir.c src/hash.c src/io.c src/main.c \ 7 | src/opts.c src/type.c src/var.c src/info.c 8 | OBJ = ${SRC:.c=.o} 9 | 10 | CC ?= cc 11 | 12 | all: ${BIN} 13 | 14 | ${BIN}: ${OBJ} 15 | ${CC} ${CFLAGS} ${OBJ} -o $@ ${LDFLAGS} 16 | 17 | src/.c.o: 18 | ${CC} -c ${CFLAGS} $< 19 | 20 | clean: 21 | rm -f ${BIN} ${OBJ} 22 | 23 | install: all 24 | mkdir -p ${DESTDIR}${PREFIX}/bin 25 | install -Dm755 ${BIN} ${DESTDIR}${PREFIX}/bin/${BIN} 26 | 27 | uninstall: 28 | rm -f ${DESTDIR}${PREFIX}/bin/${BIN} 29 | 30 | .PHONY: all clean install uninstall 31 | -------------------------------------------------------------------------------- /src/type.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "dir.h" 5 | #include "type.h" 6 | #include "icons.h" 7 | 8 | 9 | char * get_ext(char * s) { 10 | char * ns = strrchr(s, '.'); 11 | if (!ns) return NULL; 12 | return ns + 1; 13 | } 14 | 15 | 16 | void lowers(char * s) { 17 | for (char * p = s; *p; p++) 18 | // A to Z 19 | if (*p >= 65 && *p <= 90) 20 | *p += 32; // convert upper to lower 21 | } 22 | 23 | enum mime_type_t get_mime(char * file) { 24 | char * t_ext = get_ext(file); 25 | if (!t_ext) return MIME_UNKNOWN; 26 | 27 | char * ext = malloc(strlen(t_ext) + 1); 28 | strcpy(ext, t_ext); 29 | lowers(ext); 30 | 31 | struct icon_pair * t = 32 | bsearch(&ext, icons, n_icons, sizeof(icons[0]), icmp); 33 | free(ext); 34 | if (!t) return MIME_UNKNOWN; 35 | 36 | if (!strcmp(t->icon, ICON_ARCHIVE)) 37 | return MIME_ARCHIVE; 38 | else if (!strcmp(t->icon, ICON_AUDIO) || 39 | !strcmp(t->icon, ICON_VIDEO) || 40 | !strcmp(t->icon, ICON_IMAGE)) 41 | return MIME_MEDIA; 42 | else return MIME_UNKNOWN; 43 | } 44 | 45 | #if ICONS 46 | char * get_icon(struct dir_entry_t * f) { 47 | char * t_ext = get_ext(f->name); 48 | struct icon_pair * t = NULL; 49 | if (t_ext) { 50 | char * ext = malloc(strlen(t_ext) + 1); 51 | strcpy(ext, t_ext); 52 | lowers(ext); 53 | 54 | t = bsearch(&ext, icons, n_icons, sizeof(icons[0]), icmp); 55 | free(ext); 56 | } 57 | 58 | if (!t) { 59 | if (f->file_type == FILE_DIR) return ICON_DIR; 60 | if (f->mode & POWNER(M_EXEC)) return ICON_GEAR; 61 | return ICON_GENERIC; 62 | } 63 | return t->icon; 64 | } 65 | #endif 66 | -------------------------------------------------------------------------------- /include/opts.h: -------------------------------------------------------------------------------- 1 | #ifndef _OPTS_H 2 | #define _OPTS_H 3 | 4 | #include 5 | #include 6 | 7 | // RGB values can be up to 10 bits (<= 1000) 8 | #define RGB(R, G, B) (((R << 20) | (G << 10)) | B) 9 | #define GET_RGB(C) ((C >> 20) & 0x3FF), ((C >> 10) & 0x3FF), (C & 0x3FF) 10 | 11 | #define COLOR_DEFAULT 0xFFFFFFFF 12 | 13 | enum var_stat { 14 | VAR_STAT_OK, 15 | VAR_STAT_NOEQ, // no equals sign 16 | VAR_STAT_NOTYPE, 17 | }; 18 | 19 | struct opener_t { 20 | char * fpath; 21 | size_t nlen; 22 | }; 23 | 24 | #define ICONS_VAR "icons" 25 | extern bool show_icons; 26 | #define DOTS_VAR "dots" 27 | extern bool show_dot_files; 28 | #define COLOR_VAR "color" 29 | extern bool color; 30 | #define LONG_VAR "long" 31 | extern bool p_long; 32 | #define DIR_COLOR_VAR "dir_color" 33 | extern uint32_t dir_color; 34 | #define REG_COLOR_VAR "reg_color" 35 | extern uint32_t reg_color; 36 | #define FIFO_COLOR_VAR "fifo_color" 37 | extern uint32_t fifo_color; 38 | #define LINK_COLOR_VAR "link_color" 39 | extern uint32_t link_color; 40 | #define BLK_COLOR_VAR "block_color" 41 | extern uint32_t blk_color; 42 | #define CHR_COLOR_VAR "char_color" 43 | extern uint32_t chr_color; 44 | #define SOCK_COLOR_VAR "sock_color" 45 | extern uint32_t sock_color; 46 | #define UNKNOWN_COLOR_VAR "unknown_color" 47 | extern uint32_t unknown_color; 48 | #define EXEC_COLOR_VAR "exec_color" 49 | extern uint32_t exec_color; 50 | #define MEDIA_COLOR_VAR "media_color" 51 | extern uint32_t media_color; 52 | #define ARCHIVE_COLOR_VAR "archive_color" 53 | extern uint32_t archive_color; 54 | #define OPENER_VAR "opener" 55 | extern struct opener_t opener; 56 | 57 | extern uint32_t custom_colors[12]; 58 | 59 | extern bool oneshot; 60 | extern bool show_dot_dirs; 61 | 62 | 63 | bool check_config(void); 64 | void create_config(void); 65 | void read_config(void); 66 | void terminate_opts(void); 67 | void generate_colors(void); 68 | enum var_stat parse_var(char *); 69 | 70 | #endif /* _OPTS_H */ 71 | -------------------------------------------------------------------------------- /include/dir.h: -------------------------------------------------------------------------------- 1 | #ifndef _DIR_H 2 | #define _DIR_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define MOWNER(M) (M >> 8) 9 | #define MGROUP(M) (M >> 4) 10 | #define POWNER(M) (M << 8) 11 | #define PGROUP(M) (M << 4) 12 | 13 | #define M_EXEC (1 << 0) 14 | #define M_WRITE (1 << 1) 15 | #define M_READ (1 << 2) 16 | #define M_SUID (1 << 3) 17 | 18 | 19 | #ifdef __BIONIC__ 20 | #define NFTW_NFDS 1 21 | #else 22 | #define NFTW_NFDS 0 23 | #endif 24 | 25 | 26 | enum file_type_t { 27 | FILE_REG, 28 | FILE_DIR, 29 | FILE_FIFO, 30 | FILE_LINK, 31 | FILE_BLK, 32 | FILE_CHR, 33 | FILE_SOCK, 34 | FILE_UNKNOWN 35 | }; 36 | 37 | enum mime_type_t { 38 | MIME_UNKNOWN, 39 | MIME_MEDIA, 40 | MIME_ARCHIVE 41 | }; 42 | 43 | enum f_size { 44 | B = 0, 45 | KB = 1, 46 | MB = 2, 47 | GB = 3, 48 | TB = 4, 49 | PB = 5, 50 | }; 51 | 52 | struct dir_entry_t { 53 | char * name; 54 | enum file_type_t file_type; 55 | enum file_type_t under_link; 56 | enum mime_type_t m_type; 57 | bool marked; 58 | 59 | uint16_t mode; 60 | time_t mtime; 61 | long owner; 62 | long group; 63 | size_t size; 64 | enum f_size u_size; 65 | 66 | // oneshot args 67 | bool last_in_col; 68 | }; 69 | 70 | 71 | int list_dir(char *); 72 | void free_dir_entries(void); 73 | void cd_back(void); 74 | void enter_dir(char *); 75 | int remove_file(struct dir_entry_t *); 76 | void remove_marked(void); 77 | char * mode_to_s(struct dir_entry_t *); 78 | bool check_dpath(char *); 79 | struct dir_entry_t * gen_dir_entry(char *, char *); 80 | void get_home(void); 81 | 82 | 83 | // number of directory entries 84 | extern size_t n_dir_entries; 85 | // longest owner name in dir 86 | extern size_t dir_longest_owner; 87 | // longest group name in dir 88 | extern size_t dir_longest_group; 89 | // actual directory entries 90 | extern struct dir_entry_t ** dir_entries; 91 | 92 | // current working directory 93 | extern char * cwd; 94 | extern char * homedir; 95 | extern size_t homedir_len; 96 | extern bool in_home_subdir; 97 | 98 | extern bool permission_denied; 99 | extern bool cwd_is_file; 100 | 101 | #endif /* _DIR_H */ 102 | -------------------------------------------------------------------------------- /include/io.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "dir.h" 9 | 10 | #define NO_IDENT 0 11 | 12 | 13 | #define ESC "\033" 14 | #define ANSI_RED ESC "[31m" 15 | #define ANSI_GREEN ESC "[32m" 16 | #define ANSI_YELLOW ESC "[33m" 17 | #define ANSI_BLUE ESC "[34m" 18 | #define ANSI_MAGENTA ESC "[35m" 19 | #define ANSI_CYAN ESC "[36m" 20 | #define ANSI_WHITE ESC "[37m" 21 | #define ANSI_RESET ESC "[0m" 22 | 23 | 24 | #define UP_KEYS \ 25 | KEY_UP: \ 26 | case CTRL_P: \ 27 | case 'k' 28 | 29 | #define DOWN_KEYS \ 30 | KEY_DOWN: \ 31 | case CTRL_N: \ 32 | case 'j' 33 | 34 | #define LEFT_KEYS \ 35 | KEY_LEFT: \ 36 | case CTRL_B: \ 37 | case 'h' 38 | 39 | #define RIGHT_KEYS \ 40 | KEY_RIGHT: \ 41 | case CTRL_F: \ 42 | case 'l' 43 | 44 | 45 | enum colors { 46 | COLOR_DIR = 1, 47 | COLOR_LINK = 2, 48 | COLOR_EXEC = 3, 49 | COLOR_SOCK = 4, 50 | COLOR_FIFO = 5, 51 | COLOR_UNKNOWN = 6, 52 | COLOR_FILE = 7, 53 | COLOR_BLOCK = 8, 54 | COLOR_CHAR = 9, 55 | COLOR_MEDIA = 10, 56 | COLOR_ARCHIVE = 11, 57 | 58 | CUSTOM_DIR = 12, 59 | CUSTOM_LINK = 13, 60 | CUSTOM_EXEC = 14, 61 | CUSTOM_SOCK = 15, 62 | CUSTOM_FIFO = 16, 63 | CUSTOM_UNKNOWN = 17, 64 | CUSTOM_FILE = 18, 65 | CUSTOM_BLOCK = 19, 66 | CUSTOM_CHR = 20, 67 | CUSTOM_MEDIA = 21, 68 | CUSTOM_ARCHIVE = 22, 69 | 70 | RED = 23, 71 | WHITE = 24, 72 | YELLOW = 25, 73 | MAGENTA = 26, 74 | }; 75 | 76 | enum keys { 77 | CTRL_B = 2, 78 | CTRL_C = 3, 79 | CTRL_F = 6, 80 | CTRL_N = 14, 81 | CTRL_P = 16, 82 | CTRL_Z = 26, 83 | KEY_ESC = 27, 84 | KEY_DEL = 127, 85 | }; 86 | 87 | 88 | void curses_init(void); 89 | void terminate_curses(void); 90 | void curses_write_file(struct dir_entry_t *, bool); 91 | char * prompt(char *, char **); 92 | char * curses_getline(char *); 93 | void unmark_all(void); 94 | void mark_all(void); 95 | void set_color(void); 96 | void print_mode(struct dir_entry_t *); 97 | void padstr(size_t); 98 | void resize_fbuf(void); 99 | void resize_fbufcur(long); 100 | void print_oneshot(void); 101 | enum colors get_file_color(struct dir_entry_t *); 102 | char get_file_ident(struct dir_entry_t *); 103 | size_t get_ilen(long, int); 104 | char * get_oname(struct dir_entry_t *); 105 | char * get_gname(struct dir_entry_t *); 106 | void print_long_info(struct dir_entry_t *); 107 | void print_file_name(struct dir_entry_t *, bool); 108 | int putsnonl(const char *); 109 | int addch_signed(int c); 110 | 111 | 112 | extern int (*i_putc)(int); 113 | extern int (*i_puts)(const char *); 114 | extern int (*i_printf)(const char *, ...); 115 | 116 | extern bool print_path; 117 | extern int stdout_back; 118 | extern size_t n_marked_files; 119 | 120 | #endif /* _IO_H */ 121 | -------------------------------------------------------------------------------- /src/hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "hash.h" 7 | 8 | 9 | // jenkins one at a time hash 10 | static uint32_t map_hashs(char * s) { 11 | uint32_t hash; 12 | char * i; 13 | for(hash = 0, i = s; *i; i++) 14 | { 15 | hash += *i; 16 | hash += (hash << 10); 17 | hash ^= (hash >> 6); 18 | } 19 | hash += (hash << 3); 20 | hash ^= (hash >> 11); 21 | hash += (hash << 15); 22 | return hash; 23 | } 24 | 25 | 26 | static bucket_data * map_new_data(void) { 27 | bucket_data * a = malloc(sizeof(bucket_data)); 28 | 29 | a->next = NULL; 30 | a->key = NULL; 31 | a->value = NULL; 32 | 33 | return a; 34 | } 35 | 36 | 37 | static bucket * map_new_bucket(void) { 38 | bucket * a = malloc(sizeof(bucket)); 39 | 40 | a->hash = 0; 41 | a->data = NULL; 42 | 43 | return a; 44 | } 45 | 46 | 47 | map * map_new(size_t n) { 48 | map * m = malloc(sizeof(map)); 49 | 50 | m->arr = malloc(sizeof(bucket *) * (n + 1)); 51 | m->length = n; 52 | 53 | // initialize array 54 | for (size_t i = 0; i < n; i++) { 55 | m->arr[i] = NULL; 56 | } 57 | 58 | return m; 59 | } 60 | 61 | 62 | void map_insert(map * m, char * k, void (*v)(void *)) { 63 | long klen = strlen(k); 64 | uint32_t khash = map_hashs(k); 65 | 66 | // hash already exists, add to bucket 67 | if (m->length > 0 && m->arr[khash % m->length]) { 68 | bucket_data * d = map_new_data(); 69 | 70 | d->key = malloc(klen + 1); 71 | strcpy(d->key, k); 72 | 73 | d->value = v; 74 | 75 | // replace top node with new node 76 | d->next = m->arr[khash % m->length]->data; 77 | m->arr[khash % m->length]->data = d; 78 | return; 79 | } 80 | 81 | // need to create new bucket 82 | bucket_data * d = map_new_data(); 83 | 84 | d->key = malloc(klen + 1); 85 | strcpy(d->key, k); 86 | d->value = v; 87 | 88 | bucket * b = map_new_bucket(); 89 | 90 | b->hash = khash; 91 | b->data = d; 92 | 93 | m->arr[khash % m->length] = b; 94 | 95 | return; 96 | } 97 | 98 | 99 | void map_nuke(map * m) { 100 | // loop over array 101 | for (size_t i = 0; i < m->length; i++) { 102 | if (!m->arr[i]) continue; 103 | // first free keys and linked list 104 | for (bucket_data * p = m->arr[i]->data; p;) { 105 | bucket_data * tmp = p->next; 106 | free(p->key); 107 | free(p); 108 | p = tmp; 109 | } 110 | // then free bucket 111 | free(m->arr[i]); 112 | } 113 | // free array 114 | free(m->arr); 115 | // finally free map itself 116 | free(m); 117 | } 118 | 119 | 120 | void (*map_index(map * m, char * k))(void *) { 121 | uint32_t khash = map_hashs(k); 122 | bucket_data * p; 123 | // if hash exists ... 124 | if (m->arr[khash % m->length]) 125 | // ... then search bucket for key 126 | for (p = m->arr[khash % m->length]->data; p; p = p->next) 127 | if (!strcmp(p->key, k)) return p->value; 128 | return NULL; 129 | } 130 | -------------------------------------------------------------------------------- /src/info.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "info.h" 9 | #include "io.h" 10 | 11 | 12 | struct info_node { 13 | enum info_t type; 14 | char * msg; 15 | time_t start; 16 | bool disp; 17 | }; 18 | 19 | 20 | static struct { 21 | WINDOW * w; 22 | struct info_node ** i; 23 | size_t n; 24 | } info_buffer = {NULL, NULL, 0}; 25 | 26 | 27 | static int get_info_color(struct info_node * n) { 28 | switch (n->type) { 29 | default: 30 | case INFO_INFO: return COLOR_PAIR(WHITE); 31 | case INFO_WARN: return COLOR_PAIR(YELLOW) | A_REVERSE; 32 | case INFO_ERR: return COLOR_PAIR(RED) | A_REVERSE; 33 | } 34 | } 35 | 36 | 37 | void info_init(void) { 38 | info_buffer.w = newwin(1, COLS, LINES - 1, 0); 39 | wclear(info_buffer.w); 40 | refresh(); 41 | } 42 | 43 | 44 | void display_info(enum info_t type, char * fmt, ...) { 45 | if (!info_buffer.i) info_buffer.i = malloc(sizeof(struct info_node*) * 32); 46 | 47 | // realloc every 32 48 | if ((info_buffer.n + 1) % 32 == 0) { 49 | info_buffer.i = realloc(info_buffer.i, 50 | (info_buffer.n + 32) * sizeof(struct info_node*)); 51 | } 52 | 53 | size_t n = info_buffer.n; 54 | info_buffer.i[n] = malloc(sizeof(struct info_node)); 55 | info_buffer.i[n]->type = type; 56 | 57 | va_list vlist; 58 | va_start(vlist, fmt); 59 | size_t nl = vsnprintf(NULL, 0, fmt, vlist); 60 | va_end(vlist); 61 | 62 | info_buffer.i[n]->msg = malloc(nl + 1); 63 | 64 | va_start(vlist, fmt); 65 | vsprintf(info_buffer.i[n]->msg, fmt, vlist); 66 | va_end(vlist); 67 | 68 | 69 | info_buffer.i[n]->disp = true; 70 | info_buffer.i[n]->start = time(NULL); 71 | 72 | info_buffer.n++; 73 | refresh_info(); 74 | } 75 | 76 | 77 | void refresh_info(void) { 78 | size_t n = info_buffer.n - 1; 79 | if (info_buffer.n == 0 || !info_buffer.w || !info_buffer.i[n]->disp) return; 80 | 81 | if (time(NULL) - info_buffer.i[n]->start >= INFO_TIMEOUT) { 82 | info_buffer.i[n]->disp = false; 83 | return; 84 | } 85 | 86 | int cp = get_info_color(info_buffer.i[n]); 87 | werase(info_buffer.w); 88 | waddstr(info_buffer.w, "(:i) "); 89 | wattron(info_buffer.w, cp); 90 | // 5 for (:i) text, 3 for ..., 1 for exra space 91 | waddnstr(info_buffer.w, info_buffer.i[n]->msg, COLS - 9); 92 | if (strlen(info_buffer.i[n]->msg) + 8 >= (unsigned)COLS) { 93 | wattron(info_buffer.w, A_DIM); 94 | waddstr(info_buffer.w, "..."); 95 | wattroff(info_buffer.w, A_DIM); 96 | } 97 | wattroff(info_buffer.w, cp); 98 | wrefresh(info_buffer.w); 99 | } 100 | 101 | 102 | void page_info(void) { 103 | if (info_buffer.n == 0 || !info_buffer.w) return; 104 | 105 | clear(); 106 | 107 | size_t first_info = 0; 108 | size_t last_info = info_buffer.n - 1; 109 | if (last_info >= (unsigned)LINES - 1) last_info = LINES - 1; 110 | 111 | bool done = false; 112 | 113 | while (!done) { 114 | erase(); 115 | 116 | for (size_t i = first_info; i <= last_info; i++) { 117 | int cp = get_info_color(info_buffer.i[i]); 118 | attron(cp); 119 | addnstr(info_buffer.i[i]->msg, COLS - 4); 120 | if (strlen(info_buffer.i[i]->msg) >= (unsigned)COLS) { 121 | attron(A_DIM); 122 | addstr("..."); 123 | attroff(A_DIM); 124 | } 125 | attroff(cp); 126 | addch('\n'); 127 | } 128 | 129 | printw("LINES %lu-%lu/%lu, q to close\n", 130 | first_info + 1, last_info + 1, info_buffer.n); 131 | 132 | refresh(); 133 | 134 | int c = getch(); 135 | switch (c) { 136 | case UP_KEYS: 137 | if (first_info > 0) { 138 | first_info--; 139 | last_info--; 140 | } 141 | break; 142 | case DOWN_KEYS: 143 | if (last_info < info_buffer.n - 1) { 144 | first_info++; 145 | last_info++; 146 | } 147 | break; 148 | case KEY_RESIZE: 149 | // jump back to the top, it is fine. 150 | first_info = 0; 151 | last_info = info_buffer.n - 1; 152 | if (last_info >= (unsigned)LINES - 1) last_info = LINES - 1; 153 | // make sure the file buffer is resized too 154 | resize_fbuf(); 155 | break; 156 | case CTRL_C: 157 | case 'q': 158 | done = true; 159 | break; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/var.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "hash.h" 10 | #include "opts.h" 11 | #include "main.h" 12 | #include "var.h" 13 | #include "dir.h" 14 | #include "io.h" 15 | 16 | 17 | static map * var_map = NULL; 18 | 19 | 20 | // must be 6 characters long 21 | static uint32_t hextorgb(char * hex) { 22 | if (hex == VAR_FALSE || hex == VAR_TRUE) return COLOR_DEFAULT; 23 | 24 | char * p = hex; 25 | if (*p == '#') p++; 26 | 27 | char htod[] = { 28 | ['A'] = 10, ['B'] = 11, ['C'] = 12, 29 | ['D'] = 13, ['E'] = 14, ['F'] = 15 30 | }; 31 | 32 | uint16_t r, g, b; 33 | uint32_t dec = 0; 34 | 35 | for (; *p; p++) { 36 | char c = toupper(*p); 37 | if (c >= 'A' && c <= 'F') c = htod[(int)c]; 38 | else if (c >= '0' && c <= '9') c -= '0'; 39 | else return COLOR_DEFAULT; 40 | dec *= 16; 41 | dec += c; 42 | } 43 | 44 | // convert to 10 bit color over 8 bit color 45 | r = (float)((dec >> 16) & 0xFF) / 255.0 * 1000; 46 | g = (float)((dec >> 8) & 0xFF) / 255.0 * 1000; 47 | b = (float)(dec & 0xFF) / 255.0 * 1000; 48 | 49 | return RGB(r, g, b); 50 | } 51 | 52 | 53 | // void * p -> bool p 54 | static void set_icons(void * p) { 55 | show_icons = (bool)p; 56 | } 57 | 58 | 59 | static void set_color_f(void * p) { 60 | color = (bool)p; 61 | set_color(); 62 | } 63 | 64 | 65 | static void set_long(void * p) { 66 | p_long = (bool)p; 67 | } 68 | 69 | 70 | static void set_dots(void * p) { 71 | show_dot_files = (bool)p; 72 | 73 | free_dir_entries(); 74 | list_dir(cwd); 75 | cursor = 1; 76 | first_f = 0; 77 | last_f = LAST_F; 78 | } 79 | 80 | 81 | static void set_dir(void * p) { 82 | dir_color = hextorgb((char*)p); 83 | set_color(); 84 | } 85 | 86 | 87 | static void set_reg(void * p) { 88 | reg_color = hextorgb((char*)p); 89 | set_color(); 90 | } 91 | 92 | 93 | static void set_fifo(void * p) { 94 | fifo_color = hextorgb((char*)p); 95 | set_color(); 96 | } 97 | 98 | 99 | static void set_link(void * p) { 100 | link_color = hextorgb((char*)p); 101 | set_color(); 102 | } 103 | 104 | 105 | static void set_block(void * p) { 106 | blk_color = hextorgb((char*)p); 107 | set_color(); 108 | } 109 | 110 | 111 | static void set_char(void * p) { 112 | chr_color = hextorgb((char*)p); 113 | set_color(); 114 | } 115 | 116 | 117 | static void set_sock(void * p) { 118 | sock_color = hextorgb((char*)p); 119 | set_color(); 120 | } 121 | 122 | 123 | static void set_unknown(void * p) { 124 | unknown_color = hextorgb((char*)p); 125 | set_color(); 126 | } 127 | 128 | 129 | static void set_exec(void * p) { 130 | exec_color = hextorgb((char*)p); 131 | set_color(); 132 | } 133 | 134 | 135 | static void set_media(void * p) { 136 | media_color = hextorgb((char*)p); 137 | set_color(); 138 | } 139 | 140 | 141 | static void set_archive(void * p) { 142 | archive_color = hextorgb((char*)p); 143 | set_color(); 144 | } 145 | 146 | 147 | static void set_opener(void * p) { 148 | if (p == VAR_FALSE || p == VAR_TRUE) return; 149 | 150 | char * s = (char*)p; 151 | if (s && *s) { 152 | size_t l = strlen(s); 153 | if (l < opener.nlen) { 154 | opener.nlen = l; 155 | opener.fpath = realloc(opener.fpath, l + 1); 156 | strcpy(opener.fpath, s); 157 | } else if (l == opener.nlen) { 158 | strcpy(opener.fpath, s); 159 | } else { /* l > nlen */ 160 | opener.nlen = l; 161 | opener.fpath = realloc(opener.fpath, l + 1); 162 | strcpy(opener.fpath, s); 163 | } 164 | } else { 165 | if (opener.fpath) free(opener.fpath); 166 | opener.fpath = NULL; 167 | opener.nlen = 0; 168 | } 169 | } 170 | 171 | 172 | void var_init(void) { 173 | var_map = map_new(18); 174 | 175 | map_insert(var_map, ICONS_VAR, set_icons); 176 | map_insert(var_map, COLOR_VAR, set_color_f); 177 | map_insert(var_map, LONG_VAR, set_long); 178 | map_insert(var_map, DOTS_VAR, set_dots); 179 | 180 | map_insert(var_map, DIR_COLOR_VAR, set_dir); 181 | map_insert(var_map, REG_COLOR_VAR, set_reg); 182 | map_insert(var_map, FIFO_COLOR_VAR, set_fifo); 183 | map_insert(var_map, LINK_COLOR_VAR, set_link); 184 | map_insert(var_map, BLK_COLOR_VAR, set_block); 185 | map_insert(var_map, CHR_COLOR_VAR, set_char); 186 | map_insert(var_map, SOCK_COLOR_VAR, set_sock); 187 | map_insert(var_map, UNKNOWN_COLOR_VAR, set_unknown); 188 | map_insert(var_map, EXEC_COLOR_VAR, set_exec); 189 | map_insert(var_map, MEDIA_COLOR_VAR, set_media); 190 | map_insert(var_map, ARCHIVE_COLOR_VAR, set_archive); 191 | 192 | map_insert(var_map, OPENER_VAR, set_opener); 193 | } 194 | 195 | 196 | bool var_set(char * k, void * p) { 197 | void (*set_func)(void *) = map_index(var_map, k); 198 | if (!set_func) return false; 199 | 200 | set_func(p); 201 | return true; 202 | } 203 | 204 | 205 | void terminate_var(void) { 206 | map_nuke(var_map); 207 | free(opener.fpath); 208 | } 209 | -------------------------------------------------------------------------------- /src/commands.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "io.h" 10 | #include "dir.h" 11 | #include "var.h" 12 | #include "opts.h" 13 | #include "main.h" 14 | #include "info.h" 15 | #include "commands.h" 16 | 17 | 18 | bool cutting = false; 19 | char * cut_start_dir = NULL; 20 | char ** cuts = NULL; 21 | 22 | 23 | void ext_open(char * file) { 24 | clear(); 25 | refresh(); 26 | 27 | char * f = malloc(strlen(cwd) + strlen(file) + 2); 28 | sprintf(f, "%s/%s", cwd, file); 29 | 30 | endwin(); 31 | pid_t pid = fork(); 32 | if (!pid) { 33 | if (!opener.fpath){ 34 | #if defined(__APPLE__) || defined(__MACH__) 35 | execvp("open", (char*[3]){"open", f, NULL}); 36 | #else 37 | execvp("xdg-open", (char*[3]){"xdg-open", f, NULL}); 38 | #endif 39 | } else { 40 | execvp(opener.fpath, (char*[3]){opener.fpath, f, NULL}); 41 | } 42 | exit(0); 43 | } 44 | wait(NULL); 45 | free(f); 46 | 47 | initscr(); 48 | clear(); 49 | refresh(); 50 | } 51 | 52 | 53 | long lit_search(long c, char * s) { 54 | long ret = -1; 55 | for (long i = c; i < (signed)n_dir_entries; i++) { 56 | if (!strcmp(s, dir_entries[i]->name)) { 57 | ret = i; 58 | break; 59 | } 60 | } 61 | 62 | if (ret == -1 && c > 1) { 63 | for (long i = 0; i < c; i++) { 64 | if (!strcmp(s, dir_entries[i]->name)) { 65 | ret = i; 66 | break; 67 | } 68 | } 69 | } 70 | 71 | return ret; 72 | } 73 | 74 | 75 | long search_file(long c, char * s) { 76 | long ret = -1; 77 | regex_t r; 78 | regcomp(&r, s, REG_EXTENDED); 79 | for (long i = c; i < (signed)n_dir_entries; i++) { 80 | if (!regexec(&r, dir_entries[i]->name, 0, NULL, 0)) { 81 | ret = i; 82 | break; 83 | } 84 | } 85 | 86 | if (ret == -1 && c > 1) { 87 | for (long i = 0; i < c; i++) 88 | if (!regexec(&r, dir_entries[i]->name, 0, NULL, 0)) { 89 | ret = i; 90 | break; 91 | } 92 | } 93 | regfree(&r); 94 | return ret; 95 | } 96 | 97 | 98 | void create_cuts(char * wd, char ** ls) { 99 | cut_start_dir = malloc(strlen(wd) + 1); 100 | strcpy(cut_start_dir, wd); 101 | 102 | cutting = true; 103 | if (!cuts) cuts = malloc(0); 104 | // if not null, use passed list 105 | if (ls) { 106 | size_t total_cuts = 0; 107 | for (char ** p = ls; *p; p++) total_cuts++; 108 | cuts = realloc(cuts, sizeof(char*) * (total_cuts + 1)); 109 | size_t i; 110 | for (i = 0; i < total_cuts; i++) { 111 | cuts[i] = malloc(strlen(ls[i]) + 1); 112 | strcpy(cuts[i], ls[i]); 113 | } 114 | cuts[i] = NULL; 115 | return; 116 | } 117 | // else use marked files 118 | // count total number of cuts to make 119 | size_t total_cuts = 0; 120 | for (size_t i = 0; i < n_dir_entries; i++) 121 | if (dir_entries[i]->marked) total_cuts++; 122 | cuts = realloc(cuts, sizeof(char*) * (total_cuts + 1)); 123 | // store cuts into list 124 | char ** p = cuts; 125 | for (size_t i = 0; i < n_dir_entries; i++) { 126 | if (dir_entries[i]->marked) { 127 | *p = malloc(strlen(dir_entries[i]->name) + 1); 128 | strcpy(*p, dir_entries[i]->name); 129 | p++; 130 | } 131 | } 132 | *p = NULL; 133 | } 134 | 135 | 136 | void free_cuts(void) { 137 | cutting = false; 138 | for (char ** p = cuts; *p; p++) 139 | free(*p); 140 | free(cuts); 141 | cuts = NULL; 142 | 143 | free(cut_start_dir); 144 | cut_start_dir = NULL; 145 | n_marked_files = 0; 146 | } 147 | 148 | 149 | void paste_cuts(char * path) { 150 | cutting = false; 151 | for (char ** p = cuts; *p; p++) { 152 | char old_path[strlen(cut_start_dir) + strlen(*p) + 1]; 153 | char new_path[strlen(path) + strlen(*p) + 1]; 154 | sprintf(old_path, "%s/%s", cut_start_dir, *p); 155 | sprintf(new_path, "%s/%s", path, *p); 156 | rename(old_path, new_path); 157 | } 158 | } 159 | 160 | 161 | void run_cmd(char * cmd) { 162 | clear(); 163 | refresh(); 164 | endwin(); 165 | 166 | if (!fork()) { 167 | execvp("sh", (char*[]){"sh", "-c", cmd, NULL}); 168 | exit(0); 169 | } 170 | wait(NULL); 171 | 172 | puts("\nPress enter to continue"); 173 | while (fgetc(stdin) != '\n'); 174 | 175 | initscr(); 176 | clear(); 177 | refresh(); 178 | } 179 | 180 | 181 | void set(char * v) { 182 | if (!var_set(v, VAR_TRUE)) { 183 | display_info(INFO_WARN, "Unknown variable (%s)", v); 184 | } 185 | } 186 | 187 | 188 | void unset(char * v) { 189 | if (!var_set(v, VAR_FALSE)) { 190 | display_info(INFO_WARN, "Unknown variable (%s)", v); 191 | } 192 | } 193 | 194 | 195 | // attempt to "open" the file the cursor is on 196 | void open_cur_file(void) { 197 | // enter directtory/link pointing to dir 198 | if (dir_entries[cursor - 1]->file_type == FILE_DIR || 199 | dir_entries[cursor - 1]->under_link == FILE_DIR) { 200 | enter_dir(dir_entries[cursor - 1]->name); 201 | free_dir_entries(); 202 | list_dir(cwd); 203 | cursor = 1; 204 | first_f = 0; 205 | last_f = LAST_F; 206 | } else { 207 | ext_open(dir_entries[cursor - 1]->name); 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/opts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "info.h" 10 | #include "opts.h" 11 | #include "var.h" 12 | #include "io.h" 13 | 14 | #define CFG_FNAME "config" 15 | 16 | 17 | bool show_icons = true; 18 | bool show_dot_files = false; 19 | bool color = true; 20 | bool p_long = false; 21 | bool oneshot = false; 22 | bool show_dot_dirs = false; 23 | struct opener_t opener = {NULL, 0}; 24 | 25 | uint32_t custom_colors[12]; 26 | 27 | uint32_t dir_color = COLOR_DEFAULT; 28 | uint32_t link_color = COLOR_DEFAULT; 29 | uint32_t exec_color = COLOR_DEFAULT; 30 | uint32_t sock_color = COLOR_DEFAULT; 31 | uint32_t fifo_color = COLOR_DEFAULT; 32 | uint32_t unknown_color = COLOR_DEFAULT; 33 | uint32_t reg_color = COLOR_DEFAULT; 34 | uint32_t blk_color = COLOR_DEFAULT; 35 | uint32_t chr_color = COLOR_DEFAULT; 36 | uint32_t media_color = COLOR_DEFAULT; 37 | uint32_t archive_color = COLOR_DEFAULT; 38 | 39 | static char * default_config_dir = NULL; 40 | static char * csc_config_path = NULL; 41 | static char * csc_config_file = NULL; 42 | 43 | 44 | bool check_config(void) { 45 | char * csc_dir = "cscroll"; 46 | int csc_len = 7; 47 | 48 | // find default config directory 49 | char * xdg_config = getenv("XDG_CONFIG_HOME"); 50 | char * cfg_path = NULL; 51 | if (!xdg_config) { 52 | cfg_path = malloc(strlen(homedir) + 9 + csc_len + 1); 53 | sprintf(cfg_path, "%s/.config", homedir); 54 | } else { 55 | cfg_path = malloc(strlen(xdg_config) + 2 + csc_len); 56 | strcpy(cfg_path, xdg_config); 57 | } 58 | 59 | // set default config path variable 60 | default_config_dir = malloc(strlen(cfg_path) + 1); 61 | strcpy(default_config_dir, cfg_path); 62 | 63 | // set cscroll config dir path 64 | strcat(cfg_path, "/"); strcat(cfg_path, csc_dir); 65 | csc_config_path = malloc(strlen(cfg_path) + 1); 66 | strcpy(csc_config_path, cfg_path); 67 | 68 | // set path to cscroll config file 69 | csc_config_file = malloc(strlen(csc_config_path) + 8); 70 | sprintf(csc_config_file, "%s/"CFG_FNAME, csc_config_path); 71 | 72 | free(cfg_path); 73 | 74 | struct stat st_buf; 75 | // either cscroll config file or dir does not exist 76 | if (stat(csc_config_path, &st_buf) < 0 || 77 | stat(csc_config_file, &st_buf) < 0) return false; 78 | return true; 79 | } 80 | 81 | 82 | void create_config(void) { 83 | struct stat st_buf; 84 | // set with mode rwxr-xr-x 85 | if (stat(default_config_dir, &st_buf) < 0) 86 | mkdir(default_config_dir, 0755); 87 | 88 | // set with mode rwxr-xr-x 89 | if (stat(csc_config_path, &st_buf) < 0) 90 | mkdir(csc_config_path, 0755); 91 | 92 | // create config file if it doesnt exist 93 | if (stat(csc_config_file, &st_buf) < 0) { 94 | FILE * fp = fopen(csc_config_file, "w"); 95 | if (fp) fclose(fp); 96 | } 97 | } 98 | 99 | 100 | enum var_stat parse_var(char * var) { 101 | char * line = var; 102 | // go past leading white space 103 | while (*var && isspace(*var)) var++; 104 | 105 | int n = 0; 106 | 107 | // add null terminator to var 108 | char * val = strchr(var, '='); 109 | if (!val) return VAR_STAT_NOEQ; 110 | 111 | while (val + n > line && isspace(val[--n])); 112 | val[n + 1] = 0; 113 | val++; 114 | 115 | // remove trailing white space after '=' 116 | while (*val && *val != '=' && isspace(*val)) val++; 117 | 118 | void * ptr_val = NULL; 119 | size_t vlen = strlen(val); 120 | 121 | if (!strcmp(val, "true")) { 122 | ptr_val = VAR_TRUE; 123 | } else if (!strcmp(val, "false")) { 124 | ptr_val = VAR_FALSE; 125 | } else if (vlen > 2 && val[0] == '"' && val[vlen - 1] == '"') { 126 | // empty strings not supported 127 | val++; 128 | 129 | // -2 to compensate for inc on prev line & for strlen 130 | val[vlen - 2] = 0; 131 | ptr_val = val; 132 | } else return VAR_STAT_NOTYPE; 133 | 134 | var_set(var, ptr_val); 135 | return VAR_STAT_OK; 136 | } 137 | 138 | 139 | void read_config(void) { 140 | FILE * fp = fopen(csc_config_file, "r"); 141 | 142 | size_t l = 1; 143 | bool done = false; 144 | // read by line 145 | char line[256]; 146 | 147 | while (!done) { 148 | short len = 0; // only needs to go up to 255 149 | 150 | // read line from file into buffer 151 | int c; 152 | while ((c = fgetc(fp)) != '\n') { 153 | if (len >= 255 || c == EOF) { 154 | done = c == EOF; // only done at EOF 155 | break; 156 | } 157 | line[len++] = c; 158 | } 159 | 160 | line[len] = '\0'; 161 | if (*line) switch (parse_var(line)) { 162 | case VAR_STAT_NOEQ: 163 | display_info(INFO_WARN, 164 | "Config line %lu: Missing '=' in variable definition", l); 165 | break; 166 | case VAR_STAT_NOTYPE: 167 | display_info(INFO_WARN, 168 | "Config line %lu: Missing or unknown type", l); 169 | break; 170 | default: 171 | case VAR_STAT_OK: 172 | break; 173 | } 174 | 175 | l++; 176 | } 177 | 178 | fclose(fp); 179 | } 180 | 181 | 182 | void terminate_opts(void) { 183 | free(default_config_dir); 184 | free(csc_config_path); 185 | free(csc_config_file); 186 | } 187 | 188 | 189 | void generate_colors(void) { 190 | custom_colors[COLOR_DIR] = dir_color; 191 | custom_colors[COLOR_LINK] = link_color; 192 | custom_colors[COLOR_EXEC] = exec_color; 193 | custom_colors[COLOR_SOCK] = sock_color; 194 | custom_colors[COLOR_FIFO] = fifo_color; 195 | custom_colors[COLOR_UNKNOWN] = unknown_color; 196 | custom_colors[COLOR_FILE] = reg_color; 197 | custom_colors[COLOR_BLOCK] = blk_color; 198 | custom_colors[COLOR_CHAR] = chr_color; 199 | custom_colors[COLOR_MEDIA] = media_color; 200 | custom_colors[COLOR_ARCHIVE] = archive_color; 201 | } 202 | -------------------------------------------------------------------------------- /include/icons.h: -------------------------------------------------------------------------------- 1 | #ifndef _ICONS_H 2 | #define _ICONS_H 3 | 4 | #include "type.h" 5 | 6 | #define ICON_DIR "\uf413" 7 | #define ICON_GEAR "\uf013" 8 | 9 | #define ICON_APK "\ue70e" 10 | #define ICON_EXEC ICON_GEAR 11 | #define ICON_SHELL "\uf489" 12 | #define ICON_C "\ue61e" 13 | #define ICON_CPP "\ue61d" 14 | #define ICON_CS "\ue648" 15 | #define ICON_CSS "\ue749" 16 | #define ICON_FSHARP "\ue7a7" 17 | #define ICON_XML "\ue60e" 18 | #define ICON_RB "\ue739" 19 | #define ICON_LUA "\ue620" 20 | #define ICON_GIT "\ue702" 21 | #define ICON_GO "\ue626" 22 | #define ICON_HTML "\ue736" 23 | #define ICON_JAVA "\ue738" 24 | #define ICON_JS "\ue781" 25 | #define ICON_JSON "\ue60b" 26 | #define ICON_PY "\ue73c" 27 | #define ICON_SCALA "\ue737" 28 | #define ICON_MD "\ue609" 29 | #define ICON_VIM "\ue62b" 30 | 31 | #define ICON_AUDIO "\uf1c7" 32 | #define ICON_VIDEO "\uf1c8" 33 | #define ICON_IMAGE "\uf1c5" 34 | #define ICON_GENERIC "\uf016" 35 | #define ICON_ARCHIVE "\uf1c6" 36 | 37 | struct icon_pair icons[] = { 38 | {"1", ICON_ARCHIVE}, 39 | {"3g2", ICON_VIDEO}, 40 | {"3gp", ICON_VIDEO}, 41 | {"7z", ICON_ARCHIVE}, 42 | {"7zip",ICON_ARCHIVE}, 43 | /* A */ 44 | {"a", ICON_ARCHIVE}, 45 | {"aac", ICON_AUDIO}, 46 | {"ac3", ICON_AUDIO}, 47 | {"ai", ICON_IMAGE}, 48 | {"aif", ICON_AUDIO}, 49 | {"alz", ICON_ARCHIVE}, 50 | {"amv", ICON_VIDEO}, 51 | {"apk", ICON_APK}, 52 | {"asec",ICON_APK}, 53 | {"asf", ICON_VIDEO}, 54 | {"asm", ICON_GEAR}, 55 | {"avi", ICON_VIDEO}, 56 | /* B */ 57 | {"bash",ICON_SHELL}, 58 | {"bin", ICON_EXEC}, 59 | {"bmp", ICON_IMAGE}, 60 | {"bz2", ICON_ARCHIVE}, 61 | /* C */ 62 | {"c", ICON_C}, 63 | {"c++", ICON_CPP}, 64 | {"cc", ICON_CPP}, 65 | {"cbr", ICON_ARCHIVE}, 66 | {"cda", ICON_AUDIO}, 67 | {"class",ICON_JAVA}, 68 | {"cpgz",ICON_ARCHIVE}, 69 | {"cs", ICON_CS}, 70 | {"cso", ICON_ARCHIVE}, 71 | {"css", ICON_CSS}, 72 | /* D */ 73 | {"dar", ICON_ARCHIVE}, 74 | {"dbz", ICON_ARCHIVE}, 75 | {"deb", ICON_ARCHIVE}, 76 | {"drc", ICON_VIDEO}, 77 | {"dz", ICON_ARCHIVE}, 78 | /* E */ 79 | {"ear", ICON_ARCHIVE}, 80 | {"eps", ICON_IMAGE}, 81 | /* F */ 82 | {"f#", ICON_FSHARP}, 83 | {"f4a", ICON_AUDIO}, 84 | {"f4b", ICON_AUDIO}, 85 | {"f4p", ICON_VIDEO}, 86 | {"f4v", ICON_VIDEO}, 87 | {"flv", ICON_VIDEO}, 88 | {"fish", ICON_SHELL}, 89 | /* G */ 90 | {"gif", ICON_IMAGE}, 91 | {"gifv",ICON_VIDEO}, 92 | {"gip", ICON_ARCHIVE}, 93 | {"git", ICON_GIT}, 94 | {"go", ICON_GO}, 95 | {"gz", ICON_ARCHIVE}, 96 | /* H */ 97 | {"h", ICON_C}, 98 | {"h264",ICON_VIDEO}, 99 | {"heif",ICON_IMAGE}, 100 | {"hh", ICON_CPP}, 101 | {"hpp", ICON_CPP}, 102 | {"htm", ICON_HTML}, 103 | {"html",ICON_HTML}, 104 | {"htmlz",ICON_ARCHIVE}, 105 | /* I */ 106 | {"ico", ICON_IMAGE}, 107 | {"igz", ICON_ARCHIVE}, 108 | {"ipa", ICON_ARCHIVE}, 109 | /* J */ 110 | {"jar", ICON_JAVA}, 111 | {"java",ICON_JAVA}, 112 | {"jpeg",ICON_IMAGE}, 113 | {"jpg", ICON_IMAGE}, 114 | {"js", ICON_JS}, 115 | {"json",ICON_JSON}, 116 | /* K */ 117 | /* L */ 118 | {"lua", ICON_LUA}, 119 | {"lz", ICON_ARCHIVE}, 120 | {"lz4", ICON_ARCHIVE}, 121 | /* M */ 122 | {"m2ts",ICON_VIDEO}, 123 | {"m2v", ICON_VIDEO}, 124 | {"m4a", ICON_AUDIO}, 125 | {"m4p", ICON_AUDIO}, 126 | {"m4v", ICON_VIDEO}, 127 | {"maff",ICON_ARCHIVE}, 128 | {"md", ICON_MD}, 129 | {"mid", ICON_AUDIO}, 130 | {"midi",ICON_AUDIO}, 131 | {"mkv", ICON_VIDEO}, 132 | {"mng", ICON_IMAGE}, 133 | {"mov", ICON_VIDEO}, 134 | {"mp3", ICON_AUDIO}, 135 | {"mp4", ICON_VIDEO}, 136 | {"mpeg",ICON_VIDEO}, 137 | {"mpg", ICON_VIDEO}, 138 | {"mpq", ICON_ARCHIVE}, 139 | {"mts", ICON_VIDEO}, 140 | {"mxf", ICON_VIDEO}, 141 | /* N */ 142 | {"npk", ICON_ARCHIVE}, 143 | {"nsv", ICON_VIDEO}, 144 | {"nxz", ICON_ARCHIVE}, 145 | /* O */ 146 | {"o", ICON_EXEC}, 147 | {"ogg", ICON_AUDIO}, 148 | {"ogv", ICON_VIDEO}, 149 | {"out", ICON_EXEC}, 150 | /* P */ 151 | {"pbm", ICON_IMAGE}, 152 | {"pgm", ICON_IMAGE}, 153 | {"pkg", ICON_ARCHIVE}, 154 | {"png", ICON_IMAGE}, 155 | {"pnm", ICON_IMAGE}, 156 | {"ppm", ICON_IMAGE}, 157 | {"ps", ICON_IMAGE}, 158 | {"psd", ICON_IMAGE}, 159 | {"pup", ICON_ARCHIVE}, 160 | {"py", ICON_PY}, 161 | {"pyc", ICON_PY}, 162 | {"pyd", ICON_PY}, 163 | {"pyo", ICON_PY}, 164 | {"pz", ICON_ARCHIVE}, 165 | {"pzip",ICON_ARCHIVE}, 166 | /* Q */ 167 | {"qt", ICON_VIDEO}, 168 | /* R */ 169 | {"rar", ICON_ARCHIVE}, 170 | {"rb", ICON_RB}, 171 | {"rm", ICON_VIDEO}, 172 | {"rmvb",ICON_VIDEO}, 173 | {"roq", ICON_VIDEO}, 174 | {"rpa", ICON_ARCHIVE}, 175 | {"rpm", ICON_ARCHIVE}, 176 | /* S */ 177 | {"sar", ICON_ARCHIVE}, 178 | {"scala",ICON_SCALA}, 179 | {"sh", ICON_SHELL}, 180 | {"shar",ICON_ARCHIVE}, 181 | {"sis", ICON_ARCHIVE}, 182 | {"sisx",ICON_ARCHIVE}, 183 | {"so", ICON_EXEC}, 184 | {"svg", ICON_IMAGE}, 185 | {"svi", ICON_VIDEO}, 186 | /* T */ 187 | {"tar", ICON_ARCHIVE}, 188 | {"tgz", ICON_ARCHIVE}, 189 | {"tif", ICON_IMAGE}, 190 | {"tiff",ICON_IMAGE}, 191 | {"ts", ICON_VIDEO}, 192 | {"txz", ICON_ARCHIVE}, 193 | /* U */ 194 | {"uha", ICON_ARCHIVE}, 195 | /* V */ 196 | {"vim", ICON_VIM}, 197 | {"vimrc",ICON_VIM}, 198 | {"viv", ICON_VIDEO}, 199 | {"vob", ICON_VIDEO}, 200 | {"vsix",ICON_ARCHIVE}, 201 | /* W */ 202 | {"wav", ICON_AUDIO}, 203 | {"webm",ICON_VIDEO}, 204 | {"webp",ICON_IMAGE}, 205 | {"wma", ICON_AUDIO}, 206 | {"wmv", ICON_VIDEO}, 207 | /* X */ 208 | {"xap", ICON_ARCHIVE}, 209 | {"xml", ICON_XML}, 210 | {"xz", ICON_ARCHIVE}, 211 | {"xzm", ICON_ARCHIVE}, 212 | {"xzn", ICON_ARCHIVE}, 213 | /* Y */ 214 | {"yuv", ICON_VIDEO}, 215 | /* Z */ 216 | {"z", ICON_ARCHIVE}, 217 | {"zab", ICON_ARCHIVE}, 218 | {"zi", ICON_ARCHIVE}, 219 | {"zip", ICON_ARCHIVE}, 220 | {"zlib",ICON_ARCHIVE}, 221 | {"zsh", ICON_SHELL}, 222 | {"zst", ICON_ARCHIVE}, 223 | {"zstd",ICON_ARCHIVE}, 224 | {"zxp", ICON_ARCHIVE}, 225 | {"zz", ICON_ARCHIVE}, 226 | }; 227 | 228 | #define n_icons (sizeof(icons)/sizeof(icons[0])) 229 | 230 | int icmp(const void * a, const void * b) { 231 | return strcmp(*(const char **)a, ((const struct icon_pair*)b)->ext); 232 | } 233 | 234 | #endif /* _ICONS_H */ 235 | -------------------------------------------------------------------------------- /src/dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "info.h" 15 | #include "type.h" 16 | #include "opts.h" 17 | #include "dir.h" 18 | #include "io.h" 19 | 20 | 21 | char * cwd = NULL; 22 | char * homedir = NULL; 23 | size_t homedir_len = 0; 24 | bool in_home_subdir = false; 25 | 26 | size_t n_dir_entries = 0; 27 | size_t dir_longest_owner = 0; 28 | size_t dir_longest_group = 0; 29 | struct dir_entry_t ** dir_entries = NULL; 30 | 31 | bool permission_denied = false; 32 | bool cwd_is_file = false; 33 | 34 | 35 | static int cmp(const void * a, const void * b) { 36 | const struct dir_entry_t * c = *(const struct dir_entry_t **)a; 37 | const struct dir_entry_t * d = *(const struct dir_entry_t **)b; 38 | 39 | // sort directories first 40 | if (c->file_type == FILE_DIR && d->file_type != FILE_DIR) 41 | return -1; 42 | else if (d->file_type == FILE_DIR && c->file_type != FILE_DIR) 43 | return 1; 44 | else 45 | return strcasecmp(c->name, d->name); 46 | } 47 | 48 | static int acmp(const void * a, const void * b) { 49 | return strcasecmp((*(const struct dir_entry_t**)a)->name, 50 | (*(const struct dir_entry_t**)b)->name); 51 | } 52 | 53 | 54 | struct dir_entry_t * gen_dir_entry(char * dir_path, char * d_name) { 55 | struct dir_entry_t * dir_entry = malloc(sizeof(struct dir_entry_t) + 12); 56 | 57 | size_t d_name_len = strlen(d_name); 58 | dir_entry->name = malloc(d_name_len + 1); 59 | strcpy(dir_entry->name, d_name); 60 | 61 | // figure out file 'mime' type 62 | dir_entry->m_type = get_mime(dir_entry->name); 63 | 64 | dir_entry->file_type = FILE_UNKNOWN; 65 | dir_entry->under_link = FILE_UNKNOWN; 66 | dir_entry->mode = 0; 67 | dir_entry->mtime = 0; 68 | dir_entry->owner = 0; 69 | dir_entry->group = 0; 70 | dir_entry->size = 0; 71 | dir_entry->u_size = B; 72 | 73 | dir_entry->marked = false; 74 | dir_entry->last_in_col = false; 75 | 76 | struct stat * buf = malloc(sizeof(struct stat)); 77 | char * tmp_path = malloc(d_name_len + strlen(dir_path) + 2); 78 | sprintf(tmp_path, "%s/%s", dir_path, d_name); 79 | if (lstat(tmp_path, buf) == -1) { 80 | free(buf); 81 | free(tmp_path); 82 | 83 | return dir_entry; 84 | } 85 | 86 | switch(buf->st_mode & S_IFMT) { 87 | case S_IFBLK: 88 | dir_entry->file_type = FILE_BLK; 89 | break; 90 | case S_IFCHR: 91 | dir_entry->file_type = FILE_CHR; 92 | break; 93 | case S_IFSOCK: 94 | dir_entry->file_type = FILE_SOCK; 95 | break; 96 | case S_IFDIR: 97 | dir_entry->file_type = FILE_DIR; 98 | break; 99 | case S_IFLNK: 100 | dir_entry->file_type = FILE_LINK; 101 | struct stat * buf2 = malloc(sizeof(struct stat)); 102 | stat(tmp_path, buf2); 103 | if ((buf2->st_mode & S_IFMT) == S_IFDIR) 104 | dir_entry->under_link = FILE_DIR; 105 | free(buf2); 106 | break; 107 | case S_IFIFO: 108 | dir_entry->file_type = FILE_FIFO; 109 | break; 110 | case S_IFREG: 111 | dir_entry->file_type = FILE_REG; 112 | break; 113 | default: 114 | dir_entry->file_type = FILE_UNKNOWN; 115 | break; 116 | } 117 | 118 | // other mode 119 | if (buf->st_mode & S_IROTH) 120 | dir_entry->mode |= M_READ; 121 | if (buf->st_mode & S_IWOTH) 122 | dir_entry->mode |= M_WRITE; 123 | if (buf->st_mode & S_IXOTH) 124 | dir_entry->mode |= M_EXEC; 125 | if (buf->st_mode & S_ISVTX) // sticky 126 | dir_entry->mode |= M_SUID; 127 | 128 | // group mode 129 | if (buf->st_mode & S_IRGRP) 130 | dir_entry->mode |= PGROUP(M_READ); 131 | if (buf->st_mode & S_IWGRP) 132 | dir_entry->mode |= PGROUP(M_WRITE); 133 | if (buf->st_mode & S_IXGRP) 134 | dir_entry->mode |= PGROUP(M_EXEC); 135 | if (buf->st_mode & S_ISGID) // suid; group 136 | dir_entry->mode |= PGROUP(M_SUID); 137 | 138 | // owner mode 139 | if (buf->st_mode & S_IRUSR) 140 | dir_entry->mode |= POWNER(M_READ); 141 | if (buf->st_mode & S_IWUSR) 142 | dir_entry->mode |= POWNER(M_WRITE); 143 | if (buf->st_mode & S_IXUSR) 144 | dir_entry->mode |= POWNER(M_EXEC); 145 | if (buf->st_mode & S_ISUID) // suid 146 | dir_entry->mode |= POWNER(M_SUID); 147 | 148 | 149 | #if defined(__APPLE__) || defined(__MACH__) 150 | dir_entry->mtime = buf->st_mtime; 151 | #else 152 | dir_entry->mtime = buf->st_mtim.tv_sec; 153 | #endif 154 | 155 | dir_entry->owner = buf->st_uid; 156 | dir_entry->group = buf->st_gid; 157 | 158 | struct passwd * pw = getpwuid(buf->st_uid); 159 | size_t pw_l = 0; 160 | if (!pw) pw_l = get_ilen(buf->st_uid, 10); 161 | else pw_l = strlen(pw->pw_name); 162 | 163 | struct group * gr = getgrgid(buf->st_gid); 164 | size_t gr_l = 0; 165 | if (!gr) gr_l = get_ilen(buf->st_gid, 10); 166 | else gr_l = strlen(gr->gr_name); 167 | 168 | if (pw_l > dir_longest_owner) dir_longest_owner = pw_l; 169 | if (gr_l > dir_longest_group) dir_longest_group = gr_l; 170 | 171 | dir_entry->size = buf->st_size; 172 | 173 | for (int i = 0; i <= PB; i++) { 174 | if (dir_entry->size < 1000) break; 175 | dir_entry->size /= 1000; 176 | dir_entry->u_size++; 177 | } 178 | 179 | free(tmp_path); 180 | free(buf); 181 | 182 | return dir_entry; 183 | } 184 | 185 | 186 | int list_dir(char * dir_path) { 187 | struct dirent * d_entry; 188 | DIR * dir = opendir(dir_path); 189 | 190 | n_dir_entries = 0; 191 | 192 | if (!dir) { 193 | if (errno == EACCES) 194 | permission_denied = true; 195 | return 1; 196 | } 197 | permission_denied = false; 198 | 199 | while ((d_entry = readdir(dir))) { 200 | char * d_name = d_entry->d_name; 201 | 202 | if ((!strcmp(d_name, ".") || !strcmp(d_name, "..")) 203 | && !show_dot_dirs) { 204 | continue; 205 | } else if (!show_dot_files && d_name[0] == '.') { 206 | continue; 207 | } 208 | 209 | struct dir_entry_t * dir_entry = gen_dir_entry(dir_path, d_name); 210 | 211 | dir_entries = realloc(dir_entries, sizeof(struct dir_entry_t*) * (n_dir_entries + 1)); 212 | dir_entries[n_dir_entries] = dir_entry; 213 | n_dir_entries++; 214 | } 215 | 216 | closedir(dir); 217 | 218 | qsort(dir_entries, n_dir_entries, sizeof(struct dir_entry_t*), cmp); 219 | size_t d_end = 0; 220 | for (size_t i = 0; i < n_dir_entries; i++) { 221 | if (dir_entries[i]->file_type != FILE_DIR) { 222 | d_end = i; 223 | break; 224 | } 225 | } 226 | qsort(dir_entries, d_end, sizeof(struct dir_entry_t*), acmp); 227 | qsort(dir_entries + d_end, n_dir_entries - d_end, 228 | sizeof(struct dir_entry_t*), acmp); 229 | 230 | 231 | return 0; 232 | } 233 | 234 | 235 | void free_dir_entries(void) { 236 | for (size_t i = 0; i < n_dir_entries; i++) { 237 | free(dir_entries[i]->name); 238 | free(dir_entries[i]); 239 | } 240 | 241 | n_dir_entries = 0; 242 | } 243 | 244 | 245 | void cd_back(void) { 246 | char * p = strrchr(cwd, '/'); 247 | *p = '\0'; 248 | cwd = realloc(cwd, strlen(cwd) + 2); 249 | if (cwd[0] == '\0') { 250 | cwd[0] = '/'; 251 | cwd[1] = '\0'; 252 | } 253 | chdir(cwd); 254 | 255 | dir_longest_owner = 0; 256 | dir_longest_group = 0; 257 | 258 | if (!strncmp(cwd, homedir, homedir_len)) in_home_subdir = true; 259 | else in_home_subdir = false; 260 | } 261 | 262 | 263 | void enter_dir(char * name) { 264 | char * tmp = malloc(strlen(cwd) + strlen(name) + 2); 265 | 266 | if (strcmp(cwd, "/")) 267 | sprintf(tmp, "%s/%s", cwd, name); 268 | else 269 | sprintf(tmp, "%s%s", cwd, name); 270 | 271 | if (chdir(tmp) == -1) { 272 | display_info(INFO_ERR, "%s", strerror(errno)); 273 | free(tmp); 274 | } else { 275 | free(cwd); 276 | cwd = tmp; 277 | 278 | dir_longest_owner = 0; 279 | dir_longest_group = 0; 280 | 281 | if (!strncmp(cwd, homedir, homedir_len)) in_home_subdir = true; 282 | else in_home_subdir = false; 283 | } 284 | } 285 | 286 | 287 | static size_t file_count; 288 | static int nftw_file_count(const char * fp, const struct stat * sb, int tf, struct FTW * fb) { 289 | (void)fp; 290 | (void)sb; 291 | (void)tf; 292 | (void)fb; 293 | 294 | file_count++; 295 | return 0; 296 | } 297 | 298 | static int remove_all_failed; 299 | static int nftw_file_remove(const char * fp, const struct stat * sb, int tf, struct FTW * fb) { 300 | (void)sb; 301 | (void)tf; 302 | (void)fb; 303 | 304 | if (remove(fp) < 0) { 305 | display_info(INFO_ERR, "%s: Remove failed (%s)", fp, strerror(errno)); 306 | remove_all_failed = 1; 307 | } 308 | 309 | return 0; 310 | } 311 | 312 | static size_t count_files(struct dir_entry_t * de) { 313 | file_count = 0; 314 | 315 | 316 | nftw(de->name, nftw_file_count, NFTW_NFDS, FTW_MOUNT | FTW_PHYS); 317 | 318 | return file_count; 319 | } 320 | 321 | 322 | static int remove_tree(struct dir_entry_t * de) { 323 | remove_all_failed = 0; 324 | 325 | nftw(de->name, nftw_file_remove, NFTW_NFDS, FTW_MOUNT | FTW_PHYS | FTW_DEPTH); 326 | 327 | return remove_all_failed; 328 | } 329 | 330 | 331 | int remove_file(struct dir_entry_t * de) { 332 | // returns 1 on error, 0 on success, -1 on no action 333 | if (de->file_type == FILE_DIR) { 334 | size_t f_count = count_files(de); 335 | 336 | // empty directories can be removed directly 337 | // (f_count of 1 means only the directory and nothing in it) 338 | if (f_count > 1) { 339 | char * REMOVE_FILE_PROMPT = "This action will remove the directory '%s' and all %zu files inside it. Continue?"; 340 | int plen = snprintf(NULL, 0, REMOVE_FILE_PROMPT, de->name, f_count - 1); 341 | char * p = malloc(plen + 1); 342 | snprintf(p, plen + 1, REMOVE_FILE_PROMPT, de->name, f_count - 1); 343 | 344 | char * r = prompt(p, (char*[]){"No", "Yes", NULL}); 345 | free(p); 346 | 347 | if (r && !strcmp(r, "Yes")) return remove_tree(de); 348 | return -1; 349 | } 350 | } 351 | 352 | int ret; 353 | if ((ret = remove(de->name)) < 0) { 354 | display_info(INFO_ERR, "%s: Remove failed (%s)", de->name, strerror(errno)); 355 | } 356 | 357 | return ret < 0 ? 1 : 0; 358 | } 359 | 360 | 361 | void remove_marked(void) { 362 | char * REMOVE_MARKED_PROMPT = "Remove all marked files? (%zu)"; 363 | int plen = snprintf(NULL, 0, REMOVE_MARKED_PROMPT, n_marked_files); 364 | char * p = malloc(plen + 1); 365 | snprintf(p, plen + 1, REMOVE_MARKED_PROMPT, n_marked_files); 366 | 367 | char * r = prompt(p, (char*[]){"No", "Yes", NULL}); 368 | if (!r || strcmp(r, "Yes")) return; 369 | 370 | for (size_t i = 0; i < n_dir_entries; i++) { 371 | if (dir_entries[i]->marked) { 372 | if (remove_file(dir_entries[i]) == 0) n_marked_files--; 373 | } 374 | } 375 | } 376 | 377 | 378 | char * mode_to_s(struct dir_entry_t * f) { 379 | char * s = malloc(11); 380 | 381 | if (f->file_type == FILE_UNKNOWN) { 382 | strcpy(s, "??????????"); 383 | return s; 384 | } 385 | 386 | char * p = s; 387 | uint16_t mode = f->mode; 388 | 389 | switch (f->file_type) { 390 | case FILE_BLK: *p++ = 'b'; break; 391 | case FILE_CHR: *p++ = 'c'; break; 392 | case FILE_DIR: *p++ = 'd'; break; 393 | case FILE_LINK: *p++ = 'l'; break; 394 | case FILE_FIFO: *p++ = '|'; break; 395 | case FILE_SOCK: *p++ = '='; break; 396 | default: *p++ = '.'; break; 397 | 398 | } 399 | // owner mode 400 | if (MOWNER(mode) & M_READ) *p++ = 'r'; 401 | else *p++ = '-'; 402 | if (MOWNER(mode) & M_WRITE) *p++ = 'w'; 403 | else *p++ = '-'; 404 | if (MOWNER(mode) & M_EXEC) { 405 | if (MOWNER(mode) & M_SUID) *p++ = 's'; 406 | else *p++ = 'x'; 407 | } else if (MOWNER(mode) & M_SUID) *p++ = 'S'; 408 | else *p++ = '-'; 409 | 410 | // group mode 411 | if (MGROUP(mode) & M_READ) *p++ = 'r'; 412 | else *p++ = '-'; 413 | if (MGROUP(mode) & M_WRITE) *p++ = 'w'; 414 | else *p++ = '-'; 415 | if (MGROUP(mode) & M_EXEC) { 416 | if (MGROUP(mode) & M_SUID) *p++ = 's'; 417 | else *p++ = 'x'; 418 | } else if (MGROUP(mode) & M_SUID) *p++ = 'S'; 419 | else *p++ = '-'; 420 | 421 | // other mode 422 | if (mode & M_READ) *p++ = 'r'; 423 | else *p++ = '-'; 424 | if (mode & M_WRITE) *p++ = 'w'; 425 | else *p++ = '-'; 426 | if (mode & M_EXEC) { 427 | if (mode & M_SUID) *p++ = 't'; 428 | else *p++ = 'x'; 429 | } else if (mode & M_SUID) *p++ = 'T'; 430 | else *p++ = '-'; 431 | 432 | *p++ = 0; 433 | 434 | return s; 435 | } 436 | 437 | 438 | // check if path exists & is a dir 439 | bool check_dpath(char * s) { 440 | struct stat buf; 441 | if (stat(s, &buf) == -1) return false; 442 | if ((buf.st_mode & S_IFMT) == S_IFDIR) return true; 443 | return false; 444 | } 445 | 446 | 447 | void get_home(void) { 448 | char * s = getenv("HOME"); 449 | // check if var exists & is a real dir 450 | if (!s || *s == '\0' || !check_dpath(s)) { 451 | struct passwd * pw = getpwuid(geteuid()); 452 | s = pw->pw_dir; 453 | } 454 | 455 | homedir = realpath(s, NULL); 456 | homedir_len = strlen(homedir); 457 | } 458 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cscroll 2 | 3 | A small and efficient file manager. 4 | 5 | ![Regular cscroll](https://github.com/Raniconduh/cscroll/assets/63197781/1d768a4a-734f-420e-a549-6157e8b0da5d) 6 | 7 | ![cscroll in long mode](https://github.com/Raniconduh/cscroll/assets/63197781/7a854651-0a02-495d-bb89-c7449385fca0) 8 | 9 | ## Usage 10 | 11 | If an argument is provided, cscroll will open the path supplied. Otherwise it will open in the current working directory. ([see 'Options'](#options)) 12 | 13 | Files will be highlighted and shown with an identifier corresponding to the file type. If compiled with icons and the icons option is set, a nerd icon corresponding to the file's type will be shown to its left. ([see 'Icons'](#icons)) 14 | 15 | Default configurations can be specified in the [config file](#config-file). These will be overwritten by any flags passed via the command line or through the `set` command but will persist in the file. 16 | 17 | #### Colors & Identifiers: 18 | 19 | * Red, `?`: Unknown File 20 | * Yellow, `#`: Block or char device 21 | * Yellow, `|`: FIFO (named pipe) 22 | * Green, `*`: File is executable 23 | * Blue, `/`: Directory 24 | * Cyan, `@`: Symbolic link 25 | * Magenta, `=`: Unix socket 26 | * Magenta, No identifier: Media file 27 | * Red, No identifier: Archive or compressed file 28 | * White, No identifier: Regular file 29 | 30 | Files that are executable but have another identifier will keep the identifier but be colored green. Files that are either media files or archives will be colored respectively but will keep an identifier if they have one. 31 | 32 | Symbolic links that point to directories will be suffixed with `@ => /` and may be entered as a normal directory. Otherwise, deletion of a symbolic link will not delete whatever the link points to; only the link itself and opening one will open what the link points to. 33 | 34 | File colors can also be customized. ([see variables](#variables)) 35 | 36 | Additionally, in long mode, the leftmost character in the mode string will correspond to file type: 37 | 38 | * `.`: Regular file 39 | * `b`: Block device 40 | * `c`: Character device 41 | * `d`: Directory 42 | * `l`: Symbolic link 43 | * `|`: FIFO/named pipe 44 | * `=`: Unix socket 45 | 46 | ### Options 47 | 48 | * `-n`: Negate the next option 49 | * `-A`: Show dotfiles in listings but exclude . and .. 50 | * `-a`: (Only available in oneshot mode) List all dotfiles including . and .. 51 | * `-c`: Use colors in listings (default) 52 | * `-h`: No-op: for compatibility with ls 53 | * `-i`: Show icons (default) 54 | * `-p`: Print the path cscroll ends in. Useful for commands like `cd $(cscroll -p)` to cd into the last directory 55 | * `-l`: Display files in long mode (file mode, owner, group, size, modification time) 56 | * `--help`: Show the help screen 57 | * `--oneshot`: cscroll will print the files and exit, acting as if it were `ls`. The `-p` option will have no effect in oneshot mode. 58 | * `--`: Stop parsing command line flags 59 | 60 | Options beginning with a single `-` may be stacked: e.g. `cscroll -la`. The `-n` option is useful with the `-i` and `-c` flags (`-ni`, `-nc`) to turn off icons or colors. The `--` option allows running cscroll in a directory which is prefixed with a `-`. E.g. `cscroll -- -mydir` will open cscroll in the directory `-mydir`. 61 | 62 | ### Commands 63 | 64 | * `j`, `Ctrl+n` or `down arrow key`: Move the cursor down 65 | * `k`, `Ctrl+p` or `up arrow key`: Move the cursor up 66 | * `h`, `Ctrl+b` or `left arrow key`: Enter the previous directory 67 | * `l`, `Ctrl+f`, `right arrow key`, or `enter key`: If the file the cursor is on is a directory, enter that directory. Otherwise open the file with `xdg-open` 68 | * `g`, `home key`: Place cursor on first file 69 | * `G`, `end key`: Place cursor on last file 70 | * `.`: Toggle whether or not to show dot files 71 | * `d`: Delete the file the cursor is on (a [prompt](#options-prompt) will be shown first; see [Deleting](#deleting)). 72 | * `m`: Mark the file the cursor is on 73 | * `r`: Rename the file the cursor is on ([see file renaming](#renaming)) 74 | * `c`: Cut the file the cursor is currently on or all the marked files. Pressing twice in the same directory will cancel the cut 75 | * `p`: Paste all cut files into the current directory. Pasting in the same directory where the cut originated will cancel the cut 76 | * `:`: Open a commands prompt ([see 'Command Prompt' section](#command-prompt)) 77 | * `/`: Search for a file in the current directory using a POSIX regex 78 | * `!`: Run a shell command ([see shell commands](#shell-commands)) 79 | * `q`: Quit 80 | 81 | A mouse click will move the cursor to file that was clicked on. If the cursor was already on that file, it will be opened or cscroll will enter it if it is a directory. 82 | 83 | #### Options Prompt 84 | 85 | An options prompt will pop up in the center of the screen with text at the top of the pop-up and one or more options at the bottom. To move the cursor to the left, any of the left or up keys will work. To move it to the right, any of the down or right keys (except for enter) will work. To select the current option, press either the space bar or the enter key. The `q` key will quit the prompt without selecting an option. 86 | 87 | #### Command Prompt 88 | 89 | The command prompt will show up upon pressing `:` and the prompt itself is prefixed with a colon. Here you may enter multi-character commands. Available commands are: 90 | 91 | * `ma`: **M**ark **A**ll files in the directory 92 | * `mu`: **M**ark **U**nmark: Unmarks all files on the directory 93 | * `ca`: **C**ut **A**ll files in the current directory 94 | 95 | * `set`: Set a variable to true ([see Variables](#variables)) 96 | * `unset`: Unset a variable (set to false, [see Variables](#variables)) 97 | * `var`: Set a variable equal to something `var variable = false` ([see Variables](#variables) and ['config file'](#config-file)) 98 | 99 | * `i`: Open the [info buffer](#info-buffer) viewer 100 | 101 | #### Deleting 102 | 103 | The `d` command will always ask for confirmation to delete a file. If numerous files are marked, the prompt will show the number of marked files (as those will all be deleted). Whenever cscroll attempts to delete a non-empty directory, it will prompt for confirmation to do a recursive removal and will show the number of files that will be deleted inside of that directory. This secondary prompt will be shown whenever a non-empty directory is being deleted. No secondary prompt will be shown if the directory is empty. 104 | 105 | Recursive removal may not work properly on some operating systems, primarily on Android since Bionic libc seems to have a hardcode nested directory limit that it will operate on. Most directories will work fine with this but ones with thousands of nested directories may not work at all. 106 | 107 | #### Renaming 108 | 109 | The `r` command will show a prompt (similar to a command prompt but without a prefix) where the new file name is to be expected. A file may only be renamed within the same directory. 110 | 111 | E.g. if the directory cscroll is in is `/home/user/downloads` and the file `image.png` is renamed to `/home/user/image.png`, it will fail. The file can only be renamed to something like `my_image.png`. (Attempting to move a file across directories is not possible with the rename function.) Mass renaming is not possible. 112 | 113 | #### Shell Commands 114 | 115 | Pressing `!` will open a prompt prefixed with `!` which will run the shell command entered into it. Entering `%f` will format the command entered with the name of the file the cursor is currently on. To escape this format (i.e. to not have it be replaced with the file name), enter `%%f`. The output will be literally `%f`. 116 | 117 | E.g. `vim %f` will format to `vim FILE` where `FILE` is the name of the file the cursor is on. `echo %%f`, however, will format to `echo %f` and the output of the command will literally be `%f`. 118 | 119 | #### Info Buffer 120 | 121 | The info buffer shows all information messages, warnings, and errors. The `:i` command opens a pager to view every message at once. To scroll up the buffer, any of the up keys will work. To scroll down, any of the down keys will work. To close the buffer, press `q` or `Ctrl+c`. 122 | 123 | When the info buffer is not opened, the latest info message will be shown at the bottom of the screen. 124 | 125 | Messages highlighted in red are errors, yellow are warnings, and messages without highlighting are general information. 126 | 127 | #### Config File 128 | 129 | The config file allows for the specification of default variables cscroll will always set. The default location of the file will be `$HOME/.config/cscroll/config`, but the default configuration directory can be changed with the `XDG_CONFIG_HOME` environment variable. 130 | 131 | The general syntax for configurations is `variable = value`. The variables that may be specified are the same as the ones the `set` command may take and are specified [here](#variables). 132 | 133 | The values that variables may be set to are limited to `true`, `false`, and a string in the case of a color variable. 134 | 135 | Example config file: 136 | 137 | ``` 138 | long = true 139 | icons = false 140 | dir_color = "#123abc" 141 | ``` 142 | 143 | This will, by default, turn on long mode, turn off icons, and set the color of directories to the specified hex code. However, variables can still be set and unset when in cscroll itself. To turn off long listing mode once again, for example, one could run this command in cscroll: `:unset long`. 144 | 145 | Variables specified in the configuration file will also be overwritten by command line arguments to cscroll. If the configuration specifies to show icons, the `-ni` flag will turn them off regardless. Configuration file values are of the least significance in regards to all other ways to set variables, although they will last indefinitely and will work for each run. 146 | 147 | #### Variables 148 | 149 | Variables allow setting defaults or changing settings while in cscroll itself as opposed to having to stop and restart it with different flags. Variables are set with `var = value` 150 | 151 | ##### Boolean Variables 152 | 153 | Boolean variables hold the value `true` or `false`. The following are boolean variables: 154 | 155 | * `color`: Turn on or off colors. 156 | * `dots`: Enable or disable the listing of dot files 157 | * `icons`: (If compiled with icons) If true, show [icons](#icons). Otherwise don't. 158 | * `long`: Turn on or off long mode 159 | 160 | ##### String Variables 161 | 162 | String variables can hold and value encased in two double-quotes. Single quotes will not create a valid string. For example, `variable = "my string"`. The following are string variables: 163 | 164 | * `opener`: Change the default file opener from `open` on Mac `xdg-open` on other systems to the specified value. 165 | 166 | The following string variables control the colors for each type of file specified by their names. Their values must be specified as six digit hexadecimal numbers with or without a leading hash (#). An example could be `dir_color = "#123abc"`. 167 | 168 | * `dir_color`: Directories 169 | * `reg_color`: Regular files (i.e. any file which is not any other color) 170 | * `fifo_color`: FIFO/Named pipes 171 | * `link_color`: Symbolic links 172 | * `block_color`: Block files (typically devices found in `/dev`) 173 | * `char_color`: Character devices 174 | * `sock_color`: Unix sockets 175 | * `unknown_color`: A file that cannot be accessed to determine type 176 | * `exec_color`: Any executable file 177 | * `media_color`: Any media file (pictures, audio files, videos) 178 | * `archive_color`: Any file archives (tarballs, zips, etc.) 179 | 180 | #### Icons 181 | 182 | [Nerd icons](https://github.com/ryanoasis/nerd-fonts) are special icons built into certain fonts. In cscroll, they are used to show a known or predicted file type. The icons will be on the left of any given file. However, the use of these icons requires a patched font or font with icons built in. Without such a font, the icons may show as a little white box outline or the escape code itself may be shown (something similar to `~]~J`). Installation instructions may be found in the linked site. 183 | 184 | If you wish to use cscroll without any icons, they can be turned off at compile time with the `ICONS` variable, in the configuration file and at runtime with the `icons` variable, and with the command line option `-ni`. 185 | 186 | ## Compilation 187 | 188 | To run, cscroll requires libncurses and libterminfo. Compilation, however, also requires pkg-config. 189 | 190 | On Debian and Ubuntu based system, libncurses may be titled `libncurses-dev` or simply `libncurses` and can be installed with `sudo apt install libncurses{,-dev}`. 191 | 192 | Any compilation configuration can be done in the `config.mk` file. 193 | 194 | To compile, simply run `make`. Nerd icons are enabled by default. To turn them off, compile with `ICONS=0`. 195 | 196 | The program will then be accessible by running `./cscroll`. 197 | 198 | ## Installation 199 | 200 | Run `make install` to install the binary to the directory pointed at by the set PREFIX and DESTDIR. 201 | 202 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "commands.h" 11 | #include "info.h" 12 | #include "main.h" 13 | #include "opts.h" 14 | #include "dir.h" 15 | #include "var.h" 16 | #include "io.h" 17 | 18 | 19 | size_t first_f = 0, last_f = 0, cursor = 0; 20 | 21 | 22 | int main(int argc, char ** argv) { 23 | var_init(); 24 | 25 | // config file needs home directory 26 | get_home(); 27 | 28 | if (!cwd) { 29 | char p[2048]; 30 | getcwd(p, sizeof(p)); 31 | cwd = malloc(strlen(p) + 2); 32 | strcpy(cwd, p); 33 | } 34 | 35 | if (!check_config()) create_config(); 36 | else read_config(); 37 | terminate_opts(); 38 | generate_colors(); 39 | 40 | if (argc > 1) { 41 | bool parse_opts = true; 42 | bool negate_next = false; 43 | 44 | for (int i = 1; i < argc; i++) { 45 | if (parse_opts && argv[i][0] == '-') { 46 | if (argv[i][0] != '\0' && argv[i][1] == '-') { 47 | if (argv[i][2] == '\0') parse_opts = false; 48 | else if (!strcmp(argv[i] + 2, "help")) 49 | help(); 50 | else if (!strcmp(argv[i] + 2, "oneshot")) 51 | oneshot = true; 52 | else { 53 | fprintf(stderr, "Unknown option: %s\n", argv[i]); 54 | exit(1); 55 | } 56 | } else { 57 | for (char * c = &argv[i][1]; *c; c++) { 58 | switch (*c) { 59 | case 'n': 60 | negate_next = true; 61 | break; 62 | case 'p': 63 | print_path = !negate_next; 64 | break; 65 | case 'c': 66 | color = !negate_next; 67 | break; 68 | #if ICONS 69 | case 'i': 70 | show_icons = !negate_next; 71 | break; 72 | #endif 73 | case 'l': 74 | p_long = !negate_next; 75 | break; 76 | case 'A': 77 | show_dot_files = !negate_next; 78 | break; 79 | case 'a': 80 | show_dot_files = !negate_next; 81 | show_dot_dirs = !negate_next; 82 | break; 83 | case 'h': break; 84 | default: 85 | fprintf(stderr, "Unknown option: -%c\n", *c); 86 | exit(1); 87 | break; 88 | } 89 | 90 | if (*c != 'n') negate_next = false; 91 | } 92 | } 93 | } else { 94 | cwd = realpath(argv[i], NULL); 95 | if (!cwd) cwd = strdup(argv[i]); 96 | } 97 | } 98 | } 99 | 100 | if (show_dot_dirs && !oneshot) { 101 | fputs("-a is only available in oneshot mode\n", stderr); 102 | exit(1); 103 | } 104 | 105 | bool cwd_is_dir = check_dpath(cwd); 106 | 107 | if (oneshot) { 108 | i_putc = putchar; 109 | i_puts = putsnonl; 110 | i_printf = printf; 111 | 112 | if (!cwd_is_dir) cwd_is_file = true; 113 | else list_dir(cwd); 114 | 115 | print_oneshot(); 116 | 117 | free_dir_entries(); 118 | free(dir_entries); 119 | terminate_var(); 120 | free(cwd); 121 | exit(0); 122 | } else if (!cwd_is_dir) { 123 | fputs("Invalid directory specified\n", stderr); 124 | exit(1); 125 | } else { 126 | chdir(cwd); 127 | } 128 | 129 | i_putc = addch_signed; 130 | i_puts = addstr; 131 | i_printf = printw; 132 | 133 | if (!strncmp(cwd, homedir, homedir_len)) in_home_subdir = true; 134 | 135 | curses_init(); 136 | info_init(); 137 | 138 | signal(SIGCONT, sig_handler); 139 | 140 | list_dir(cwd); 141 | 142 | cursor = 1; 143 | 144 | first_f = cursor - 1; 145 | last_f = LAST_F; 146 | 147 | while (true) { 148 | erase(); 149 | 150 | // scroll down 151 | if (cursor + 1 >= last_f && last_f < n_dir_entries) { 152 | first_f++; 153 | last_f++; 154 | // scroll back up 155 | } else if (cursor - 2 <= first_f && first_f > 0) { 156 | first_f--; 157 | last_f--; 158 | } 159 | 160 | // computer however many characters of cwd will be printed 161 | int cwd_len = 0; 162 | if (in_home_subdir) cwd_len = strlen(cwd) - homedir_len + 1; 163 | else cwd_len = strlen(cwd); 164 | 165 | // print cwd at top 166 | if (cwd_len >= COLS) { 167 | char * p = cwd + (cwd_len - COLS); 168 | if (in_home_subdir) { 169 | addch('~'); 170 | p += homedir_len; 171 | } 172 | 173 | // there is not enough room for an elipsis, 174 | // just print whatever is left 175 | if (p + 5 > cwd + strlen(cwd)) { 176 | //addstr(p); 177 | } else { 178 | p += 5; // strlen("/...") 179 | addch('/'); 180 | attron(A_DIM); 181 | addstr("..."); 182 | attroff(A_DIM); 183 | addstr(p); 184 | } 185 | // write the full path regularly 186 | } else { 187 | char * p = cwd; 188 | if (in_home_subdir) { 189 | addch('~'); 190 | p += homedir_len; 191 | } 192 | 193 | if (!*p) addch('/'); 194 | else addstr(p); 195 | } 196 | 197 | addch('\n'); 198 | bool printed_info = false; 199 | if (permission_denied) { 200 | printed_info = true; 201 | attron(COLOR_PAIR(RED)); 202 | addstr("Permission Denied"); 203 | attroff(COLOR_PAIR(RED)); 204 | } 205 | if (cutting) { 206 | printed_info = true; 207 | if (permission_denied) padstr(2); 208 | addstr("Cut"); 209 | } 210 | if (printed_info) addch('\n'); 211 | addch('\n'); 212 | 213 | int fstart_y, _fstart_x; 214 | getyx(stdscr, fstart_y, _fstart_x); 215 | // print files 216 | for (size_t i = first_f; i < last_f; i++) { 217 | bool h = false; 218 | if (cursor - 1 == i) 219 | h = true; 220 | 221 | curses_write_file(dir_entries[i], h); 222 | } 223 | int fend_y, _fend_x; 224 | getyx(stdscr, fend_y, _fend_x); 225 | 226 | // print cursor / total entries 227 | if (n_dir_entries) 228 | printw("\n%lu/%lu\n", cursor, n_dir_entries); 229 | 230 | refresh(); 231 | refresh_info(); 232 | 233 | int c = getch(); 234 | switch (c) { 235 | case UP_KEYS: 236 | if (cursor > 1) cursor--; 237 | break; 238 | case DOWN_KEYS: 239 | if (cursor < n_dir_entries) cursor++; 240 | break; 241 | case LEFT_KEYS: 242 | // can't cd back when in / 243 | if (cwd[0] == '/' && cwd[1] == '\0') break; 244 | 245 | char * cur_dir = strdup(strrchr(cwd, '/') + 1); 246 | 247 | cd_back(); 248 | free_dir_entries(); 249 | list_dir(cwd); 250 | 251 | // set cursor to old dir 252 | long lc = lit_search(0, cur_dir); 253 | if (lc >= 0) { 254 | cursor = lc + 1; 255 | resize_fbufcur(lc); 256 | } else { 257 | cursor = 1; 258 | first_f = 0; 259 | last_f = LAST_F; 260 | } 261 | 262 | free(cur_dir); 263 | break; 264 | case RIGHT_KEYS: 265 | case '\n': 266 | if (!n_dir_entries) break; 267 | open_cur_file(); 268 | break; 269 | case KEY_MOUSE:; 270 | MEVENT mouse_event; 271 | bool ok = false; 272 | unsigned norm_row = 0; // normalized row 273 | if (getmouse(&mouse_event) == OK) { 274 | ok = true; 275 | int mrow = mouse_event.y; // row @ mouse click; start @ 1 276 | if (mrow < fstart_y || mrow >= fend_y) break; 277 | 278 | norm_row = first_f + (mrow - fstart_y) + 1; 279 | } 280 | 281 | if (ok) { 282 | if (norm_row == cursor) { 283 | open_cur_file(); 284 | } else { 285 | cursor = norm_row; 286 | } 287 | } 288 | 289 | break; 290 | case KEY_HOME: 291 | case 'g': 292 | cursor = 1; 293 | first_f = 0; 294 | last_f = LAST_F; 295 | break; 296 | case KEY_END: 297 | case 'G': 298 | cursor = n_dir_entries; 299 | last_f = n_dir_entries - 1; 300 | first_f = n_dir_entries > (unsigned)LINES - 6? n_dir_entries - LINES + 5 : -1; 301 | break; 302 | case '.': 303 | show_dot_files = !show_dot_files; 304 | free_dir_entries(); 305 | list_dir(cwd); 306 | cursor = 1; 307 | first_f = 0; 308 | last_f = LAST_F; 309 | break; 310 | case 'd': 311 | if (n_marked_files) remove_marked(); 312 | else { 313 | char * rp = "Remove the file '%s'?"; 314 | int plen = snprintf(NULL, 0, rp, dir_entries[cursor - 1]->name); 315 | char * p = malloc(plen + 1); 316 | snprintf(p, plen + 1, rp, dir_entries[cursor - 1]->name); 317 | 318 | char * r = prompt(p, (char*[]){"No", "Yes", NULL}); 319 | free(p); 320 | 321 | if (r && !strcmp(r, "Yes")) remove_file(dir_entries[cursor - 1]); 322 | } 323 | 324 | free_dir_entries(); 325 | list_dir(cwd); 326 | 327 | if (cursor > n_dir_entries) cursor = n_dir_entries; 328 | resize_fbufcur(cursor); 329 | break; 330 | case 'm': 331 | // cannot mark files outside of start dir 332 | // while cutting 333 | if (cutting && strcmp(cwd, cut_start_dir)) break; 334 | 335 | if (!dir_entries[cursor - 1]->marked) { 336 | dir_entries[cursor - 1]->marked = true; 337 | n_marked_files++; 338 | } else { 339 | dir_entries[cursor - 1]->marked = false; 340 | if (n_marked_files) n_marked_files--; 341 | } 342 | break; 343 | case 'r': 344 | if (n_marked_files) 345 | break; 346 | char * nn = curses_getline(NULL); // new name 347 | if (!nn) break; 348 | // old path 349 | char * op = malloc(strlen(cwd) + strlen(dir_entries[cursor - 1]->name) + 2); 350 | // new path 351 | char * np = malloc(strlen(cwd) + strlen(nn) + 2); 352 | sprintf(op, "%s/%s", cwd, dir_entries[cursor - 1]->name); 353 | sprintf(np, "%s/%s", cwd, nn); 354 | rename(op, np); 355 | 356 | free(op); 357 | free(np); 358 | free(nn); 359 | 360 | free_dir_entries(); 361 | list_dir(cwd); 362 | 363 | if (cursor > n_dir_entries) cursor--; 364 | else if (cursor < 1) cursor++; 365 | 366 | if (last_f > n_dir_entries) last_f--; 367 | 368 | break; 369 | case 'c': 370 | // stop cutting if pressed twice 371 | if (cutting) { 372 | free_cuts(); 373 | break; 374 | } 375 | // copy cwd to start directory for cuts 376 | if (!n_marked_files) { 377 | char ** args = malloc(sizeof(char*) * 2); 378 | args[0] = malloc(strlen(dir_entries[cursor - 1]->name) + 1); 379 | strcpy(args[0], dir_entries[cursor - 1]->name); 380 | args[1] = NULL; 381 | create_cuts(cwd, args); 382 | free(args[0]); 383 | free(args); 384 | } else 385 | create_cuts(cwd, NULL); 386 | break; 387 | case 'p': 388 | if (!cutting) break; 389 | // only paste to different directories 390 | if (!strcmp(cwd, cut_start_dir)) { 391 | free_cuts(); 392 | break; 393 | } 394 | 395 | if (n_marked_files) { 396 | char * args[] = {"No", "Yes", NULL}; 397 | char * p = malloc(30); 398 | sprintf(p, "Paste all files (%lu)", n_marked_files); 399 | if (strcmp(prompt(p, args), "Yes")) { 400 | free(p); 401 | break; 402 | } 403 | } 404 | 405 | paste_cuts(cwd); 406 | free_cuts(); 407 | 408 | free_dir_entries(); 409 | list_dir(cwd); 410 | 411 | first_f = 0; 412 | last_f = LAST_F; 413 | cursor = 1; 414 | break; 415 | case ':':; 416 | char * inp = curses_getline(":"); 417 | if (!inp) break; 418 | 419 | char * sp = strchr(inp, ' '); 420 | if (sp) { 421 | *sp = 0; 422 | char * cmd = inp; 423 | char * args = ++sp; 424 | if (!strcmp(cmd, "set")) set(args); 425 | else if (!strcmp(cmd, "var")) switch (parse_var(args)) { 426 | case VAR_STAT_NOEQ: 427 | display_info(INFO_WARN, 428 | "Missing '=' in variable definition"); 429 | break; 430 | case VAR_STAT_NOTYPE: 431 | display_info(INFO_WARN, 432 | "Missing or unknown type"); 433 | break; 434 | default: 435 | case VAR_STAT_OK: 436 | break; 437 | } else if (!strcmp(cmd, "unset")) unset(args); 438 | } else if (!strcmp(inp, "ma") && !cutting) 439 | mark_all(); 440 | else if (!strcmp(inp, "mu")) { 441 | if (cutting) free_cuts(); 442 | unmark_all(); 443 | } else if (!strcmp(inp, "ca") && !cutting) { 444 | mark_all(); 445 | create_cuts(cwd, NULL); 446 | } else if (!strcmp(inp, "i")) { 447 | page_info(); 448 | } 449 | free(inp); 450 | break; 451 | case '/':; 452 | char * search_str = curses_getline("/"); 453 | if (!search_str) break; 454 | 455 | long c = search_file(cursor, search_str); 456 | free(search_str); 457 | 458 | if (c == -1) break; 459 | cursor = c + 1; 460 | 461 | resize_fbufcur(c); 462 | break; 463 | case '!':; 464 | char * cmd = curses_getline("!"); 465 | if (!cmd) break; 466 | // file name & length to sub 467 | char * f = dir_entries[cursor - 1]->name; 468 | size_t flen = strlen(f); 469 | 470 | char * cmdp = cmd; // tmp ptr to cmd 471 | char * f_sep; // where %f is found 472 | size_t moves = 0; // no. of times %f is found 473 | // replace '%f' with file name cursor is on 474 | while ((f_sep = strstr(cmdp, "%f"))) { 475 | // escape '%f' if '%%f' is found 476 | if (f_sep != cmd && f_sep[-1] == '%') { 477 | size_t seplen = strlen(f_sep); 478 | memmove(f_sep - 1, f_sep, seplen); 479 | f_sep[seplen - 1] = 0; 480 | cmdp = f_sep + 2; 481 | continue; 482 | } 483 | ++moves; 484 | // store position of f_sep to be restored 485 | // after reallocating cmd 486 | ptrdiff_t t = f_sep - cmd; 487 | cmd = realloc(cmd, strlen(cmd) + flen * moves + 1); 488 | f_sep = cmd + t; 489 | cmdp = f_sep + 2; 490 | // make room for file name 491 | memmove(f_sep + flen, f_sep + 2, strlen(f_sep) - 1); 492 | // replace '%f' with file name 493 | for (size_t i = 0; i < flen; i++) 494 | *f_sep++ = f[i]; 495 | } 496 | 497 | run_cmd(cmd); 498 | free(cmd); 499 | 500 | free_dir_entries(); 501 | list_dir(cwd); 502 | 503 | resize_fbuf(); 504 | 505 | break; 506 | case KEY_RESIZE: 507 | 508 | resize_fbuf(); 509 | 510 | erase(); 511 | refresh(); 512 | 513 | break; 514 | case CTRL_Z: 515 | terminate_curses(); 516 | // send self SIGSTOP -- restore shell feature 517 | kill(getpid(), SIGSTOP); 518 | break; 519 | case CTRL_C: 520 | case 'q': 521 | goto done; 522 | default: 523 | break; 524 | } 525 | } 526 | 527 | done: 528 | free_dir_entries(); 529 | free(dir_entries); 530 | terminate_var(); 531 | terminate_curses(); 532 | 533 | if (print_path) puts(cwd); 534 | 535 | free(cwd); 536 | free(homedir); 537 | 538 | return 0; 539 | } 540 | 541 | 542 | void help(void) { 543 | puts( 544 | "cscroll\n" 545 | "A small and efficient file manager\n" 546 | "\n" 547 | "Usage:\n" 548 | " cscroll [OPTION]... [DIR]\n" 549 | "\n" 550 | "Options:\n" 551 | " -A Show dotfiles except . and ..\n" 552 | " -a Show all dotfiles, including . and ..\n" 553 | " Only available in oneshot mode\n" 554 | " -c Use colors\n" 555 | " -h No-op: for compatibility purposes\n" 556 | #if ICONS 557 | " -i Use icons\n" 558 | #endif 559 | " -l Print files in long mode\n" 560 | " -n Negate the next flag\n" 561 | " -p Print the path cscroll is in when it exits\n" 562 | " --help Show this screen and exit\n" 563 | " --oneshot Print and exit as if cscroll is ls\n" 564 | " -- Stop parsing '-' flags\n" 565 | "\n" 566 | "See https://github.com/Raniconduh/cscroll for documentation\n" 567 | ); 568 | exit(0); 569 | } 570 | 571 | 572 | void sig_handler(int signo) { 573 | switch (signo) { 574 | case SIGCONT: 575 | curses_init(); 576 | break; 577 | } 578 | } 579 | -------------------------------------------------------------------------------- /src/io.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #if ICONS 18 | #include "type.h" 19 | #endif 20 | #include "info.h" 21 | #include "main.h" 22 | #include "opts.h" 23 | #include "dir.h" 24 | #include "io.h" 25 | 26 | 27 | int (*i_putc)(int) = NULL; 28 | int (*i_puts)(const char *) = NULL; 29 | int (*i_printf)(const char *, ...) = NULL; 30 | 31 | bool print_path = false; 32 | int stdout_back = 0; 33 | size_t n_marked_files = false; 34 | 35 | 36 | static int default_colors[] = { 37 | [COLOR_DIR] = COLOR_BLUE, 38 | [COLOR_LINK] = COLOR_CYAN, 39 | [COLOR_EXEC] = COLOR_GREEN, 40 | [COLOR_SOCK] = COLOR_MAGENTA, 41 | [COLOR_FIFO] = COLOR_YELLOW, 42 | [COLOR_UNKNOWN] = COLOR_RED, 43 | [COLOR_FILE] = COLOR_WHITE, 44 | [COLOR_BLOCK] = COLOR_YELLOW, 45 | [COLOR_CHAR] = COLOR_YELLOW, 46 | [COLOR_MEDIA] = COLOR_MAGENTA, 47 | [COLOR_ARCHIVE] = COLOR_RED, 48 | }; 49 | 50 | 51 | static char * ansi_colors[] = { 52 | [RED] = ANSI_RED, 53 | [YELLOW] = ANSI_YELLOW, 54 | [MAGENTA] = ANSI_MAGENTA, 55 | [WHITE] = ANSI_WHITE, 56 | 57 | [COLOR_DIR] = ANSI_BLUE, 58 | [COLOR_LINK] = ANSI_CYAN, 59 | [COLOR_EXEC] = ANSI_GREEN, 60 | [COLOR_SOCK] = ANSI_YELLOW, 61 | [COLOR_FIFO] = ANSI_YELLOW, 62 | [COLOR_UNKNOWN] = ANSI_RED, 63 | [COLOR_FILE] = ANSI_WHITE, 64 | [COLOR_BLOCK] = ANSI_YELLOW, 65 | [COLOR_CHAR] = ANSI_YELLOW, 66 | [COLOR_MEDIA] = ANSI_MAGENTA, 67 | [COLOR_ARCHIVE] = ANSI_RED, 68 | }; 69 | 70 | 71 | static char * size_strings[] = { 72 | "B", "KB", "MB", "GB", "TB", "PB" 73 | }; 74 | 75 | 76 | int putsnonl(const char * s) { 77 | return fputs(s, stdout); 78 | } 79 | 80 | 81 | int addch_signed(int c) { 82 | return addch((unsigned)c); 83 | } 84 | 85 | void curses_init(void) { 86 | if (print_path) { 87 | stdout_back = dup(STDOUT_FILENO); 88 | dup2(open("/dev/tty", O_WRONLY), STDOUT_FILENO); 89 | } 90 | 91 | setlocale(LC_CTYPE, ""); 92 | 93 | initscr(); 94 | keypad(stdscr, true); 95 | mouseinterval(0); 96 | curs_set(0); 97 | noecho(); 98 | raw(); 99 | 100 | mousemask(BUTTON1_PRESSED, NULL); 101 | 102 | use_default_colors(); 103 | start_color(); 104 | set_color(); 105 | } 106 | 107 | 108 | void terminate_curses(void) { 109 | keypad(stdscr, false); 110 | curs_set(1); 111 | echo(); 112 | noraw(); 113 | endwin(); 114 | 115 | if (print_path) { 116 | dup2(stdout_back, STDOUT_FILENO); 117 | } 118 | } 119 | 120 | 121 | void set_color(void) { 122 | generate_colors(); 123 | bool cc = can_change_color(); 124 | 125 | if (color) { 126 | for (int i = CUSTOM_DIR; i <= CUSTOM_ARCHIVE; i++) { 127 | int def = i - CUSTOM_DIR + 1; // default color / index 128 | if (custom_colors[def] == COLOR_DEFAULT || !cc) { 129 | init_pair(def, default_colors[def], -1); 130 | } else { 131 | init_color(i, GET_RGB(custom_colors[def])); 132 | init_pair(def, i, -1); 133 | } 134 | } 135 | 136 | init_pair(RED, COLOR_RED, -1); 137 | init_pair(WHITE, COLOR_WHITE, -1); 138 | init_pair(YELLOW, COLOR_YELLOW, -1); 139 | init_pair(MAGENTA, COLOR_MAGENTA, -1); 140 | } else { 141 | // COLOR_ARCHIVE is highest enum value 142 | for (int i = 1; i <= COLOR_ARCHIVE; i++) { 143 | init_pair(i, -1, -1); 144 | } 145 | 146 | init_pair(RED, -1, -1); 147 | init_pair(WHITE, -1, -1); 148 | init_pair(YELLOW, -1, -1); 149 | init_pair(MAGENTA, -1, -1); 150 | } 151 | } 152 | 153 | 154 | void curses_write_file(struct dir_entry_t * dir_entry, bool highlight) { 155 | if (dir_entry->marked) addstr("- "); 156 | if (p_long) print_long_info(dir_entry); 157 | print_file_name(dir_entry, highlight); 158 | addch('\n'); 159 | } 160 | 161 | 162 | void print_mode(struct dir_entry_t * f) { 163 | enum colors m_colors[127] = { 164 | ['s'] = YELLOW, ['r'] = RED, ['w'] = MAGENTA, 165 | ['x'] = COLOR_EXEC, ['S'] = YELLOW, ['t'] = RED, 166 | ['T'] = RED, ['-'] = WHITE, ['?'] = WHITE, 167 | 168 | ['.'] = WHITE, 169 | ['d'] = COLOR_DIR, ['b'] = COLOR_BLOCK, ['c'] = COLOR_CHAR, 170 | ['l'] = COLOR_LINK, ['|'] = COLOR_FIFO, ['='] = COLOR_SOCK, 171 | }; 172 | 173 | char * mode = mode_to_s(f); 174 | for (char * c = mode; *c; c++) { 175 | if (oneshot) { 176 | if (color) { 177 | char * color = ansi_colors[m_colors[(int)*c]]; 178 | printf("%s%c" ANSI_RESET, color, *c); 179 | } else { 180 | fputc(*c, stdout); 181 | } 182 | } else { 183 | bool dim = false; 184 | 185 | int cp = m_colors[(int)*c]; 186 | if (!cp) cp = WHITE; 187 | if (cp == WHITE) dim = true; 188 | cp = COLOR_PAIR(cp); 189 | if (dim) cp |= A_DIM; 190 | attron(cp); 191 | addch(*c); 192 | attroff(cp); 193 | } 194 | } 195 | free(mode); 196 | } 197 | 198 | 199 | void padstr(size_t n) { 200 | for (size_t i = 0; i < n; i++) i_putc(' '); 201 | } 202 | 203 | 204 | char * prompt(char * t, char ** args) { 205 | size_t tlen = strlen(t); 206 | 207 | int sub_cols; 208 | // wider than 75% of the screen; need to break it down 209 | if (tlen > (unsigned)(COLS * 3) / 4) sub_cols = COLS * 3 / 4 - 1; 210 | // default is 4 wider than printed text (padding) 211 | else sub_cols = tlen + 4; 212 | 213 | /********** 214 | * calculate the number of lines the text will need 215 | * 2 lines padding top/bottom 216 | * lines_of_text + 1 for prompt 217 | * extra line between text and options 218 | * 2 lines for options 219 | * 6 total 220 | **********/ 221 | size_t n_text_lines = tlen / sub_cols + 1; // +1 for int division 222 | int sub_rows = n_text_lines + 6; 223 | 224 | // newwin(rows, cols, y, x) 225 | WINDOW * w = newwin(sub_rows, sub_cols, LINES / 2 - sub_rows / 2, COLS / 2 - sub_cols / 2); 226 | werase(w); 227 | 228 | box(w, 0, 0); 229 | 230 | // print text string 231 | if (tlen < (unsigned)sub_cols - 2) 232 | mvwprintw(w, 2, sub_cols / 2 - tlen / 2, "%s", t); 233 | else { 234 | int row = 1; 235 | int col = 1; 236 | for (size_t i = 0; i < tlen; i++) { 237 | col++; 238 | if (i % (sub_cols - 4) == 0) { 239 | row++; 240 | col = 2; 241 | } 242 | mvwaddch(w, row, col, t[i]); 243 | } 244 | } 245 | 246 | if (!args) { 247 | wrefresh(w); 248 | napms(3500); 249 | delwin(w); 250 | return NULL; 251 | } 252 | 253 | size_t argcount = 0; 254 | size_t argstrlen = 0; 255 | for (char ** p = args; *p; p++) { 256 | argcount++; 257 | argstrlen += strlen(*p); 258 | } 259 | 260 | int cursor = 1; 261 | 262 | // pick option 263 | while (true) { 264 | int col = 2; 265 | for (size_t i = 0; i < argcount; i++) { 266 | if ((unsigned)cursor - 1 == i) 267 | wattron(w, COLOR_PAIR(WHITE) | A_REVERSE); 268 | mvwprintw(w, sub_rows - 2, col + (sub_cols / 2 - argstrlen), "%s", args[i]); 269 | if ((unsigned)cursor - 1 == i) 270 | wattroff(w, COLOR_PAIR(WHITE) | A_REVERSE); 271 | col += strlen(args[i]) + 2; 272 | } 273 | 274 | wrefresh(w); 275 | 276 | int c = getch(); 277 | switch (c) { 278 | case UP_KEYS: 279 | case LEFT_KEYS: 280 | if (cursor > 1) cursor--; 281 | break; 282 | case DOWN_KEYS: 283 | case RIGHT_KEYS: 284 | if ((unsigned)cursor < argcount) cursor++; 285 | break; 286 | case '\n': 287 | case ' ': 288 | delwin(w); 289 | return args[cursor - 1]; 290 | case 'q': 291 | case KEY_ESC: 292 | goto done; 293 | default: 294 | break; 295 | } 296 | } 297 | done:; 298 | delwin(w); 299 | return NULL; 300 | } 301 | 302 | 303 | char * curses_getline(char * p) { 304 | curs_set(1); 305 | 306 | size_t plen = 0; 307 | if (p) { 308 | addstr(p); 309 | plen = strlen(p); 310 | refresh(); 311 | } 312 | 313 | char * inp = malloc(128); 314 | size_t l = 0; 315 | int c; 316 | while ((c = getch()) != '\n') { 317 | if ((l + 1) % 127 == 0) { 318 | // add 1 byte to prevent buffer overrun on deletion 319 | inp = realloc(inp, l + 129); 320 | } 321 | 322 | if (c == KEY_DEL || c == KEY_BACKSPACE) { 323 | if (l > 0) l--; 324 | } else if (isprint(c)) { 325 | // do not bother with non-printable characters 326 | // line editing may be added later 327 | inp[l++] = c; 328 | } else continue; 329 | 330 | int y, x; 331 | getyx(stdscr, y, x); 332 | 333 | // it is time to scroll 334 | if (l >= COLS - plen - 1) { 335 | move(y, plen); 336 | for (size_t i = l - COLS + plen + 1; i < l; i++) { 337 | addch(inp[i]); 338 | } 339 | } else if ((unsigned)x != plen && (c == KEY_DEL || c == KEY_BACKSPACE)) { 340 | mvaddch(y, x - 1, ' '); 341 | move(y, x - 1); 342 | } else if (c != KEY_DEL && c != KEY_BACKSPACE) { 343 | addch(c); 344 | } 345 | 346 | refresh(); 347 | } 348 | 349 | if (l == 0) { 350 | free(inp); 351 | inp = NULL; 352 | } else inp[l] = '\0'; 353 | curs_set(0); 354 | 355 | return inp; 356 | } 357 | 358 | 359 | void mark_all(void) { 360 | for (size_t i = 0; i < n_dir_entries; i++) 361 | dir_entries[i]->marked = true; 362 | n_marked_files = n_dir_entries; 363 | } 364 | 365 | 366 | void unmark_all(void) { 367 | for (size_t i = 0; i < n_dir_entries; i++) 368 | dir_entries[i]->marked = false; 369 | n_marked_files = 0; 370 | } 371 | 372 | 373 | void resize_fbuf(void) { 374 | if (n_dir_entries <= (unsigned)LINES - 6) { 375 | first_f = 0; 376 | last_f = n_dir_entries; 377 | } else if (LINES <= 6) { 378 | if (first_f + 1 < n_dir_entries) last_f = first_f + 1; 379 | else last_f = first_f; 380 | } else if ((unsigned)LINES - 6 > n_dir_entries) { 381 | last_f = n_dir_entries; 382 | } else { 383 | last_f = first_f + LINES - 6; 384 | } 385 | 386 | if (cursor > last_f + 1) cursor = last_f + 1; 387 | 388 | } 389 | 390 | 391 | void resize_fbufcur(long c) { 392 | // all files can fit on screen 393 | if (n_dir_entries <= (unsigned)LINES - 6) { 394 | first_f = 0; 395 | last_f = n_dir_entries; 396 | // somewhere in middle but no need to scroll 397 | } else if (cursor > first_f && cursor <= last_f) { 398 | ; // no-op 399 | // somewhere in the middle 400 | } else if (cursor + LINES - 7 <= n_dir_entries) { 401 | first_f = c; 402 | last_f = first_f + LINES - 6; 403 | // at the end 404 | } else if (n_dir_entries > (unsigned)LINES - 6) { 405 | last_f = n_dir_entries; 406 | first_f = last_f - LINES + 6; 407 | } 408 | } 409 | 410 | 411 | int get_fwidth(struct dir_entry_t * de) { 412 | int w = 0; 413 | w += strlen(de->name); 414 | #if ICONS 415 | if (show_icons) w += 2; 416 | #endif 417 | 418 | // file ident 419 | if (get_file_ident(de) != NO_IDENT) w += 1; 420 | 421 | // final ident will be: @ => / 422 | if (de->under_link == FILE_DIR) w += 5; 423 | 424 | return w; 425 | } 426 | 427 | 428 | void print_oneshot(void) { 429 | if (cwd_is_file) { 430 | char * end_sep = strrchr(cwd, '/'); 431 | char * f_name; 432 | char * wd; 433 | bool malloc_wd = false; 434 | 435 | if (end_sep) { 436 | *end_sep = '\0'; 437 | f_name = end_sep + 1; 438 | wd = cwd; 439 | } else { 440 | f_name = cwd; 441 | malloc_wd = true; 442 | wd = malloc(128); 443 | getcwd(wd, 128); 444 | } 445 | 446 | // need to manually check if it exists 447 | struct stat st_buf; 448 | if (stat(f_name, &st_buf) == -1) { 449 | fputs(strerror(errno), stderr); 450 | fputc('\n', stderr); 451 | free(cwd); 452 | exit(1); 453 | } 454 | 455 | struct dir_entry_t * de = gen_dir_entry(wd, f_name); 456 | if (malloc_wd) free(wd); 457 | 458 | n_dir_entries = 1; 459 | dir_entries = malloc(sizeof(struct dir_entry_t*)); 460 | dir_entries[0] = de; 461 | } 462 | 463 | if (p_long) { 464 | for (size_t i = 0; i < n_dir_entries; i++) { 465 | struct dir_entry_t * de = dir_entries[i]; 466 | 467 | print_long_info(de); 468 | print_file_name(de, false); 469 | putchar('\n'); 470 | } 471 | } else { // regular printing mode 472 | struct winsize w; 473 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); 474 | int t_width = w.ws_col; 475 | 476 | // calculate the maximum columns 477 | size_t longest_file = 0; 478 | for (size_t i = 0; i < n_dir_entries; i++) { 479 | size_t l = get_fwidth(dir_entries[i]); 480 | if (l > longest_file) longest_file = l; 481 | } 482 | int cols = t_width / (longest_file + 2); 483 | if (n_dir_entries < (unsigned)cols) cols = n_dir_entries; 484 | 485 | // calculate padding 486 | int col_widths[cols]; 487 | memset(col_widths, 0, sizeof(col_widths)); 488 | for (size_t i = 0; i < n_dir_entries; i++) { 489 | size_t l = get_fwidth(dir_entries[i]); 490 | if (l > (unsigned)col_widths[i % cols]) 491 | col_widths[i % cols] = l; 492 | } 493 | 494 | // print files 495 | for (size_t i = 0; i < n_dir_entries; i++) { 496 | print_file_name(dir_entries[i], false); 497 | if ((i + 1) % cols == 0 || i + 1 == n_dir_entries) putchar('\n'); 498 | else padstr(col_widths[i % cols] - get_fwidth(dir_entries[i]) + 2); 499 | } 500 | } 501 | } 502 | 503 | 504 | enum colors get_file_color(struct dir_entry_t * de) { 505 | int cp = -1; 506 | 507 | switch (de->file_type) { 508 | case FILE_DIR: 509 | cp = COLOR_DIR; break; 510 | case FILE_FIFO: 511 | cp = COLOR_FIFO; break; 512 | case FILE_BLK: 513 | cp = COLOR_BLOCK; break; 514 | case FILE_CHR: 515 | cp = COLOR_CHAR; break; 516 | case FILE_LINK: 517 | cp = COLOR_LINK; break; 518 | case FILE_SOCK: 519 | cp = COLOR_SOCK; break; 520 | case FILE_UNKNOWN: 521 | cp = COLOR_UNKNOWN; break; 522 | case FILE_REG: 523 | cp = COLOR_FILE; break; 524 | } 525 | 526 | switch (de->m_type) { 527 | case MIME_MEDIA: 528 | cp = COLOR_MEDIA; break; 529 | case MIME_ARCHIVE: 530 | cp = COLOR_ARCHIVE; break; 531 | case MIME_UNKNOWN: 532 | default: break; 533 | } 534 | 535 | if ((de->mode & POWNER(M_EXEC)) && 536 | de->file_type != FILE_LINK && 537 | de->file_type != FILE_DIR) { 538 | cp = COLOR_EXEC; 539 | } else if (cp == -1) { 540 | cp = COLOR_FILE; 541 | } 542 | 543 | return (enum colors)cp; 544 | } 545 | 546 | 547 | char get_file_ident(struct dir_entry_t * de) { 548 | char f_ident; 549 | 550 | switch (de->file_type) { 551 | case FILE_DIR: 552 | f_ident = '/'; 553 | break; 554 | case FILE_FIFO: 555 | f_ident = '|'; 556 | break; 557 | case FILE_BLK: 558 | case FILE_CHR: 559 | f_ident = '#'; 560 | break; 561 | case FILE_LINK: 562 | f_ident = '@'; 563 | break; 564 | case FILE_SOCK: 565 | f_ident = '='; 566 | break; 567 | case FILE_UNKNOWN: 568 | f_ident = '?'; 569 | break; 570 | default: 571 | f_ident = NO_IDENT; 572 | break; 573 | } 574 | 575 | if ((de->mode & POWNER(M_EXEC)) && 576 | de->file_type != FILE_LINK && 577 | de->file_type != FILE_DIR && 578 | f_ident == NO_IDENT) { 579 | f_ident = '*'; 580 | } 581 | 582 | return f_ident; 583 | } 584 | 585 | 586 | size_t get_ilen(long i, int base) { 587 | size_t l = 0; 588 | 589 | while (i > 0) { 590 | i /= base; 591 | l++; 592 | } 593 | 594 | return l; 595 | } 596 | 597 | 598 | char * get_oname(struct dir_entry_t * de) { 599 | if (de->file_type == FILE_UNKNOWN) return strdup("?"); 600 | 601 | uid_t uid = de->owner; 602 | char * buf = NULL; 603 | struct passwd * pw = getpwuid(uid); 604 | if (!pw) { 605 | buf = malloc(get_ilen(uid, 10) + 1); 606 | sprintf(buf, "%d", uid); 607 | } else { 608 | buf = malloc(strlen(pw->pw_name) + 1); 609 | strcpy(buf, pw->pw_name); 610 | } 611 | return buf; 612 | } 613 | 614 | 615 | char * get_gname(struct dir_entry_t * de) { 616 | if (de->file_type == FILE_UNKNOWN) return strdup("?"); 617 | 618 | gid_t gid = de->group; 619 | char * buf = NULL; 620 | struct group * gr = getgrgid(gid); 621 | if (!gr) { 622 | buf = malloc(get_ilen(gid, 10) + 1); 623 | sprintf(buf, "%d", gid); 624 | } else { 625 | buf = malloc(strlen(gr->gr_name) + 1); 626 | strcpy(buf, gr->gr_name); 627 | } 628 | return buf; 629 | } 630 | 631 | 632 | void print_file_name(struct dir_entry_t * de, bool highlight) { 633 | #if ICONS 634 | if (show_icons) { 635 | i_puts(get_icon(de)); 636 | i_putc(' '); 637 | } 638 | #endif 639 | 640 | char f_ident = get_file_ident(de); 641 | char * u_text = de->under_link == FILE_DIR ? "=> /" : NULL; 642 | int cp = get_file_color(de); 643 | 644 | if (oneshot) { 645 | char * fcolor = NULL; 646 | 647 | if (color && cp != COLOR_WHITE) fcolor = ansi_colors[cp]; 648 | 649 | if (fcolor) i_puts(fcolor); 650 | i_puts(de->name); 651 | if (fcolor) i_puts(ANSI_RESET); 652 | } else { 653 | cp = COLOR_PAIR((unsigned)cp); 654 | 655 | int y, x; 656 | (void)(y); 657 | getyx(stdscr, y, x); 658 | 659 | size_t ext_len = 0; 660 | if (f_ident != NO_IDENT) ext_len++; 661 | if (u_text) ext_len += strlen(u_text); 662 | 663 | if (highlight) cp |= A_REVERSE; 664 | 665 | attron(cp); 666 | // trim the file name so that it fits on the screen 667 | if (x + strlen(de->name) + ext_len >= (unsigned)COLS) { 668 | int padding = 4; // 3 for ..., 1 for space before NL 669 | padding += ext_len ? ext_len + 1 : 0; 670 | if (padding > COLS - x) padding = 4; 671 | 672 | addnstr(de->name, COLS - x - padding); 673 | attron(A_DIM); 674 | addstr("..."); 675 | attroff(A_DIM); 676 | } else { 677 | i_puts(de->name); 678 | } 679 | attroff(cp); 680 | } 681 | 682 | if (f_ident) i_putc(f_ident); 683 | if (u_text) { 684 | i_putc(' '); 685 | i_puts(u_text); 686 | } 687 | } 688 | 689 | 690 | void print_long_info(struct dir_entry_t * de) { 691 | char * owner = get_oname(de); 692 | char * group = get_gname(de); 693 | size_t n; 694 | 695 | print_mode(de); 696 | 697 | // write owner 698 | i_putc(' '); 699 | i_puts(owner); 700 | n = strlen(owner); 701 | if (n < dir_longest_owner) padstr(dir_longest_owner - n); 702 | free(owner); 703 | 704 | // write group 705 | i_putc(' '); 706 | i_puts(group); 707 | n = strlen(group); 708 | if (n < dir_longest_group) padstr(dir_longest_group - n); 709 | free(group); 710 | 711 | // write size and time 712 | char * size = size_strings[de->u_size]; 713 | char time[128]; 714 | strftime(time, sizeof(time), "%b %d %H:%M %Y", localtime(&de->mtime)); 715 | 716 | i_printf(" %4lu %-2s %s ", de->size, size, time); 717 | } 718 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | cscroll Copyright (C) 2021 Raniconduh 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------