1 /* See LICENSE file for copyright and license details. */
5 #elif defined(__APPLE__)
6 #define _DARWIN_C_SOURCE
7 #elif defined(__FreeBSD__)
8 #define __BSD_VISIBLE 1
10 #include <sys/types.h>
11 #include <sys/resource.h>
15 #if defined(__linux__)
16 #include <sys/inotify.h>
17 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
19 #include <sys/event.h>
46 #define MAX_STATUS 255
51 #define CURSOR(x) (x)->direntr[(x)->hdir - 1]
70 char dirn
[MAX_P
]; // dir name cwd
72 Entry
*direntr
; // dir entries
73 int dirx
; // pane cwd x pos
74 int dirc
; // dir entries sum
75 int hdir
; // highlighted dir
79 int parent_row
; // FIX
93 uint16_t key
; /* one of the TB_KEY_* constants */
94 uint32_t ch
; /* unicode character */
104 void (*func
)(const Arg
*);
108 /* function declarations */
109 static void print_tb(const char *, int, int, uint16_t, uint16_t);
110 static void printf_tb(int, int, Cpair
, const char *, ...);
111 static void print_status(Cpair
, const char *, ...);
112 static void print_xstatus(char, int);
113 static void print_error(char *);
114 static void print_prompt(char *);
115 static void print_info(Pane
*, char *);
116 static void print_row(Pane
*, size_t, Cpair
);
117 static void clear(int, int, int, uint16_t);
118 static void clear_status(void);
119 static void clear_pane(Pane
*);
120 static void add_hi(Pane
*, size_t);
121 static void rm_hi(Pane
*, size_t);
122 static int check_dir(char *);
123 static mode_t
chech_execf(mode_t
);
124 static int sort_name(const void *const, const void *const);
125 static void get_dirp(char *);
126 static char *get_ext(char *);
127 static int get_fdt(char *, time_t);
128 static char *get_fgrp(gid_t
);
129 static char *get_fperm(mode_t
);
130 static char *get_fsize(off_t
);
131 static char *get_fullpath(char *, char *);
132 static char *get_fusr(uid_t
);
133 static void get_dirsize(char *, off_t
*);
134 static void get_hicol(Cpair
*, mode_t
);
135 static void delent(const Arg
*arg
);
136 static void calcdir(const Arg
*arg
);
137 static void crnd(const Arg
*arg
);
138 static void crnf(const Arg
*arg
);
139 static void mv_ver(const Arg
*arg
);
140 static void mvbk(const Arg
*arg
);
141 static void mvbtm(const Arg
*arg
);
142 static void mvfwd(const Arg
*arg
);
143 static void mvmid(const Arg
*arg
);
144 static void mvtop(const Arg
*arg
);
145 static void bkmrk(const Arg
*arg
);
146 static int get_usrinput(char *, size_t, const char *, ...);
147 static int frules(char *);
148 static int spawn(const void *, size_t, const void *, size_t, char *);
149 static int opnf(char *);
150 static int fsev_init(void);
151 static int addwatch(Pane
*);
152 static int read_events(void);
153 static void rmwatch(Pane
*);
154 static void fsev_shdn(void);
155 static void toggle_df(const Arg
*arg
);
156 static void start_filter(const Arg
*arg
);
157 static void start_vmode(const Arg
*arg
);
158 static void exit_vmode(const Arg
*arg
);
159 static void selup(const Arg
*arg
);
160 static void seldwn(const Arg
*arg
);
161 static void selall(const Arg
*arg
);
162 static void selref(void);
163 static void selynk(const Arg
*arg
);
164 static void selcalc(void);
165 static void paste(const Arg
*arg
);
166 static void selmv(const Arg
*arg
);
167 static void seldel(const Arg
*arg
);
168 static void init_files(void);
169 static void free_files(void);
170 static void yank(const Arg
*arg
);
171 static void rname(const Arg
*arg
);
172 static void switch_pane(const Arg
*arg
);
173 static void quit(const Arg
*arg
);
174 static void grabkeys(struct tb_event
*, Key
*, size_t);
175 static void *read_th(void *arg
);
176 static void start_ev(void);
177 static void refresh_pane(Pane
*);
178 static void set_direntr(Pane
*, struct dirent
*, DIR *, char *);
179 static int listdir(Pane
*);
180 static void t_resize(void);
181 static void set_panes(void);
182 static void draw_frame(void);
183 static void start(void);
185 /* global variables */
186 static pthread_t fsev_thread
;
187 static Pane panes
[2];
190 static char *editor
[2];
191 static char fed
[] = "vi";
192 static int theight
, twidth
, scrheight
;
193 static int *sel_indexes
;
194 static size_t sel_len
= 0;
195 static char **sel_files
;
196 static int cont_vmode
= 0;
198 #if defined(_SYS_INOTIFY_H)
200 static int inotify_fd
;
201 #elif defined(_SYS_EVENT_H_)
204 struct kevent evlist
[2]; /* events we want to monitor */
205 struct kevent chlist
[2]; /* events that were triggered */
206 static struct timespec gtimeout
;
208 #if defined(__linux__) || defined(__FreeBSD__)
210 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
213 enum { Left
, Right
}; /* panes */
215 /* configuration, allows nested code to access above variables */
218 /* function implementations */
220 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
222 while (*str
!= '\0') {
224 str
+= tb_utf8_char_to_unicode(&uni
, str
);
225 tb_change_cell(x
, y
, uni
, fg
, bg
);
231 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
236 (void)vsnprintf(buf
, MAX_LINE
, fmt
, vl
);
238 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
242 print_status(Cpair col
, const char *fmt
, ...)
244 char buf
[MAX_STATUS
];
247 (void)vsnprintf(buf
, MAX_STATUS
, fmt
, vl
);
250 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
254 print_xstatus(char c
, int x
)
257 (void)tb_utf8_char_to_unicode(&uni
, &c
);
258 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
262 print_error(char *errmsg
)
264 print_status(cerr
, errmsg
);
268 print_prompt(char *prompt
)
270 print_status(cprompt
, prompt
);
274 print_info(Pane
*pane
, char *dirsize
)
276 char *sz
, *ur
, *gr
, *dt
, *prm
;
278 dt
= ecalloc(MAX_DTF
, sizeof(char));
280 prm
= get_fperm(CURSOR(pane
).mode
);
281 ur
= get_fusr(CURSOR(pane
).user
);
282 gr
= get_fgrp(CURSOR(pane
).group
);
284 if (get_fdt(dt
, CURSOR(pane
).dt
) < 0)
287 if (S_ISREG(CURSOR(pane
).mode
)) {
288 sz
= get_fsize(CURSOR(pane
).size
);
290 if (dirsize
== NULL
) {
291 sz
= ecalloc(1, sizeof(char));
298 print_status(cstatus
, "%02d/%02d %s %s:%s %s %s", pane
->hdir
,
299 pane
->dirc
, prm
, ur
, gr
, dt
, sz
);
309 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
314 char lnk_full
[MAX_P
];
317 width
= (twidth
/ 2) - 4;
318 result
= basename(pane
->direntr
[entpos
].name
);
320 y
= entpos
- pane
->firstrow
+ 1;
322 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
323 if (realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
325 lnk_full
, MAX_N
, "%s -> %s", result
, buf
);
330 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
334 clear(int sx
, int ex
, int y
, uint16_t bg
)
336 /* clear line from to */
337 /* x = line number vertical */
338 /* y = column number horizontal */
340 for (i
= sx
; i
< ex
; i
++) {
341 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
348 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
352 clear_pane(Pane
*pane
)
357 while (i
< scrheight
) {
358 clear(pane
->dirx
, pane
->ex
, y
, TB_DEFAULT
);
364 for (y
= pane
->dirx
; y
< pane
->ex
; ++y
) {
365 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
370 add_hi(Pane
*pane
, size_t entpos
)
373 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
374 col
.fg
|= TB_REVERSE
| TB_BOLD
;
375 col
.bg
|= TB_REVERSE
;
376 print_row(pane
, entpos
, col
);
380 rm_hi(Pane
*pane
, size_t entpos
)
383 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
384 print_row(pane
, entpos
, col
);
388 check_dir(char *path
)
394 if (errno
== ENOTDIR
) {
401 if (closedir(dir
) < 0)
408 chech_execf(mode_t mode
)
411 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
416 sort_name(const void *const A
, const void *const B
)
419 mode_t data1
= (*(Entry
*)A
).mode
;
420 mode_t data2
= (*(Entry
*)B
).mode
;
424 } else if (data1
== data2
) {
425 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
438 len
= strnlen(cdir
, MAX_P
);
442 for (i
= len
- 1; i
> 1; i
--) {
449 cdir
[len
- counter
- 1] = '\0';
457 size_t counter
, len
, i
;
461 len
= strnlen(str
, MAX_N
);
463 for (i
= len
- 1; i
> 0; i
--) {
471 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
472 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
478 get_fdt(char *result
, time_t status
)
481 localtime_r(&status
, <
);
482 return strftime(result
, MAX_DTF
, dtfmt
, <
);
486 get_fgrp(gid_t status
)
491 result
= ecalloc(MAX_GRPN
, sizeof(char));
492 gr
= getgrgid(status
);
494 (void)snprintf(result
, MAX_GRPN
, "%u", status
);
496 strncpy(result
, gr
->gr_name
, MAX_GRPN
);
498 result
[MAX_GRPN
- 1] = '\0';
503 get_fperm(mode_t mode
)
508 const char chars
[] = "rwxrwxrwx";
509 buf
= ecalloc(11, sizeof(char));
513 else if (S_ISREG(mode
))
515 else if (S_ISLNK(mode
))
517 else if (S_ISBLK(mode
))
519 else if (S_ISCHR(mode
))
521 else if (S_ISFIFO(mode
))
523 else if (S_ISSOCK(mode
))
528 for (i
= 1; i
< 10; i
++) {
529 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
537 get_fsize(off_t size
)
539 char *result
; /* need to be freed */
545 result_len
= 6; /* 9999X/0 */
546 result
= ecalloc(result_len
, sizeof(char));
548 while (size
>= 1000) {
573 if (snprintf(result
, result_len
, OFF_T
"%c", size
, unit
) < 0)
574 strncat(result
, "???", result_len
);
580 get_fullpath(char *first
, char *second
)
584 full_path
= ecalloc(MAX_P
, sizeof(char));
586 if (strncmp(first
, "/", MAX_P
) == 0)
587 (void)snprintf(full_path
, MAX_P
, "/%s", second
);
589 (void)snprintf(full_path
, MAX_P
, "%s/%s", first
, second
);
595 get_fusr(uid_t status
)
600 result
= ecalloc(MAX_USRN
, sizeof(char));
601 pw
= getpwuid(status
);
603 (void)snprintf(result
, MAX_USRN
, "%u", status
);
605 strncpy(result
, pw
->pw_name
, MAX_USRN
);
607 result
[MAX_USRN
- 1] = '\0';
612 get_dirsize(char *fullpath
, off_t
*fullsize
)
617 struct dirent
*entry
;
620 dir
= opendir(fullpath
);
625 while ((entry
= readdir(dir
)) != 0) {
626 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
627 strncmp(entry
->d_name
, "..", 3) == 0))
630 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
631 if (lstat(ent_full
, &status
) == 0) {
632 mode
= status
.st_mode
;
634 get_dirsize(ent_full
, fullsize
);
637 *fullsize
+= status
.st_size
;
648 get_hicol(Cpair
*col
, mode_t mode
)
653 else if (S_ISLNK(mode
))
655 else if (chech_execf(mode
) > 0)
660 delent(const Arg
*arg
)
668 inp_conf
= ecalloc(conf_len
, sizeof(char));
669 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
670 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
672 return; /* canceled by user or wrong inp_conf */
677 tmp
[0] = CURSOR(cpane
).name
;
678 if (spawn(rm_cmd
, rm_cmd_len
, tmp
, 1, NULL
) < 0) {
679 print_error(strerror(errno
));
685 calcdir(const Arg
*arg
)
689 if (!S_ISDIR(CURSOR(cpane
).mode
))
695 fullsize
= ecalloc(1, sizeof(off_t
));
696 get_dirsize(CURSOR(cpane
).name
, fullsize
);
697 csize
= get_fsize(*fullsize
);
699 CURSOR(cpane
).size
= *fullsize
;
700 print_info(cpane
, csize
);
707 char *user_input
, *path
;
709 user_input
= ecalloc(MAX_USRI
, sizeof(char));
710 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
715 path
= ecalloc(MAX_P
, sizeof(char));
716 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
722 PERROR(mkdir(path
, ndir_perm
) < 0);
731 char *user_input
, *path
;
734 user_input
= ecalloc(MAX_USRI
, sizeof(char));
735 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
740 path
= ecalloc(MAX_P
, sizeof(char));
741 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
747 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
750 print_error(strerror(errno
));
751 else if (close(rf
) < 0)
752 print_error(strerror(errno
));
758 mv_ver(const Arg
*arg
)
763 if (cpane
->hdir
- arg
->i
< 1) /* first line */
766 if (cpane
->hdir
- arg
->i
> cpane
->dirc
) /* last line */
769 if (cpane
->firstrow
> 0 && arg
->i
> 0 &&
770 cpane
->hdir
<= (cpane
->firstrow
+ arg
->i
)) { /* scroll up */
771 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
772 rm_hi(cpane
, cpane
->hdir
- 1);
773 cpane
->hdir
= cpane
->hdir
- arg
->i
;
775 add_hi(cpane
, cpane
->hdir
- 1);
779 if (cpane
->hdir
- cpane
->firstrow
>= scrheight
+ arg
->i
&&
780 arg
->i
< 0) { /* scroll down */
781 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
782 rm_hi(cpane
, cpane
->hdir
- 1);
783 cpane
->hdir
= cpane
->hdir
- arg
->i
;
785 add_hi(cpane
, cpane
->hdir
- 1);
789 rm_hi(cpane
, cpane
->hdir
- 1);
790 cpane
->hdir
= cpane
->hdir
- arg
->i
;
791 add_hi(cpane
, cpane
->hdir
- 1);
792 print_info(cpane
, NULL
);
798 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
802 get_dirp(cpane
->dirn
);
803 if (check_dir(cpane
->dirn
) < 0) {
804 print_error(strerror(errno
));
809 cpane
->firstrow
= cpane
->parent_firstrow
;
810 cpane
->hdir
= cpane
->parent_row
;
811 PERROR(listdir(cpane
) < 0);
812 cpane
->parent_firstrow
= 0;
813 cpane
->parent_row
= 1;
817 mvbtm(const Arg
*arg
)
821 if (cpane
->dirc
> scrheight
) {
822 rm_hi(cpane
, cpane
->hdir
- 1);
823 cpane
->hdir
= cpane
->dirc
;
824 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
826 add_hi(cpane
, cpane
->hdir
- 1);
828 rm_hi(cpane
, cpane
->hdir
- 1);
829 cpane
->hdir
= cpane
->dirc
;
830 add_hi(cpane
, cpane
->hdir
- 1);
832 print_info(cpane
, NULL
);
836 mvfwd(const Arg
*arg
)
842 switch (check_dir(CURSOR(cpane
).name
)) {
844 strncpy(cpane
->dirn
, CURSOR(cpane
).name
, MAX_P
);
845 cpane
->parent_row
= cpane
->hdir
;
846 cpane
->parent_firstrow
= cpane
->firstrow
;
850 PERROR(listdir(cpane
) < 0);
852 case 1: /* not a directory open file */
855 s
= opnf(CURSOR(cpane
).name
);
860 print_error("process failed non-zero exit");
862 case -1: /* failed to open directory */
863 print_error(strerror(errno
));
868 mvmid(const Arg
*arg
)
872 rm_hi(cpane
, cpane
->hdir
- 1);
873 if (cpane
->dirc
< scrheight
/ 2)
874 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
876 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
877 add_hi(cpane
, cpane
->hdir
- 1);
878 print_info(cpane
, NULL
);
882 mvtop(const Arg
*arg
)
886 if (cpane
->dirc
> scrheight
) {
887 rm_hi(cpane
, cpane
->hdir
- 1);
891 add_hi(cpane
, cpane
->hdir
- 1);
893 rm_hi(cpane
, cpane
->hdir
- 1);
895 add_hi(cpane
, cpane
->hdir
- 1);
896 print_info(cpane
, NULL
);
901 bkmrk(const Arg
*arg
)
903 if (check_dir((char *)arg
->v
) != 0) {
904 print_error(strerror(errno
));
909 strncpy(cpane
->dirn
, (char *)arg
->v
, MAX_P
);
911 cpane
->parent_row
= 1;
913 PERROR(listdir(cpane
) < 0);
917 get_usrinput(char *result
, size_t max_chars
, const char *fmt
, ...)
920 size_t i
, cpos
, startat
;
928 startat
= vsnprintf(msg
, MAX_N
, fmt
, vl
) + 1;
932 print_tb(msg
, 1, theight
- 1, cprompt
.fg
, cprompt
.bg
);
933 tb_set_cursor(startat
+ 1, theight
- 1);
936 while (tb_poll_event(&fev
) != 0) {
939 if (fev
.key
== TB_KEY_ESC
) {
940 tb_set_cursor(-1, -1);
945 if (fev
.key
== TB_KEY_BACKSPACE
||
946 fev
.key
== TB_KEY_BACKSPACE2
) {
947 if (BETWEEN(cpos
, 2, max_chars
)) {
948 result
[i
- 1] = '\0';
951 print_xstatus(' ', startat
+ cpos
);
953 startat
+ cpos
, theight
- 1);
956 } else if (fev
.key
== TB_KEY_ENTER
) {
957 tb_set_cursor(-1, -1);
958 result
[cpos
- 1] = '\0';
961 } else if (fev
.key
) { /* disable other TB_KEY_* */
965 if (cpos
< max_chars
) {
966 print_xstatus((char)fev
.ch
,
968 result
[i
] = (char)fev
.ch
;
969 tb_set_cursor((startat
+ cpos
+ 1),
979 case TB_EVENT_RESIZE
:
982 print_tb(msg
, 1, theight
- 1, cprompt
.fg
, cprompt
.bg
);
983 print_tb(result
, startat
+ 1, theight
- 1, cstatus
.fg
,
1001 for (c
= 0; c
< LEN(rules
); c
++)
1002 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1003 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1009 spawn(const void *com_argv
, size_t com_argc
, const void *f_argv
, size_t f_argc
,
1016 argc
= com_argc
+ f_argc
+ 2;
1019 memcpy(argv
, com_argv
, com_argc
* sizeof(char *)); /* command */
1020 memcpy(&argv
[com_argc
], f_argv
, f_argc
* sizeof(char *)); /* files */
1022 argv
[argc
- 2] = fn
;
1023 argv
[argc
- 1] = NULL
;
1030 execvp(argv
[0], argv
);
1033 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1037 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1053 if (c
< 0) /* extension not found open in editor */
1054 return spawn(editor
, 1, NULL
, 0, fn
);
1056 return spawn((char **)rules
[c
].v
, rules
[c
].vlen
, NULL
, 0, fn
);
1062 #if defined(_SYS_INOTIFY_H)
1063 inotify_fd
= inotify_init();
1066 #elif defined(_SYS_EVENT_H_)
1067 gtimeout
.tv_sec
= 1;
1076 addwatch(Pane
*pane
)
1078 #if defined(_SYS_INOTIFY_H)
1079 return pane
->inotify_wd
= inotify_add_watch(inotify_fd
, pane
->dirn
,
1080 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1081 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1082 #elif defined(_SYS_EVENT_H_)
1083 pane
->event_fd
= open(pane
->dirn
, O_RDONLY
);
1084 if (pane
->event_fd
< 0)
1085 return pane
->event_fd
;
1086 EV_SET(&evlist
[pane
->pane_id
], pane
->event_fd
, EVFILT_VNODE
,
1088 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
| NOTE_RENAME
|
1089 NOTE_REVOKE
| NOTE_WRITE
,
1098 #if defined(_SYS_INOTIFY_H)
1101 struct inotify_event
*event
;
1102 const size_t events
= 32;
1103 const size_t evbuflen
=
1104 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1107 if (cpane
->inotify_wd
< 0)
1109 r
= read(inotify_fd
, buf
, evbuflen
);
1113 for (p
= buf
; p
< buf
+ r
;) {
1114 event
= (struct inotify_event
*)p
;
1121 p
+= sizeof(struct inotify_event
) + event
->len
;
1123 #elif defined(_SYS_EVENT_H_)
1124 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1132 #if defined(_SYS_INOTIFY_H)
1133 if (pane
->inotify_wd
>= 0)
1134 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1135 #elif defined(_SYS_EVENT_H_)
1136 close(pane
->event_fd
);
1143 pthread_cancel(fsev_thread
);
1144 #if defined(__linux__)
1145 pthread_join(fsev_thread
, NULL
);
1147 rmwatch(&panes
[Left
]);
1148 rmwatch(&panes
[Right
]);
1149 #if defined(_SYS_INOTIFY_H)
1151 #elif defined(_SYS_EVENT_H_)
1157 toggle_df(const Arg
*arg
)
1159 show_dotfiles
= !show_dotfiles
;
1160 PERROR(listdir(&panes
[Left
]));
1161 PERROR(listdir(&panes
[Right
]));
1166 start_filter(const Arg
*arg
)
1168 if (cpane
->dirc
< 1)
1171 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1172 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1176 cpane
->filter
= user_input
;
1177 if (listdir(cpane
) < 0)
1178 print_error("no match");
1179 cpane
->filter
= NULL
;
1184 start_vmode(const Arg
*arg
)
1186 if (cpane
->dirc
< 1)
1188 struct tb_event fev
;
1189 if (sel_indexes
!= NULL
) {
1194 sel_indexes
= ecalloc(cpane
->dirc
, sizeof(size_t));
1195 sel_indexes
[0] = cpane
->hdir
;
1197 print_prompt("-- VISUAL --");
1199 while (tb_poll_event(&fev
) != 0) {
1202 grabkeys(&fev
, vkeys
, vkeyslen
);
1203 if (cont_vmode
== -1)
1212 exit_vmode(const Arg
*arg
)
1214 refresh_pane(cpane
);
1215 add_hi(cpane
, cpane
->hdir
- 1);
1220 selup(const Arg
*arg
)
1223 print_prompt("-- VISUAL --");
1224 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1226 if (cpane
->hdir
< sel_indexes
[0]) {
1227 sel_indexes
[index
] = cpane
->hdir
;
1228 add_hi(cpane
, sel_indexes
[index
]);
1229 } else if (index
< cpane
->dirc
) {
1230 sel_indexes
[index
+ 1] = 0;
1232 if (cpane
->dirc
>= scrheight
||
1233 cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1239 seldwn(const Arg
*arg
)
1242 print_prompt("-- VISUAL --");
1243 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1245 if (cpane
->hdir
> sel_indexes
[0]) {
1246 sel_indexes
[index
] = cpane
->hdir
;
1247 add_hi(cpane
, sel_indexes
[index
] - 2);
1249 sel_indexes
[index
+ 1] = 0;
1251 if (cpane
->dirc
>= scrheight
||
1252 cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1258 selall(const Arg
*arg
)
1261 for (i
= 0; i
< cpane
->dirc
; i
++) {
1262 sel_indexes
[i
] = i
+ 1;
1271 for (i
= 0; i
< cpane
->dirc
; i
++) {
1272 if (sel_indexes
[i
] < (scrheight
+ cpane
->firstrow
) &&
1274 cpane
->firstrow
) { /* checks if in the frame of the directories */
1275 add_hi(cpane
, sel_indexes
[i
] - 1);
1286 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1287 if (sel_indexes
[j
] != 0)
1299 if (sel_files
!= NULL
) {
1300 for (i
= 0; i
< sel_len
; i
++) {
1302 sel_files
[i
] = NULL
;
1316 sel_files
= ecalloc(sel_len
, sizeof(char *));
1318 for (i
= 0; i
< sel_len
; i
++) {
1319 sel_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1320 strncpy(sel_files
[i
], cpane
->direntr
[sel_indexes
[i
] - 1].name
,
1326 selynk(const Arg
*arg
)
1329 refresh_pane(cpane
);
1330 add_hi(cpane
, cpane
->hdir
- 1);
1331 print_status(cprompt
, "%zu files are yanked", sel_len
);
1336 seldel(const Arg
*arg
)
1340 inp_conf
= ecalloc(delconf_len
, sizeof(char));
1341 if ((get_usrinput(inp_conf
, delconf_len
, "delete file (yes) ?") < 0) ||
1342 (strncmp(inp_conf
, delconf
, delconf_len
) != 0)) {
1344 return; /* canceled by user or wrong inp_conf */
1350 if (spawn(rm_cmd
, rm_cmd_len
, sel_files
, sel_len
, NULL
) < 0)
1351 print_error(strerror(errno
));
1353 print_status(cprompt
, "%zu files are deleted", sel_len
);
1360 paste(const Arg
*arg
)
1362 if (sel_files
== NULL
) {
1363 print_error("nothing to paste");
1367 if (spawn(cp_cmd
, cp_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1368 print_error(strerror(errno
));
1370 print_status(cprompt
, "%zu files are copied", sel_len
);
1376 selmv(const Arg
*arg
)
1378 if (sel_files
== NULL
) {
1379 print_error("nothing to move");
1383 if (spawn(mv_cmd
, mv_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1384 print_error(strerror(errno
));
1386 print_status(cprompt
, "%zu files are moved", sel_len
);
1392 rname(const Arg
*arg
)
1394 if (cpane
->dirc
< 1)
1396 char new_name
[MAX_P
];
1399 input_name
= ecalloc(MAX_N
, sizeof(char));
1401 if (get_usrinput(input_name
, MAX_N
, "rename: %s",
1402 basename(CURSOR(cpane
).name
)) < 0) {
1407 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1409 print_error(strerror(errno
));
1413 char *rename_cmd
[] = { "mv", CURSOR(cpane
).name
, new_name
};
1414 PERROR(spawn(rename_cmd
, 3, NULL
, 0, NULL
) < 0);
1420 yank(const Arg
*arg
)
1422 if (cpane
->dirc
< 1)
1427 sel_files
= ecalloc(sel_len
, sizeof(char *));
1428 sel_files
[0] = ecalloc(MAX_P
, sizeof(char));
1429 strncpy(sel_files
[0], CURSOR(cpane
).name
, MAX_P
);
1430 print_status(cprompt
, "1 file is yanked", sel_len
);
1434 switch_pane(const Arg
*arg
)
1436 if (cpane
->dirc
> 0)
1437 rm_hi(cpane
, cpane
->hdir
- 1);
1438 cpane
= &panes
[pane_idx
^= 1];
1439 if (cpane
->dirc
> 0) {
1440 add_hi(cpane
, cpane
->hdir
- 1);
1441 print_info(cpane
, NULL
);
1448 quit(const Arg
*arg
)
1450 if (cont_vmode
== -1) { /* check if selection was allocated */
1452 if (sel_files
!= NULL
)
1455 free(panes
[Left
].direntr
);
1456 free(panes
[Right
].direntr
);
1463 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1467 for (i
= 0; i
< max_keys
; i
++) {
1468 if (event
->ch
!= 0) {
1469 if (event
->ch
== key
[i
].evkey
.ch
) {
1470 key
[i
].func(&key
[i
].arg
);
1473 } else if (event
->key
!= 0) {
1474 if (event
->key
== key
[i
].evkey
.key
) {
1475 key
[i
].func(&key
[i
].arg
);
1485 struct timespec tim
;
1487 tim
.tv_nsec
= 5000000L; /* 0.005 sec */
1490 if (read_events() > READEVSZ
) {
1491 kill(main_pid
, SIGUSR1
);
1492 nanosleep(&tim
, NULL
);
1502 while (tb_poll_event(&ev
) != 0) {
1505 grabkeys(&ev
, nkeys
, nkeyslen
);
1508 case TB_EVENT_RESIZE
:
1519 refresh_pane(Pane
*pane
)
1521 size_t y
, dyn_max
, start_from
;
1523 width
= (twidth
/ 2) - 4;
1527 start_from
= pane
->firstrow
;
1528 dyn_max
= MIN(pane
->dirc
, (scrheight
- 1) + pane
->firstrow
);
1530 /* print each entry in directory */
1531 while (start_from
< dyn_max
) {
1532 get_hicol(&col
, pane
->direntr
[start_from
].mode
);
1533 print_row(pane
, start_from
, col
);
1539 print_info(pane
, NULL
);
1543 /* print current directory title */
1544 pane
->dircol
.fg
|= TB_BOLD
;
1545 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", width
, pane
->dirn
);
1549 set_direntr(Pane
*pane
, struct dirent
*entry
, DIR *dir
, char *filter
)
1556 tmpfull = get_fullpath(pane->dirn, entry->d_name); \
1557 strncpy(pane->direntr[i].name, tmpfull, MAX_N); \
1558 if (lstat(tmpfull, &status) == 0) { \
1559 pane->direntr[i].size = status.st_size; \
1560 pane->direntr[i].mode = status.st_mode; \
1561 pane->direntr[i].group = status.st_gid; \
1562 pane->direntr[i].user = status.st_uid; \
1563 pane->direntr[i].dt = status.st_mtime; \
1570 erealloc(pane
->direntr
, (10 + pane
->dirc
) * sizeof(Entry
));
1571 while ((entry
= readdir(dir
)) != 0) {
1572 if (show_dotfiles
== 1) {
1573 if (entry
->d_name
[0] == '.' &&
1574 (entry
->d_name
[1] == '\0' ||
1575 entry
->d_name
[1] == '.'))
1578 if (entry
->d_name
[0] == '.')
1582 if (filter
== NULL
) {
1584 } else if (filter
!= NULL
) {
1585 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1598 struct dirent
*entry
;
1600 int filtercount
= 0;
1601 size_t oldc
= pane
->dirc
;
1603 width
= (twidth
/ 2) - 4;
1606 dir
= opendir(pane
->dirn
);
1610 /* get content and filter sum */
1611 while ((entry
= readdir(dir
)) != 0) {
1612 if (pane
->filter
!= NULL
) {
1613 if (strcasestr(entry
->d_name
, pane
->filter
) != NULL
)
1615 } else { /* no filter */
1620 if (pane
->filter
== NULL
) {
1625 if (pane
->filter
!= NULL
) {
1626 if (filtercount
> 0) {
1627 pane
->dirc
= filtercount
;
1630 } else if (filtercount
== 0) {
1631 if (closedir(dir
) < 0)
1638 /* print current directory title */
1639 pane
->dircol
.fg
|= TB_BOLD
;
1640 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", width
, pane
->dirn
);
1642 if (pane
->filter
== NULL
) /* dont't watch when filtering */
1643 if (addwatch(pane
) < 0)
1644 print_error("can't add watch");
1646 /* empty directory */
1647 if (pane
->dirc
== 0) {
1649 if (closedir(dir
) < 0)
1654 rewinddir(dir
); /* reset position */
1656 pane
, entry
, dir
, pane
->filter
); /* create array of entries */
1657 qsort(pane
->direntr
, pane
->dirc
, sizeof(Entry
), sort_name
);
1660 if (pane
->hdir
> pane
->dirc
)
1661 pane
->hdir
= pane
->dirc
;
1663 if (pane
== cpane
&& pane
->dirc
> 0)
1664 add_hi(pane
, pane
->hdir
- 1);
1666 if (closedir(dir
) < 0)
1676 panes
[Right
].dirx
= (twidth
/ 2) + 2;
1677 refresh_pane(&panes
[Left
]);
1678 refresh_pane(&panes
[Right
]);
1679 if (cpane
->dirc
> 0)
1680 add_hi(cpane
, cpane
->hdir
- 1);
1687 editor
[0] = getenv("EDITOR");
1690 if (editor
[0] == NULL
)
1700 home
= getenv("HOME");
1703 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1704 strncpy(cwd
, home
, MAX_P
);
1706 pane_idx
= Left
; /* cursor pane */
1707 cpane
= &panes
[pane_idx
];
1709 panes
[Left
].ex
= (twidth
/ 2) - 1;
1710 panes
[Left
].pane_id
= 0;
1711 panes
[Left
].dirx
= 2;
1712 panes
[Left
].dircol
= cpanell
;
1713 panes
[Left
].firstrow
= 0;
1714 panes
[Left
].direntr
= ecalloc(0, sizeof(Entry
));
1715 strncpy(panes
[Left
].dirn
, cwd
, MAX_P
);
1716 panes
[Left
].hdir
= 1;
1717 panes
[Left
].inotify_wd
= -1;
1718 panes
[Left
].parent_row
= 1;
1720 panes
[Right
].ex
= twidth
- 1;
1721 panes
[Right
].pane_id
= 1;
1722 panes
[Right
].dirx
= (twidth
/ 2) + 2;
1723 panes
[Right
].dircol
= cpanelr
;
1724 panes
[Right
].firstrow
= 0;
1725 panes
[Right
].direntr
= ecalloc(0, sizeof(Entry
));
1726 strncpy(panes
[Right
].dirn
, home
, MAX_P
);
1727 panes
[Right
].hdir
= 1;
1728 panes
[Right
].inotify_wd
= -1;
1729 panes
[Right
].parent_row
= 1;
1736 theight
= tb_height();
1737 twidth
= tb_width();
1738 scrheight
= theight
- 2;
1740 /* 2 horizontal lines */
1741 for (i
= 1; i
< twidth
- 1; ++i
) {
1742 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1743 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1746 /* 4 vertical lines */
1747 for (i
= 1; i
< theight
- 1; ++i
) {
1748 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1750 (twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
, cframe
.bg
);
1751 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1753 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1757 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1758 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1759 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1760 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1762 /* 2 middel top and bottom */
1763 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1765 (twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1772 PERROR(listdir(&panes
[Left
]));
1773 PERROR(listdir(&panes
[Right
]));
1780 struct sigaction sa
;
1782 main_pid
= getpid();
1783 sa
.sa_handler
= th_handler
;
1784 sigemptyset(&sa
.sa_mask
);
1785 sa
.sa_flags
= SA_RESTART
;
1786 return sigaction(SIGUSR1
, &sa
, NULL
);
1792 FAIL_IF(tb_init() != 0, "tb_init()");
1793 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1794 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1795 die("output error");
1799 PERROR(start_signal() < 0);
1800 PERROR(fsev_init() < 0);
1801 PERROR(listdir(&panes
[Left
]) < 0);
1802 PERROR(listdir(&panes
[Right
]) < 0);
1805 pthread_create(&fsev_thread
, NULL
, read_th
, NULL
);
1810 main(int argc
, char *argv
[])
1812 #if defined(__OpenBSD__)
1813 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1816 #endif /* __OpenBSD__ */
1819 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1820 die("sfm-" VERSION
);
1822 die("usage: sfm [-v]");