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
78 int parent_row
; // FIX
92 uint16_t key
; /* one of the TB_KEY_* constants */
93 uint32_t ch
; /* unicode character */
103 void (*func
)(const Arg
*);
107 /* function declarations */
108 static void print_tb(const char *, int, int, uint16_t, uint16_t);
109 static void printf_tb(int, int, Cpair
, const char *, ...);
110 static void print_status(Cpair
, const char *, ...);
111 static void print_xstatus(char, int);
112 static void print_error(char *);
113 static void print_prompt(char *);
114 static void print_info(Pane
*, char *);
115 static void print_row(Pane
*, size_t, Cpair
);
116 static void clear(int, int, int, uint16_t);
117 static void clear_status(void);
118 static void clear_pane(Pane
*);
119 static void add_hi(Pane
*, size_t);
120 static void rm_hi(Pane
*, size_t);
121 static int check_dir(char *);
122 static mode_t
chech_execf(mode_t
);
123 static int sort_name(const void *const, const void *const);
124 static void get_dirp(char *);
125 static char *get_ext(char *);
126 static int get_fdt(char *, time_t);
127 static char *get_fgrp(gid_t
);
128 static char *get_fperm(mode_t
);
129 static char *get_fsize(off_t
);
130 static char *get_fullpath(char *, char *);
131 static char *get_fusr(uid_t
);
132 static void get_dirsize(char *, off_t
*);
133 static void get_hicol(Cpair
*, mode_t
);
134 static void delent(const Arg
*arg
);
135 static void calcdir(const Arg
*arg
);
136 static void crnd(const Arg
*arg
);
137 static void crnf(const Arg
*arg
);
138 static void mv_ver(const Arg
*arg
);
139 static void mvbk(const Arg
*arg
);
140 static void mvbtm(const Arg
*arg
);
141 static void mvfwd(const Arg
*arg
);
142 static void mvmid(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 pane_r
, pane_l
, *cpane
;
187 static char *editor
[2];
188 static char fed
[] = "vi";
189 static int theight
, twidth
, scrheight
;
190 static int *sel_indexes
;
191 static size_t sel_len
= 0;
192 static char **sel_files
;
193 static int cont_vmode
= 0;
195 #if defined(_SYS_INOTIFY_H)
197 static int inotify_fd
;
198 #elif defined(_SYS_EVENT_H_)
201 struct kevent evlist
[2]; /* events we want to monitor */
202 struct kevent chlist
[2]; /* events that were triggered */
203 static struct timespec gtimeout
;
205 #if defined(__linux__) || defined(__FreeBSD__)
207 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
211 /* configuration, allows nested code to access above variables */
214 /* function implementations */
216 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
218 while (*str
!= '\0') {
220 str
+= tb_utf8_char_to_unicode(&uni
, str
);
221 tb_change_cell(x
, y
, uni
, fg
, bg
);
227 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
232 (void)vsnprintf(buf
, MAX_LINE
, fmt
, vl
);
234 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
238 print_status(Cpair col
, const char *fmt
, ...)
240 char buf
[MAX_STATUS
];
243 (void)vsnprintf(buf
, MAX_STATUS
, fmt
, vl
);
246 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
250 print_xstatus(char c
, int x
)
253 (void)tb_utf8_char_to_unicode(&uni
, &c
);
254 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
258 print_error(char *errmsg
)
260 print_status(cerr
, errmsg
);
264 print_prompt(char *prompt
)
266 print_status(cprompt
, prompt
);
270 print_info(Pane
*pane
, char *dirsize
)
272 char *sz
, *ur
, *gr
, *dt
, *prm
;
274 dt
= ecalloc(MAX_DTF
, sizeof(char));
276 prm
= get_fperm(CURSOR(pane
).mode
);
277 ur
= get_fusr(CURSOR(pane
).user
);
278 gr
= get_fgrp(CURSOR(pane
).group
);
280 if (get_fdt(dt
, CURSOR(pane
).dt
) < 0)
283 if (S_ISREG(CURSOR(pane
).mode
)) {
284 sz
= get_fsize(CURSOR(pane
).size
);
286 if (dirsize
== NULL
) {
287 sz
= ecalloc(1, sizeof(char));
294 print_status(cstatus
, "%02d/%02d %s %s:%s %s %s", pane
->hdir
,
295 pane
->dirc
, prm
, ur
, gr
, dt
, sz
);
305 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
310 char lnk_full
[MAX_P
];
313 width
= (twidth
/ 2) - 4;
314 result
= basename(pane
->direntr
[entpos
].name
);
316 y
= entpos
- pane
->firstrow
+ 1;
318 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
319 if (realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
321 lnk_full
, MAX_N
, "%s -> %s", result
, buf
);
326 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
330 clear(int sx
, int ex
, int y
, uint16_t bg
)
332 /* clear line from to */
333 /* x = line number vertical */
334 /* y = column number horizontal */
336 for (i
= sx
; i
< ex
; i
++) {
337 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
344 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
348 clear_pane(Pane
*pane
)
351 y
= 0, i
= 0, ex
= 0;
354 ex
= (twidth
/ 2) - 1;
355 else if (pane
== &pane_r
)
358 while (i
< scrheight
) {
359 clear(pane
->dirx
, ex
, y
, TB_DEFAULT
);
365 for (y
= pane
->dirx
; y
< ex
; ++y
) {
366 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
371 add_hi(Pane
*pane
, size_t entpos
)
374 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
375 col
.fg
|= TB_REVERSE
| TB_BOLD
;
376 col
.bg
|= TB_REVERSE
;
377 print_row(pane
, entpos
, col
);
381 rm_hi(Pane
*pane
, size_t entpos
)
384 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
385 print_row(pane
, entpos
, col
);
389 check_dir(char *path
)
395 if (errno
== ENOTDIR
) {
402 if (closedir(dir
) < 0)
409 chech_execf(mode_t mode
)
412 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
417 sort_name(const void *const A
, const void *const B
)
420 mode_t data1
= (*(Entry
*)A
).mode
;
421 mode_t data2
= (*(Entry
*)B
).mode
;
425 } else if (data1
== data2
) {
426 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
439 len
= strnlen(cdir
, MAX_P
);
443 for (i
= len
- 1; i
> 1; i
--) {
450 cdir
[len
- counter
- 1] = '\0';
458 size_t counter
, len
, i
;
462 len
= strnlen(str
, MAX_N
);
464 for (i
= len
- 1; i
> 0; i
--) {
472 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
473 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
479 get_fdt(char *result
, time_t status
)
482 localtime_r(&status
, <
);
483 return strftime(result
, MAX_DTF
, dtfmt
, <
);
487 get_fgrp(gid_t status
)
492 result
= ecalloc(MAX_GRPN
, sizeof(char));
493 gr
= getgrgid(status
);
495 (void)snprintf(result
, MAX_GRPN
, "%u", status
);
497 strncpy(result
, gr
->gr_name
, MAX_GRPN
);
499 result
[MAX_GRPN
- 1] = '\0';
504 get_fperm(mode_t mode
)
509 const char chars
[] = "rwxrwxrwx";
510 buf
= ecalloc(11, sizeof(char));
514 else if (S_ISREG(mode
))
516 else if (S_ISLNK(mode
))
518 else if (S_ISBLK(mode
))
520 else if (S_ISCHR(mode
))
522 else if (S_ISFIFO(mode
))
524 else if (S_ISSOCK(mode
))
529 for (i
= 1; i
< 10; i
++) {
530 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
538 get_fsize(off_t size
)
540 char *result
; /* need to be freed */
546 result_len
= 6; /* 9999X/0 */
547 result
= ecalloc(result_len
, sizeof(char));
549 while (size
>= 1000) {
574 if (snprintf(result
, result_len
, OFF_T
"%c", size
, unit
) < 0)
575 strncat(result
, "???", result_len
);
581 get_fullpath(char *first
, char *second
)
585 full_path
= ecalloc(MAX_P
, sizeof(char));
587 if (strncmp(first
, "/", MAX_P
) == 0)
588 (void)snprintf(full_path
, MAX_P
, "/%s", second
);
590 (void)snprintf(full_path
, MAX_P
, "%s/%s", first
, second
);
596 get_fusr(uid_t status
)
601 result
= ecalloc(MAX_USRN
, sizeof(char));
602 pw
= getpwuid(status
);
604 (void)snprintf(result
, MAX_USRN
, "%u", status
);
606 strncpy(result
, pw
->pw_name
, MAX_USRN
);
608 result
[MAX_USRN
- 1] = '\0';
613 get_dirsize(char *fullpath
, off_t
*fullsize
)
618 struct dirent
*entry
;
621 dir
= opendir(fullpath
);
626 while ((entry
= readdir(dir
)) != 0) {
627 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
628 strncmp(entry
->d_name
, "..", 3) == 0))
631 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
632 if (lstat(ent_full
, &status
) == 0) {
633 mode
= status
.st_mode
;
635 get_dirsize(ent_full
, fullsize
);
638 *fullsize
+= status
.st_size
;
649 get_hicol(Cpair
*col
, mode_t mode
)
654 else if (S_ISLNK(mode
))
656 else if (chech_execf(mode
) > 0)
661 delent(const Arg
*arg
)
669 inp_conf
= ecalloc(conf_len
, sizeof(char));
670 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
671 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
673 return; /* canceled by user or wrong inp_conf */
678 tmp
[0] = CURSOR(cpane
).name
;
679 if (spawn(rm_cmd
, rm_cmd_len
, tmp
, 1, NULL
) < 0) {
680 print_error(strerror(errno
));
686 calcdir(const Arg
*arg
)
690 if (!S_ISDIR(CURSOR(cpane
).mode
))
696 fullsize
= ecalloc(1, sizeof(off_t
));
697 get_dirsize(CURSOR(cpane
).name
, fullsize
);
698 csize
= get_fsize(*fullsize
);
700 CURSOR(cpane
).size
= *fullsize
;
701 print_info(cpane
, csize
);
708 char *user_input
, *path
;
710 user_input
= ecalloc(MAX_USRI
, sizeof(char));
711 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
716 path
= ecalloc(MAX_P
, sizeof(char));
717 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
723 if (mkdir(path
, ndir_perm
) < 0)
724 print_error(strerror(errno
));
733 char *user_input
, *path
;
736 user_input
= ecalloc(MAX_USRI
, sizeof(char));
737 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
742 path
= ecalloc(MAX_P
, sizeof(char));
743 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
749 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
752 print_error(strerror(errno
));
753 else if (close(rf
) < 0)
754 print_error(strerror(errno
));
760 mv_ver(const Arg
*arg
)
765 if (cpane
->hdir
- cpane
->firstrow
- arg
->i
< 1) { /* move to the top */
770 if (cpane
->hdir
- arg
->i
> cpane
->dirc
|| /* move to the bottom */
771 cpane
->hdir
- cpane
->firstrow
- arg
->i
> scrheight
- 1) {
777 if (cpane
->firstrow
> 1 && arg
->i
> 0 &&
778 cpane
->hdir
< (cpane
->firstrow
+ arg
->i
)) { /* scroll up */
779 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
780 rm_hi(cpane
, cpane
->hdir
- 1);
781 cpane
->hdir
= cpane
->hdir
- arg
->i
;
783 add_hi(cpane
, cpane
->hdir
- 1);
787 if (cpane
->hdir
- cpane
->firstrow
>= scrheight
- 1 &&
788 arg
->i
< 0) { /* scroll down */
789 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
790 rm_hi(cpane
, cpane
->hdir
- 1);
791 cpane
->hdir
= cpane
->hdir
- arg
->i
;
793 add_hi(cpane
, cpane
->hdir
- 1);
797 rm_hi(cpane
, cpane
->hdir
- 1);
798 cpane
->hdir
= cpane
->hdir
- arg
->i
;
799 add_hi(cpane
, cpane
->hdir
- 1);
800 print_info(cpane
, NULL
);
806 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
810 get_dirp(cpane
->dirn
);
811 if (check_dir(cpane
->dirn
) < 0) {
812 print_error(strerror(errno
));
817 cpane
->firstrow
= cpane
->parent_firstrow
;
818 cpane
->hdir
= cpane
->parent_row
;
819 if (listdir(cpane
) < 0)
820 print_error(strerror(errno
));
821 cpane
->parent_firstrow
= 0;
822 cpane
->parent_row
= 1;
826 mvbtm(const Arg
*arg
)
830 if (cpane
->dirc
> scrheight
) {
831 rm_hi(cpane
, cpane
->hdir
- 1);
832 cpane
->hdir
= cpane
->dirc
;
833 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
835 add_hi(cpane
, cpane
->hdir
- 1);
837 rm_hi(cpane
, cpane
->hdir
- 1);
838 cpane
->hdir
= cpane
->dirc
;
839 add_hi(cpane
, cpane
->hdir
- 1);
841 print_info(cpane
, NULL
);
845 mvfwd(const Arg
*arg
)
851 switch (check_dir(CURSOR(cpane
).name
)) {
853 strncpy(cpane
->dirn
, CURSOR(cpane
).name
, MAX_P
);
854 cpane
->parent_row
= cpane
->hdir
;
855 cpane
->parent_firstrow
= cpane
->firstrow
;
859 if (listdir(cpane
) < 0)
860 print_error(strerror(errno
));
862 case 1: /* not a directory open file */
865 s
= opnf(CURSOR(cpane
).name
);
870 print_error("process failed non-zero exit");
872 case -1: /* failed to open directory */
873 print_error(strerror(errno
));
878 mvmid(const Arg
*arg
)
882 rm_hi(cpane
, cpane
->hdir
- 1);
883 if (cpane
->dirc
< scrheight
/ 2)
884 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
886 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
887 add_hi(cpane
, cpane
->hdir
- 1);
888 print_info(cpane
, NULL
);
892 mvtop(const Arg
*arg
)
896 if (cpane
->dirc
> scrheight
) {
897 rm_hi(cpane
, cpane
->hdir
- 1);
901 add_hi(cpane
, cpane
->hdir
- 1);
903 rm_hi(cpane
, cpane
->hdir
- 1);
905 add_hi(cpane
, cpane
->hdir
- 1);
906 print_info(cpane
, NULL
);
911 bkmrk(const Arg
*arg
)
913 if (check_dir((char *)arg
->v
) != 0) {
914 print_error(strerror(errno
));
919 strncpy(cpane
->dirn
, (char *)arg
->v
, MAX_P
);
921 cpane
->parent_row
= 1;
923 if (listdir(cpane
) < 0)
924 print_error(strerror(errno
));
928 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
930 int height
= tb_height();
933 size_t counter
= (size_t)1;
945 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
947 print_tb(buf
, 1, height
- 1, col
.fg
, col
.bg
);
948 startat
= name_size
+ 1;
949 tb_set_cursor((int)(startat
+ 1), height
- 1);
952 while (tb_poll_event(&fev
) != 0) {
955 if (fev
.key
== TB_KEY_ESC
) {
956 tb_set_cursor(-1, -1);
961 if (fev
.key
== TB_KEY_BACKSPACE
||
962 fev
.key
== TB_KEY_BACKSPACE2
) {
963 if (BETWEEN(counter
, 2, sout
)) {
967 print_xstatus(empty
, startat
+ counter
);
969 startat
+ counter
, theight
- 1);
972 } else if (fev
.key
== TB_KEY_ENTER
) {
973 tb_set_cursor(-1, -1);
974 out
[counter
- 1] = '\0';
978 if (counter
< sout
) {
979 print_xstatus((char)fev
.ch
,
980 (startat
+ counter
));
981 out
[x
] = (char)fev
.ch
;
982 tb_set_cursor((startat
+ counter
+ 1),
1005 for (c
= 0; c
< LEN(rules
); c
++)
1006 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1007 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1013 spawn(const void *com_argv
, size_t com_argc
, const void *f_argv
, size_t f_argc
,
1020 argc
= com_argc
+ f_argc
+ 2;
1023 memcpy(argv
, com_argv
, com_argc
* sizeof(char *)); /* command */
1024 memcpy(&argv
[com_argc
], f_argv
, f_argc
* sizeof(char *)); /* files */
1026 argv
[argc
- 2] = fn
;
1027 argv
[argc
- 1] = NULL
;
1034 execvp(argv
[0], argv
);
1037 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1041 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1057 if (c
< 0) /* extension not found open in editor */
1058 return spawn(editor
, 1, NULL
, 0, fn
);
1060 return spawn((char **)rules
[c
].v
, rules
[c
].vlen
, NULL
, 0, fn
);
1066 #if defined(_SYS_INOTIFY_H)
1067 inotify_fd
= inotify_init();
1070 #elif defined(_SYS_EVENT_H_)
1071 gtimeout
.tv_sec
= 1;
1080 addwatch(Pane
*pane
)
1082 #if defined(_SYS_INOTIFY_H)
1083 return pane
->inotify_wd
= inotify_add_watch(inotify_fd
, pane
->dirn
,
1084 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1085 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1086 #elif defined(_SYS_EVENT_H_)
1087 pane
->event_fd
= open(pane
->dirn
, O_RDONLY
);
1088 if (pane
->event_fd
< 0)
1089 return pane
->event_fd
;
1090 EV_SET(&evlist
[pane
->pane_id
], pane
->event_fd
, EVFILT_VNODE
,
1092 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
| NOTE_RENAME
|
1093 NOTE_REVOKE
| NOTE_WRITE
,
1102 #if defined(_SYS_INOTIFY_H)
1105 struct inotify_event
*event
;
1106 const size_t events
= 32;
1107 const size_t evbuflen
=
1108 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1111 if (cpane
->inotify_wd
< 0)
1113 r
= read(inotify_fd
, buf
, evbuflen
);
1117 for (p
= buf
; p
< buf
+ r
;) {
1118 event
= (struct inotify_event
*)p
;
1125 p
+= sizeof(struct inotify_event
) + event
->len
;
1127 #elif defined(_SYS_EVENT_H_)
1128 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1136 #if defined(_SYS_INOTIFY_H)
1137 if (pane
->inotify_wd
>= 0)
1138 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1139 #elif defined(_SYS_EVENT_H_)
1140 close(pane
->event_fd
);
1147 pthread_cancel(fsev_thread
);
1148 #if defined(__linux__)
1149 pthread_join(fsev_thread
, NULL
);
1153 #if defined(_SYS_INOTIFY_H)
1155 #elif defined(_SYS_EVENT_H_)
1161 toggle_df(const Arg
*arg
)
1163 show_dotfiles
= !show_dotfiles
;
1164 if (cpane
== &pane_l
) {
1165 if (listdir(&pane_r
) < 0)
1166 print_error(strerror(errno
));
1167 if (listdir(&pane_l
) < 0)
1168 print_error(strerror(errno
));
1169 } else if (cpane
== &pane_r
) {
1170 if (listdir(&pane_l
) < 0)
1171 print_error(strerror(errno
));
1172 if (listdir(&pane_r
) < 0)
1173 print_error(strerror(errno
));
1178 start_filter(const Arg
*arg
)
1180 if (cpane
->dirc
< 1)
1183 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1184 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1188 cpane
->filter
= user_input
;
1189 if (listdir(cpane
) < 0)
1190 print_error("no match");
1191 cpane
->filter
= NULL
;
1196 start_vmode(const Arg
*arg
)
1198 if (cpane
->dirc
< 1)
1200 struct tb_event fev
;
1201 if (sel_indexes
!= NULL
) {
1206 sel_indexes
= ecalloc(cpane
->dirc
, sizeof(size_t));
1207 sel_indexes
[0] = cpane
->hdir
;
1209 print_prompt("-- VISUAL --");
1211 while (tb_poll_event(&fev
) != 0) {
1214 grabkeys(&fev
, vkeys
, vkeyslen
);
1215 if (cont_vmode
== -1)
1224 exit_vmode(const Arg
*arg
)
1226 refresh_pane(cpane
);
1227 add_hi(cpane
, cpane
->hdir
- 1);
1232 selup(const Arg
*arg
)
1235 print_prompt("-- VISUAL --");
1236 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1238 if (cpane
->hdir
< sel_indexes
[0]) {
1239 sel_indexes
[index
] = cpane
->hdir
;
1240 add_hi(cpane
, sel_indexes
[index
]);
1241 } else if (index
< cpane
->dirc
) {
1242 sel_indexes
[index
+ 1] = 0;
1244 if (cpane
->dirc
>= scrheight
||
1245 cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1251 seldwn(const Arg
*arg
)
1254 print_prompt("-- VISUAL --");
1255 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1257 if (cpane
->hdir
> sel_indexes
[0]) {
1258 sel_indexes
[index
] = cpane
->hdir
;
1259 add_hi(cpane
, sel_indexes
[index
] - 2);
1261 sel_indexes
[index
+ 1] = 0;
1263 if (cpane
->dirc
>= scrheight
||
1264 cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1270 selall(const Arg
*arg
)
1273 for (i
= 0; i
< cpane
->dirc
; i
++) {
1274 sel_indexes
[i
] = i
+ 1;
1283 for (i
= 0; i
< cpane
->dirc
; i
++) {
1284 if (sel_indexes
[i
] < (scrheight
+ cpane
->firstrow
) &&
1286 cpane
->firstrow
) { /* checks if in the frame of the directories */
1287 add_hi(cpane
, sel_indexes
[i
] - 1);
1298 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1299 if (sel_indexes
[j
] != 0)
1311 if (sel_files
!= NULL
) {
1312 for (i
= 0; i
< sel_len
; i
++) {
1314 sel_files
[i
] = NULL
;
1328 sel_files
= ecalloc(sel_len
, sizeof(char *));
1330 for (i
= 0; i
< sel_len
; i
++) {
1331 sel_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1332 strncpy(sel_files
[i
], cpane
->direntr
[sel_indexes
[i
] - 1].name
,
1338 selynk(const Arg
*arg
)
1341 refresh_pane(cpane
);
1342 add_hi(cpane
, cpane
->hdir
- 1);
1343 print_status(cprompt
, "%zu files are yanked", sel_len
);
1348 seldel(const Arg
*arg
)
1352 inp_conf
= ecalloc(delconf_len
, sizeof(char));
1353 if ((get_usrinput(inp_conf
, delconf_len
, "delete file (yes) ?") < 0) ||
1354 (strncmp(inp_conf
, delconf
, delconf_len
) != 0)) {
1356 return; /* canceled by user or wrong inp_conf */
1362 if (spawn(rm_cmd
, rm_cmd_len
, sel_files
, sel_len
, NULL
) < 0)
1363 print_error(strerror(errno
));
1365 print_status(cprompt
, "%zu files are deleted", sel_len
);
1372 paste(const Arg
*arg
)
1374 if (sel_files
== NULL
) {
1375 print_error("nothing to paste");
1379 if (spawn(cp_cmd
, cp_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1380 print_error(strerror(errno
));
1382 print_status(cprompt
, "%zu files are copied", sel_len
);
1388 selmv(const Arg
*arg
)
1390 if (sel_files
== NULL
) {
1391 print_error("nothing to move");
1395 if (spawn(mv_cmd
, mv_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1396 print_error(strerror(errno
));
1398 print_status(cprompt
, "%zu files are moved", sel_len
);
1404 rname(const Arg
*arg
)
1406 if (cpane
->dirc
< 1)
1408 char new_name
[MAX_P
];
1411 input_name
= ecalloc(MAX_N
, sizeof(char));
1413 if (get_usrinput(input_name
, MAX_N
, "rename: %s",
1414 basename(CURSOR(cpane
).name
)) < 0) {
1419 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1421 print_error(strerror(errno
));
1425 char *rename_cmd
[] = { "mv", CURSOR(cpane
).name
, new_name
};
1426 if (spawn(rename_cmd
, 3, NULL
, 0, NULL
) < 0)
1427 print_error(strerror(errno
));
1433 yank(const Arg
*arg
)
1435 if (cpane
->dirc
< 1)
1440 sel_files
= ecalloc(sel_len
, sizeof(char *));
1441 sel_files
[0] = ecalloc(MAX_P
, sizeof(char));
1442 strncpy(sel_files
[0], CURSOR(cpane
).name
, MAX_P
);
1443 print_status(cprompt
, "1 file is yanked", sel_len
);
1447 switch_pane(const Arg
*arg
)
1449 if (cpane
->dirc
> 0)
1450 rm_hi(cpane
, cpane
->hdir
- 1);
1451 if (cpane
== &pane_l
)
1453 else if (cpane
== &pane_r
)
1455 if (cpane
->dirc
> 0) {
1456 add_hi(cpane
, cpane
->hdir
- 1);
1457 print_info(cpane
, NULL
);
1464 quit(const Arg
*arg
)
1466 if (cont_vmode
== -1) { /* check if selection was allocated */
1468 if (sel_files
!= NULL
)
1471 free(pane_l
.direntr
);
1472 free(pane_r
.direntr
);
1479 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1483 for (i
= 0; i
< max_keys
; i
++) {
1484 if (event
->ch
!= 0) {
1485 if (event
->ch
== key
[i
].evkey
.ch
) {
1486 key
[i
].func(&key
[i
].arg
);
1489 } else if (event
->key
!= 0) {
1490 if (event
->key
== key
[i
].evkey
.key
) {
1491 key
[i
].func(&key
[i
].arg
);
1501 struct timespec tim
;
1503 tim
.tv_nsec
= 5000000L; /* 0.005 sec */
1506 if (read_events() > READEVSZ
) {
1507 kill(main_pid
, SIGUSR1
);
1508 nanosleep(&tim
, NULL
);
1518 while (tb_poll_event(&ev
) != 0) {
1521 grabkeys(&ev
, nkeys
, nkeyslen
);
1524 case TB_EVENT_RESIZE
:
1535 refresh_pane(Pane
*pane
)
1537 size_t y
, dyn_max
, start_from
;
1539 width
= (twidth
/ 2) - 4;
1543 start_from
= pane
->firstrow
;
1544 dyn_max
= MIN(pane
->dirc
, (scrheight
- 1) + pane
->firstrow
);
1546 /* print each entry in directory */
1547 while (start_from
< dyn_max
) {
1548 get_hicol(&col
, pane
->direntr
[start_from
].mode
);
1549 print_row(pane
, start_from
, col
);
1555 print_info(pane
, NULL
);
1559 /* print current directory title */
1560 pane
->dircol
.fg
|= TB_BOLD
;
1561 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", width
, pane
->dirn
);
1565 set_direntr(Pane
*pane
, struct dirent
*entry
, DIR *dir
, char *filter
)
1572 tmpfull = get_fullpath(pane->dirn, entry->d_name); \
1573 strncpy(pane->direntr[i].name, tmpfull, MAX_N); \
1574 if (lstat(tmpfull, &status) == 0) { \
1575 pane->direntr[i].size = status.st_size; \
1576 pane->direntr[i].mode = status.st_mode; \
1577 pane->direntr[i].group = status.st_gid; \
1578 pane->direntr[i].user = status.st_uid; \
1579 pane->direntr[i].dt = status.st_mtime; \
1586 erealloc(pane
->direntr
, (10 + pane
->dirc
) * sizeof(Entry
));
1587 while ((entry
= readdir(dir
)) != 0) {
1588 if (show_dotfiles
== 1) {
1589 if (entry
->d_name
[0] == '.' &&
1590 (entry
->d_name
[1] == '\0' || entry
->d_name
[1] == '.'))
1593 if (entry
->d_name
[0] == '.')
1598 if (filter
== NULL
) {
1600 } else if (filter
!= NULL
) {
1601 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1614 struct dirent
*entry
;
1616 int filtercount
= 0;
1617 size_t oldc
= pane
->dirc
;
1619 width
= (twidth
/ 2) - 4;
1622 dir
= opendir(pane
->dirn
);
1626 /* get content and filter sum */
1627 while ((entry
= readdir(dir
)) != 0) {
1628 if (pane
->filter
!= NULL
) {
1629 if (strcasestr(entry
->d_name
, pane
->filter
) != NULL
)
1631 } else { /* no filter */
1636 if (pane
->filter
== NULL
) {
1641 if (pane
->filter
!= NULL
) {
1642 if (filtercount
> 0) {
1643 pane
->dirc
= filtercount
;
1646 } else if (filtercount
== 0) {
1647 if (closedir(dir
) < 0)
1654 /* print current directory title */
1655 pane
->dircol
.fg
|= TB_BOLD
;
1656 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", width
, pane
->dirn
);
1658 if (pane
->filter
== NULL
) /* dont't watch when filtering */
1659 if (addwatch(pane
) < 0)
1660 print_error("can't add watch");
1662 /* empty directory */
1663 if (pane
->dirc
== 0) {
1665 if (closedir(dir
) < 0)
1670 rewinddir(dir
); /* reset position */
1672 pane
, entry
, dir
, pane
->filter
); /* create array of entries */
1673 qsort(pane
->direntr
, pane
->dirc
, sizeof(Entry
), sort_name
);
1676 if (pane
->hdir
> pane
->dirc
)
1677 pane
->hdir
= pane
->dirc
;
1679 if (pane
== cpane
&& pane
->dirc
> 0)
1680 add_hi(pane
, pane
->hdir
- 1);
1682 if (closedir(dir
) < 0)
1690 /* TODO need refactoring */
1693 pane_r
.dirx
= (twidth
/ 2) + 2;
1695 if (cpane
== &pane_l
) {
1696 refresh_pane(&pane_r
);
1697 refresh_pane(&pane_l
);
1698 if (cpane
->dirc
> 0)
1699 add_hi(&pane_l
, pane_l
.hdir
- 1);
1700 } else if (cpane
== &pane_r
) {
1701 refresh_pane(&pane_l
);
1702 refresh_pane(&pane_r
);
1703 if (cpane
->dirc
> 0)
1704 add_hi(&pane_r
, pane_r
.hdir
- 1);
1713 editor
[0] = getenv("EDITOR");
1716 if (editor
[0] == NULL
)
1726 home
= getenv("HOME");
1729 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1730 strncpy(cwd
, home
, MAX_P
);
1734 pane_l
.dircol
= cpanell
;
1735 pane_l
.firstrow
= 0;
1736 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1737 strncpy(pane_l
.dirn
, cwd
, MAX_P
);
1739 pane_l
.inotify_wd
= -1;
1740 pane_l
.parent_row
= 1;
1743 pane_r
.dirx
= (twidth
/ 2) + 2;
1744 pane_r
.dircol
= cpanelr
;
1745 pane_r
.firstrow
= 0;
1746 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1747 strncpy(pane_r
.dirn
, home
, MAX_P
);
1749 pane_r
.inotify_wd
= -1;
1750 pane_r
.parent_row
= 1;
1757 theight
= tb_height();
1758 twidth
= tb_width();
1759 scrheight
= theight
- 2;
1761 /* 2 horizontal lines */
1762 for (i
= 1; i
< twidth
- 1; ++i
) {
1763 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1764 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1767 /* 4 vertical lines */
1768 for (i
= 1; i
< theight
- 1; ++i
) {
1769 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1771 (twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
, cframe
.bg
);
1772 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1774 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1778 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1779 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1780 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1781 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1783 /* 2 middel top and bottom */
1784 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1786 (twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1793 if (cpane
== &pane_l
) {
1794 if (listdir(&pane_r
) < 0)
1795 print_error(strerror(errno
));
1796 if (listdir(&pane_l
) < 0)
1797 print_error(strerror(errno
));
1798 } else if (cpane
== &pane_r
) {
1799 if (listdir(&pane_l
) < 0)
1800 print_error(strerror(errno
));
1801 if (listdir(&pane_r
) < 0)
1802 print_error(strerror(errno
));
1810 struct sigaction sa
;
1812 main_pid
= getpid();
1813 sa
.sa_handler
= th_handler
;
1814 sigemptyset(&sa
.sa_mask
);
1815 sa
.sa_flags
= SA_RESTART
;
1816 return sigaction(SIGUSR1
, &sa
, NULL
);
1824 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1825 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1826 die("output error");
1831 if (start_signal() < 0)
1832 print_error(strerror(errno
));
1833 if (fsev_init() < 0)
1834 print_error(strerror(errno
));
1836 if (listdir(&pane_r
) < 0)
1837 print_error(strerror(errno
));
1838 if (listdir(&pane_l
) < 0)
1839 print_error(strerror(errno
));
1842 pthread_create(&fsev_thread
, NULL
, read_th
, NULL
);
1847 main(int argc
, char *argv
[])
1849 #if defined(__OpenBSD__)
1850 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1853 #endif /* __OpenBSD__ */
1856 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1857 die("sfm-" VERSION
);
1859 die("usage: sfm [-v]");