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 mvtop(const Arg
*arg
);
144 static void bkmrk(const Arg
*arg
);
145 static int get_usrinput(char *, size_t, const char *, ...);
146 static int frules(char *);
147 static int spawn(const void *, size_t, const void *, size_t, char *);
148 static int opnf(char *);
149 static int fsev_init(void);
150 static int addwatch(Pane
*);
151 static int read_events(void);
152 static void rmwatch(Pane
*);
153 static void fsev_shdn(void);
154 static void toggle_df(const Arg
*arg
);
155 static void start_filter(const Arg
*arg
);
156 static void start_vmode(const Arg
*arg
);
157 static void exit_vmode(const Arg
*arg
);
158 static void selup(const Arg
*arg
);
159 static void seldwn(const Arg
*arg
);
160 static void selall(const Arg
*arg
);
161 static void selref(void);
162 static void selynk(const Arg
*arg
);
163 static void selcalc(void);
164 static void paste(const Arg
*arg
);
165 static void selmv(const Arg
*arg
);
166 static void seldel(const Arg
*arg
);
167 static void init_files(void);
168 static void free_files(void);
169 static void yank(const Arg
*arg
);
170 static void rname(const Arg
*arg
);
171 static void switch_pane(const Arg
*arg
);
172 static void quit(const Arg
*arg
);
173 static void grabkeys(struct tb_event
*, Key
*, size_t);
174 static void *read_th(void *arg
);
175 static void start_ev(void);
176 static void refresh_pane(Pane
*);
177 static void set_direntr(Pane
*, struct dirent
*, DIR *, char *);
178 static int listdir(Pane
*);
179 static void t_resize(void);
180 static void set_panes(void);
181 static void draw_frame(void);
182 static void start(void);
184 /* global variables */
185 static pthread_t fsev_thread
;
186 static Pane panes
[2];
189 static char *editor
[2];
190 static char fed
[] = "vi";
191 static int theight
, twidth
, scrheight
;
192 static int *sel_indexes
;
193 static size_t sel_len
= 0;
194 static char **sel_files
;
195 static int cont_vmode
= 0;
197 #if defined(_SYS_INOTIFY_H)
199 static int inotify_fd
;
200 #elif defined(_SYS_EVENT_H_)
203 struct kevent evlist
[2]; /* events we want to monitor */
204 struct kevent chlist
[2]; /* events that were triggered */
205 static struct timespec gtimeout
;
207 #if defined(__linux__) || defined(__FreeBSD__)
209 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
212 enum { Left
, Right
}; /* panes */
214 /* configuration, allows nested code to access above variables */
217 /* function implementations */
219 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
221 while (*str
!= '\0') {
223 str
+= tb_utf8_char_to_unicode(&uni
, str
);
224 tb_change_cell(x
, y
, uni
, fg
, bg
);
230 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
235 (void)vsnprintf(buf
, MAX_LINE
, fmt
, vl
);
237 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
241 print_status(Cpair col
, const char *fmt
, ...)
243 char buf
[MAX_STATUS
];
246 (void)vsnprintf(buf
, MAX_STATUS
, fmt
, vl
);
249 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
253 print_xstatus(char c
, int x
)
256 (void)tb_utf8_char_to_unicode(&uni
, &c
);
257 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
261 print_error(char *errmsg
)
263 print_status(cerr
, errmsg
);
267 print_prompt(char *prompt
)
269 print_status(cprompt
, prompt
);
273 print_info(Pane
*pane
, char *dirsize
)
275 char *sz
, *ur
, *gr
, *dt
, *prm
;
277 dt
= ecalloc(MAX_DTF
, sizeof(char));
279 prm
= get_fperm(CURSOR(pane
).mode
);
280 ur
= get_fusr(CURSOR(pane
).user
);
281 gr
= get_fgrp(CURSOR(pane
).group
);
283 if (get_fdt(dt
, CURSOR(pane
).dt
) < 0)
286 if (S_ISREG(CURSOR(pane
).mode
)) {
287 sz
= get_fsize(CURSOR(pane
).size
);
289 if (dirsize
== NULL
) {
290 sz
= ecalloc(1, sizeof(char));
297 print_status(cstatus
, "%02d/%02d %s %s:%s %s %s", pane
->hdir
,
298 pane
->dirc
, prm
, ur
, gr
, dt
, sz
);
308 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
313 char lnk_full
[MAX_P
];
316 width
= (twidth
/ 2) - 4;
317 result
= basename(pane
->direntr
[entpos
].name
);
319 y
= entpos
- pane
->firstrow
+ 1;
321 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
322 if (realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
324 lnk_full
, MAX_N
, "%s -> %s", result
, buf
);
329 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
333 clear(int sx
, int ex
, int y
, uint16_t bg
)
335 /* clear line from to */
336 /* x = line number vertical */
337 /* y = column number horizontal */
339 for (i
= sx
; i
< ex
; i
++) {
340 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
347 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
351 clear_pane(Pane
*pane
)
356 while (i
< scrheight
) {
357 clear(pane
->dirx
, pane
->ex
, y
, TB_DEFAULT
);
363 for (y
= pane
->dirx
; y
< pane
->ex
; ++y
) {
364 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
369 add_hi(Pane
*pane
, size_t entpos
)
372 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
373 col
.fg
|= TB_REVERSE
| TB_BOLD
;
374 col
.bg
|= TB_REVERSE
;
375 print_row(pane
, entpos
, col
);
379 rm_hi(Pane
*pane
, size_t entpos
)
382 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
383 print_row(pane
, entpos
, col
);
387 check_dir(char *path
)
393 if (errno
== ENOTDIR
) {
400 if (closedir(dir
) < 0)
407 chech_execf(mode_t mode
)
410 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
415 sort_name(const void *const A
, const void *const B
)
418 mode_t data1
= (*(Entry
*)A
).mode
;
419 mode_t data2
= (*(Entry
*)B
).mode
;
423 } else if (data1
== data2
) {
424 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
437 len
= strnlen(cdir
, MAX_P
);
441 for (i
= len
- 1; i
> 1; i
--) {
448 cdir
[len
- counter
- 1] = '\0';
456 size_t counter
, len
, i
;
460 len
= strnlen(str
, MAX_N
);
462 for (i
= len
- 1; i
> 0; i
--) {
470 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
471 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
477 get_fdt(char *result
, time_t status
)
480 localtime_r(&status
, <
);
481 return strftime(result
, MAX_DTF
, dtfmt
, <
);
485 get_fgrp(gid_t status
)
490 result
= ecalloc(MAX_GRPN
, sizeof(char));
491 gr
= getgrgid(status
);
493 (void)snprintf(result
, MAX_GRPN
, "%u", status
);
495 strncpy(result
, gr
->gr_name
, MAX_GRPN
);
497 result
[MAX_GRPN
- 1] = '\0';
502 get_fperm(mode_t mode
)
507 const char chars
[] = "rwxrwxrwx";
508 buf
= ecalloc(11, sizeof(char));
512 else if (S_ISREG(mode
))
514 else if (S_ISLNK(mode
))
516 else if (S_ISBLK(mode
))
518 else if (S_ISCHR(mode
))
520 else if (S_ISFIFO(mode
))
522 else if (S_ISSOCK(mode
))
527 for (i
= 1; i
< 10; i
++) {
528 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
536 get_fsize(off_t size
)
538 char *result
; /* need to be freed */
544 result_len
= 6; /* 9999X/0 */
545 result
= ecalloc(result_len
, sizeof(char));
547 while (size
>= 1000) {
572 if (snprintf(result
, result_len
, OFF_T
"%c", size
, unit
) < 0)
573 strncat(result
, "???", result_len
);
579 get_fullpath(char *first
, char *second
)
583 full_path
= ecalloc(MAX_P
, sizeof(char));
585 if (strncmp(first
, "/", MAX_P
) == 0)
586 (void)snprintf(full_path
, MAX_P
, "/%s", second
);
588 (void)snprintf(full_path
, MAX_P
, "%s/%s", first
, second
);
594 get_fusr(uid_t status
)
599 result
= ecalloc(MAX_USRN
, sizeof(char));
600 pw
= getpwuid(status
);
602 (void)snprintf(result
, MAX_USRN
, "%u", status
);
604 strncpy(result
, pw
->pw_name
, MAX_USRN
);
606 result
[MAX_USRN
- 1] = '\0';
611 get_dirsize(char *fullpath
, off_t
*fullsize
)
616 struct dirent
*entry
;
619 dir
= opendir(fullpath
);
624 while ((entry
= readdir(dir
)) != 0) {
625 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
626 strncmp(entry
->d_name
, "..", 3) == 0))
629 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
630 if (lstat(ent_full
, &status
) == 0) {
631 mode
= status
.st_mode
;
633 get_dirsize(ent_full
, fullsize
);
636 *fullsize
+= status
.st_size
;
647 get_hicol(Cpair
*col
, mode_t mode
)
652 else if (S_ISLNK(mode
))
654 else if (chech_execf(mode
) > 0)
659 delent(const Arg
*arg
)
667 inp_conf
= ecalloc(conf_len
, sizeof(char));
668 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
669 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
671 return; /* canceled by user or wrong inp_conf */
676 tmp
[0] = CURSOR(cpane
).name
;
677 if (spawn(rm_cmd
, rm_cmd_len
, tmp
, 1, NULL
) < 0) {
678 print_error(strerror(errno
));
684 calcdir(const Arg
*arg
)
688 if (!S_ISDIR(CURSOR(cpane
).mode
))
694 fullsize
= ecalloc(1, sizeof(off_t
));
695 get_dirsize(CURSOR(cpane
).name
, fullsize
);
696 csize
= get_fsize(*fullsize
);
698 CURSOR(cpane
).size
= *fullsize
;
699 print_info(cpane
, csize
);
706 char *user_input
, *path
;
708 user_input
= ecalloc(MAX_USRI
, sizeof(char));
709 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
714 path
= ecalloc(MAX_P
, sizeof(char));
715 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
721 PERROR(mkdir(path
, ndir_perm
) < 0);
730 char *user_input
, *path
;
733 user_input
= ecalloc(MAX_USRI
, sizeof(char));
734 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
739 path
= ecalloc(MAX_P
, sizeof(char));
740 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
746 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
749 print_error(strerror(errno
));
750 else if (close(rf
) < 0)
751 print_error(strerror(errno
));
757 mv_ver(const Arg
*arg
)
762 if (cpane
->hdir
- arg
->i
< 1) /* first line */
765 if (cpane
->hdir
- arg
->i
> cpane
->dirc
) /* last line */
768 if (cpane
->firstrow
> 0 && arg
->i
> 0 &&
769 cpane
->hdir
<= (cpane
->firstrow
+ arg
->i
)) { /* scroll up */
770 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
771 rm_hi(cpane
, cpane
->hdir
- 1);
772 cpane
->hdir
= cpane
->hdir
- arg
->i
;
774 add_hi(cpane
, cpane
->hdir
- 1);
778 if (cpane
->hdir
- cpane
->firstrow
>= scrheight
+ arg
->i
&&
779 arg
->i
< 0) { /* scroll down */
780 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
781 rm_hi(cpane
, cpane
->hdir
- 1);
782 cpane
->hdir
= cpane
->hdir
- arg
->i
;
784 add_hi(cpane
, cpane
->hdir
- 1);
788 rm_hi(cpane
, cpane
->hdir
- 1);
789 cpane
->hdir
= cpane
->hdir
- arg
->i
;
790 add_hi(cpane
, cpane
->hdir
- 1);
791 print_info(cpane
, NULL
);
797 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
801 get_dirp(cpane
->dirn
);
802 if (check_dir(cpane
->dirn
) < 0) {
803 print_error(strerror(errno
));
808 cpane
->firstrow
= cpane
->parent_firstrow
;
809 cpane
->hdir
= cpane
->parent_row
;
810 PERROR(listdir(cpane
) < 0);
811 cpane
->parent_firstrow
= 0;
812 cpane
->parent_row
= 1;
816 mvbtm(const Arg
*arg
)
820 if (cpane
->dirc
> scrheight
) {
821 rm_hi(cpane
, cpane
->hdir
- 1);
822 cpane
->hdir
= cpane
->dirc
;
823 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
825 add_hi(cpane
, cpane
->hdir
- 1);
827 rm_hi(cpane
, cpane
->hdir
- 1);
828 cpane
->hdir
= cpane
->dirc
;
829 add_hi(cpane
, cpane
->hdir
- 1);
831 print_info(cpane
, NULL
);
835 mvfwd(const Arg
*arg
)
841 switch (check_dir(CURSOR(cpane
).name
)) {
843 strncpy(cpane
->dirn
, CURSOR(cpane
).name
, MAX_P
);
844 cpane
->parent_row
= cpane
->hdir
;
845 cpane
->parent_firstrow
= cpane
->firstrow
;
849 PERROR(listdir(cpane
) < 0);
851 case 1: /* not a directory open file */
854 s
= opnf(CURSOR(cpane
).name
);
859 print_error("process failed non-zero exit");
861 case -1: /* failed to open directory */
862 print_error(strerror(errno
));
867 mvtop(const Arg
*arg
)
871 if (cpane
->dirc
> scrheight
) {
872 rm_hi(cpane
, cpane
->hdir
- 1);
876 add_hi(cpane
, cpane
->hdir
- 1);
878 rm_hi(cpane
, cpane
->hdir
- 1);
880 add_hi(cpane
, cpane
->hdir
- 1);
881 print_info(cpane
, NULL
);
886 bkmrk(const Arg
*arg
)
888 if (check_dir((char *)arg
->v
) != 0) {
889 print_error(strerror(errno
));
894 strncpy(cpane
->dirn
, (char *)arg
->v
, MAX_P
);
896 cpane
->parent_row
= 1;
898 PERROR(listdir(cpane
) < 0);
902 get_usrinput(char *result
, size_t max_chars
, const char *fmt
, ...)
905 size_t i
, cpos
, startat
;
913 startat
= vsnprintf(msg
, MAX_N
, fmt
, vl
) + 1;
917 print_tb(msg
, 1, theight
- 1, cprompt
.fg
, cprompt
.bg
);
918 tb_set_cursor(startat
+ 1, theight
- 1);
921 while (tb_poll_event(&fev
) != 0) {
924 if (fev
.key
== TB_KEY_ESC
) {
925 tb_set_cursor(-1, -1);
930 if (fev
.key
== TB_KEY_BACKSPACE
||
931 fev
.key
== TB_KEY_BACKSPACE2
) {
932 if (BETWEEN(cpos
, 2, max_chars
)) {
933 result
[i
- 1] = '\0';
936 print_xstatus(' ', startat
+ cpos
);
938 startat
+ cpos
, theight
- 1);
941 } else if (fev
.key
== TB_KEY_ENTER
) {
942 tb_set_cursor(-1, -1);
943 result
[cpos
- 1] = '\0';
946 } else if (fev
.key
) { /* disable other TB_KEY_* */
950 if (cpos
< max_chars
) {
951 print_xstatus((char)fev
.ch
,
953 result
[i
] = (char)fev
.ch
;
954 tb_set_cursor((startat
+ cpos
+ 1),
964 case TB_EVENT_RESIZE
:
967 print_tb(msg
, 1, theight
- 1, cprompt
.fg
, cprompt
.bg
);
968 print_tb(result
, startat
+ 1, theight
- 1, cstatus
.fg
,
986 for (c
= 0; c
< LEN(rules
); c
++)
987 for (d
= 0; d
< rules
[c
].exlen
; d
++)
988 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
994 spawn(const void *com_argv
, size_t com_argc
, const void *f_argv
, size_t f_argc
,
1001 argc
= com_argc
+ f_argc
+ 2;
1004 memcpy(argv
, com_argv
, com_argc
* sizeof(char *)); /* command */
1005 memcpy(&argv
[com_argc
], f_argv
, f_argc
* sizeof(char *)); /* files */
1007 argv
[argc
- 2] = fn
;
1008 argv
[argc
- 1] = NULL
;
1015 execvp(argv
[0], argv
);
1018 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1022 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1038 if (c
< 0) /* extension not found open in editor */
1039 return spawn(editor
, 1, NULL
, 0, fn
);
1041 return spawn((char **)rules
[c
].v
, rules
[c
].vlen
, NULL
, 0, fn
);
1047 #if defined(_SYS_INOTIFY_H)
1048 inotify_fd
= inotify_init();
1051 #elif defined(_SYS_EVENT_H_)
1052 gtimeout
.tv_sec
= 1;
1061 addwatch(Pane
*pane
)
1063 #if defined(_SYS_INOTIFY_H)
1064 return pane
->inotify_wd
= inotify_add_watch(inotify_fd
, pane
->dirn
,
1065 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1066 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1067 #elif defined(_SYS_EVENT_H_)
1068 pane
->event_fd
= open(pane
->dirn
, O_RDONLY
);
1069 if (pane
->event_fd
< 0)
1070 return pane
->event_fd
;
1071 EV_SET(&evlist
[pane
->pane_id
], pane
->event_fd
, EVFILT_VNODE
,
1073 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
| NOTE_RENAME
|
1074 NOTE_REVOKE
| NOTE_WRITE
,
1083 #if defined(_SYS_INOTIFY_H)
1086 struct inotify_event
*event
;
1087 const size_t events
= 32;
1088 const size_t evbuflen
=
1089 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1092 if (cpane
->inotify_wd
< 0)
1094 r
= read(inotify_fd
, buf
, evbuflen
);
1098 for (p
= buf
; p
< buf
+ r
;) {
1099 event
= (struct inotify_event
*)p
;
1106 p
+= sizeof(struct inotify_event
) + event
->len
;
1108 #elif defined(_SYS_EVENT_H_)
1109 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1117 #if defined(_SYS_INOTIFY_H)
1118 if (pane
->inotify_wd
>= 0)
1119 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1120 #elif defined(_SYS_EVENT_H_)
1121 close(pane
->event_fd
);
1128 pthread_cancel(fsev_thread
);
1129 #if defined(__linux__)
1130 pthread_join(fsev_thread
, NULL
);
1132 rmwatch(&panes
[Left
]);
1133 rmwatch(&panes
[Right
]);
1134 #if defined(_SYS_INOTIFY_H)
1136 #elif defined(_SYS_EVENT_H_)
1142 toggle_df(const Arg
*arg
)
1144 show_dotfiles
= !show_dotfiles
;
1145 PERROR(listdir(&panes
[Left
]));
1146 PERROR(listdir(&panes
[Right
]));
1151 start_filter(const Arg
*arg
)
1153 if (cpane
->dirc
< 1)
1156 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1157 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1161 cpane
->filter
= user_input
;
1162 if (listdir(cpane
) < 0)
1163 print_error("no match");
1164 cpane
->filter
= NULL
;
1169 start_vmode(const Arg
*arg
)
1171 if (cpane
->dirc
< 1)
1173 struct tb_event fev
;
1174 if (sel_indexes
!= NULL
) {
1179 sel_indexes
= ecalloc(cpane
->dirc
, sizeof(size_t));
1180 sel_indexes
[0] = cpane
->hdir
;
1182 print_prompt("-- VISUAL --");
1184 while (tb_poll_event(&fev
) != 0) {
1187 grabkeys(&fev
, vkeys
, vkeyslen
);
1188 if (cont_vmode
== -1)
1197 exit_vmode(const Arg
*arg
)
1199 refresh_pane(cpane
);
1200 add_hi(cpane
, cpane
->hdir
- 1);
1205 selup(const Arg
*arg
)
1208 print_prompt("-- VISUAL --");
1209 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1211 if (cpane
->hdir
< sel_indexes
[0]) {
1212 sel_indexes
[index
] = cpane
->hdir
;
1213 add_hi(cpane
, sel_indexes
[index
]);
1214 } else if (index
< cpane
->dirc
) {
1215 sel_indexes
[index
+ 1] = 0;
1217 if (cpane
->dirc
>= scrheight
||
1218 cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1224 seldwn(const Arg
*arg
)
1227 print_prompt("-- VISUAL --");
1228 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1230 if (cpane
->hdir
> sel_indexes
[0]) {
1231 sel_indexes
[index
] = cpane
->hdir
;
1232 add_hi(cpane
, sel_indexes
[index
] - 2);
1234 sel_indexes
[index
+ 1] = 0;
1236 if (cpane
->dirc
>= scrheight
||
1237 cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1243 selall(const Arg
*arg
)
1246 for (i
= 0; i
< cpane
->dirc
; i
++) {
1247 sel_indexes
[i
] = i
+ 1;
1256 for (i
= 0; i
< cpane
->dirc
; i
++) {
1257 if (sel_indexes
[i
] < (scrheight
+ cpane
->firstrow
) &&
1259 cpane
->firstrow
) { /* checks if in the frame of the directories */
1260 add_hi(cpane
, sel_indexes
[i
] - 1);
1271 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1272 if (sel_indexes
[j
] != 0)
1284 if (sel_files
!= NULL
) {
1285 for (i
= 0; i
< sel_len
; i
++) {
1287 sel_files
[i
] = NULL
;
1301 sel_files
= ecalloc(sel_len
, sizeof(char *));
1303 for (i
= 0; i
< sel_len
; i
++) {
1304 sel_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1305 strncpy(sel_files
[i
], cpane
->direntr
[sel_indexes
[i
] - 1].name
,
1311 selynk(const Arg
*arg
)
1314 refresh_pane(cpane
);
1315 add_hi(cpane
, cpane
->hdir
- 1);
1316 print_status(cprompt
, "%zu files are yanked", sel_len
);
1321 seldel(const Arg
*arg
)
1325 inp_conf
= ecalloc(delconf_len
, sizeof(char));
1326 if ((get_usrinput(inp_conf
, delconf_len
, "delete file (yes) ?") < 0) ||
1327 (strncmp(inp_conf
, delconf
, delconf_len
) != 0)) {
1329 return; /* canceled by user or wrong inp_conf */
1335 if (spawn(rm_cmd
, rm_cmd_len
, sel_files
, sel_len
, NULL
) < 0)
1336 print_error(strerror(errno
));
1338 print_status(cprompt
, "%zu files are deleted", sel_len
);
1345 paste(const Arg
*arg
)
1347 if (sel_files
== NULL
) {
1348 print_error("nothing to paste");
1352 if (spawn(cp_cmd
, cp_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1353 print_error(strerror(errno
));
1355 print_status(cprompt
, "%zu files are copied", sel_len
);
1361 selmv(const Arg
*arg
)
1363 if (sel_files
== NULL
) {
1364 print_error("nothing to move");
1368 if (spawn(mv_cmd
, mv_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1369 print_error(strerror(errno
));
1371 print_status(cprompt
, "%zu files are moved", sel_len
);
1377 rname(const Arg
*arg
)
1379 if (cpane
->dirc
< 1)
1381 char new_name
[MAX_P
];
1384 input_name
= ecalloc(MAX_N
, sizeof(char));
1386 if (get_usrinput(input_name
, MAX_N
, "rename: %s",
1387 basename(CURSOR(cpane
).name
)) < 0) {
1392 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1394 print_error(strerror(errno
));
1398 char *rename_cmd
[] = { "mv", CURSOR(cpane
).name
, new_name
};
1399 PERROR(spawn(rename_cmd
, 3, NULL
, 0, NULL
) < 0);
1405 yank(const Arg
*arg
)
1407 if (cpane
->dirc
< 1)
1412 sel_files
= ecalloc(sel_len
, sizeof(char *));
1413 sel_files
[0] = ecalloc(MAX_P
, sizeof(char));
1414 strncpy(sel_files
[0], CURSOR(cpane
).name
, MAX_P
);
1415 print_status(cprompt
, "1 file is yanked", sel_len
);
1419 switch_pane(const Arg
*arg
)
1421 if (cpane
->dirc
> 0)
1422 rm_hi(cpane
, cpane
->hdir
- 1);
1423 cpane
= &panes
[pane_idx
^= 1];
1424 if (cpane
->dirc
> 0) {
1425 add_hi(cpane
, cpane
->hdir
- 1);
1426 print_info(cpane
, NULL
);
1433 quit(const Arg
*arg
)
1435 if (cont_vmode
== -1) { /* check if selection was allocated */
1437 if (sel_files
!= NULL
)
1440 free(panes
[Left
].direntr
);
1441 free(panes
[Right
].direntr
);
1448 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1452 for (i
= 0; i
< max_keys
; i
++) {
1453 if (event
->ch
!= 0) {
1454 if (event
->ch
== key
[i
].evkey
.ch
) {
1455 key
[i
].func(&key
[i
].arg
);
1458 } else if (event
->key
!= 0) {
1459 if (event
->key
== key
[i
].evkey
.key
) {
1460 key
[i
].func(&key
[i
].arg
);
1470 struct timespec tim
;
1472 tim
.tv_nsec
= 5000000L; /* 0.005 sec */
1475 if (read_events() > READEVSZ
) {
1476 kill(main_pid
, SIGUSR1
);
1477 nanosleep(&tim
, NULL
);
1487 while (tb_poll_event(&ev
) != 0) {
1490 grabkeys(&ev
, nkeys
, nkeyslen
);
1493 case TB_EVENT_RESIZE
:
1504 refresh_pane(Pane
*pane
)
1506 size_t y
, dyn_max
, start_from
;
1508 width
= (twidth
/ 2) - 4;
1512 start_from
= pane
->firstrow
;
1513 dyn_max
= MIN(pane
->dirc
, (scrheight
- 1) + pane
->firstrow
);
1515 /* print each entry in directory */
1516 while (start_from
< dyn_max
) {
1517 get_hicol(&col
, pane
->direntr
[start_from
].mode
);
1518 print_row(pane
, start_from
, col
);
1524 print_info(pane
, NULL
);
1528 /* print current directory title */
1529 pane
->dircol
.fg
|= TB_BOLD
;
1530 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", width
, pane
->dirn
);
1534 set_direntr(Pane
*pane
, struct dirent
*entry
, DIR *dir
, char *filter
)
1541 tmpfull = get_fullpath(pane->dirn, entry->d_name); \
1542 strncpy(pane->direntr[i].name, tmpfull, MAX_N); \
1543 if (lstat(tmpfull, &status) == 0) { \
1544 pane->direntr[i].size = status.st_size; \
1545 pane->direntr[i].mode = status.st_mode; \
1546 pane->direntr[i].group = status.st_gid; \
1547 pane->direntr[i].user = status.st_uid; \
1548 pane->direntr[i].dt = status.st_mtime; \
1555 erealloc(pane
->direntr
, (10 + pane
->dirc
) * sizeof(Entry
));
1556 while ((entry
= readdir(dir
)) != 0) {
1557 if (show_dotfiles
== 1) {
1558 if (entry
->d_name
[0] == '.' &&
1559 (entry
->d_name
[1] == '\0' ||
1560 entry
->d_name
[1] == '.'))
1563 if (entry
->d_name
[0] == '.')
1567 if (filter
== NULL
) {
1569 } else if (filter
!= NULL
) {
1570 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1583 struct dirent
*entry
;
1585 int filtercount
= 0;
1586 size_t oldc
= pane
->dirc
;
1588 width
= (twidth
/ 2) - 4;
1591 dir
= opendir(pane
->dirn
);
1595 /* get content and filter sum */
1596 while ((entry
= readdir(dir
)) != 0) {
1597 if (pane
->filter
!= NULL
) {
1598 if (strcasestr(entry
->d_name
, pane
->filter
) != NULL
)
1600 } else { /* no filter */
1605 if (pane
->filter
== NULL
) {
1610 if (pane
->filter
!= NULL
) {
1611 if (filtercount
> 0) {
1612 pane
->dirc
= filtercount
;
1615 } else if (filtercount
== 0) {
1616 if (closedir(dir
) < 0)
1623 /* print current directory title */
1624 pane
->dircol
.fg
|= TB_BOLD
;
1625 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", width
, pane
->dirn
);
1627 if (pane
->filter
== NULL
) /* dont't watch when filtering */
1628 if (addwatch(pane
) < 0)
1629 print_error("can't add watch");
1631 /* empty directory */
1632 if (pane
->dirc
== 0) {
1634 if (closedir(dir
) < 0)
1639 rewinddir(dir
); /* reset position */
1641 pane
, entry
, dir
, pane
->filter
); /* create array of entries */
1642 qsort(pane
->direntr
, pane
->dirc
, sizeof(Entry
), sort_name
);
1645 if (pane
->hdir
> pane
->dirc
)
1646 pane
->hdir
= pane
->dirc
;
1648 if (pane
== cpane
&& pane
->dirc
> 0)
1649 add_hi(pane
, pane
->hdir
- 1);
1651 if (closedir(dir
) < 0)
1661 panes
[Right
].dirx
= (twidth
/ 2) + 2;
1662 refresh_pane(&panes
[Left
]);
1663 refresh_pane(&panes
[Right
]);
1664 if (cpane
->dirc
> 0)
1665 add_hi(cpane
, cpane
->hdir
- 1);
1672 editor
[0] = getenv("EDITOR");
1675 if (editor
[0] == NULL
)
1685 home
= getenv("HOME");
1688 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1689 strncpy(cwd
, home
, MAX_P
);
1691 pane_idx
= Left
; /* cursor pane */
1692 cpane
= &panes
[pane_idx
];
1694 panes
[Left
].ex
= (twidth
/ 2) - 1;
1695 panes
[Left
].pane_id
= 0;
1696 panes
[Left
].dirx
= 2;
1697 panes
[Left
].dircol
= cpanell
;
1698 panes
[Left
].firstrow
= 0;
1699 panes
[Left
].direntr
= ecalloc(0, sizeof(Entry
));
1700 strncpy(panes
[Left
].dirn
, cwd
, MAX_P
);
1701 panes
[Left
].hdir
= 1;
1702 panes
[Left
].inotify_wd
= -1;
1703 panes
[Left
].parent_row
= 1;
1705 panes
[Right
].ex
= twidth
- 1;
1706 panes
[Right
].pane_id
= 1;
1707 panes
[Right
].dirx
= (twidth
/ 2) + 2;
1708 panes
[Right
].dircol
= cpanelr
;
1709 panes
[Right
].firstrow
= 0;
1710 panes
[Right
].direntr
= ecalloc(0, sizeof(Entry
));
1711 strncpy(panes
[Right
].dirn
, home
, MAX_P
);
1712 panes
[Right
].hdir
= 1;
1713 panes
[Right
].inotify_wd
= -1;
1714 panes
[Right
].parent_row
= 1;
1721 theight
= tb_height();
1722 twidth
= tb_width();
1723 scrheight
= theight
- 2;
1725 /* 2 horizontal lines */
1726 for (i
= 1; i
< twidth
- 1; ++i
) {
1727 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1728 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1731 /* 4 vertical lines */
1732 for (i
= 1; i
< theight
- 1; ++i
) {
1733 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1735 (twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
, cframe
.bg
);
1736 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1738 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1742 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1743 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1744 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1745 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1747 /* 2 middel top and bottom */
1748 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1750 (twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1757 PERROR(listdir(&panes
[Left
]));
1758 PERROR(listdir(&panes
[Right
]));
1765 struct sigaction sa
;
1767 main_pid
= getpid();
1768 sa
.sa_handler
= th_handler
;
1769 sigemptyset(&sa
.sa_mask
);
1770 sa
.sa_flags
= SA_RESTART
;
1771 return sigaction(SIGUSR1
, &sa
, NULL
);
1777 FAIL_IF(tb_init() != 0, "tb_init()");
1778 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1779 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1780 die("output error");
1784 PERROR(start_signal() < 0);
1785 PERROR(fsev_init() < 0);
1786 PERROR(listdir(&panes
[Left
]) < 0);
1787 PERROR(listdir(&panes
[Right
]) < 0);
1790 pthread_create(&fsev_thread
, NULL
, read_th
, NULL
);
1795 main(int argc
, char *argv
[])
1797 #if defined(__OpenBSD__)
1798 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1801 #endif /* __OpenBSD__ */
1804 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1805 die("sfm-" VERSION
);
1807 die("usage: sfm [-v]");