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
, hwidth
, 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
)
312 char rez_pth
[MAX_N
- 5];
313 char lnk_full
[MAX_N
];
315 full_str
= basename(pane
->direntr
[entpos
].name
);
317 y
= entpos
- pane
->firstrow
+ 1;
319 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
320 if (realpath(pane
->direntr
[entpos
].name
, rez_pth
) != NULL
) {
322 lnk_full
, MAX_N
, "%s -> %s", full_str
, rez_pth
);
327 printf_tb(x
, y
, col
, "%*.*s", ~hwidth
, hwidth
, full_str
);
331 clear(int sx
, int ex
, int y
, uint16_t bg
)
333 /* clear line from to */
334 /* x = line number vertical */
335 /* y = column number horizontal */
337 for (i
= sx
; i
< ex
; i
++) {
338 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
345 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
349 clear_pane(Pane
*pane
)
354 while (i
< scrheight
) {
355 clear(pane
->dirx
, pane
->ex
, y
, TB_DEFAULT
);
361 for (y
= pane
->dirx
; y
< pane
->ex
; ++y
) {
362 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
367 add_hi(Pane
*pane
, size_t entpos
)
370 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
371 col
.fg
|= TB_REVERSE
| TB_BOLD
;
372 col
.bg
|= TB_REVERSE
;
373 print_row(pane
, entpos
, col
);
377 rm_hi(Pane
*pane
, size_t entpos
)
380 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
381 print_row(pane
, entpos
, col
);
385 check_dir(char *path
)
391 if (errno
== ENOTDIR
) {
398 if (closedir(dir
) < 0)
405 chech_execf(mode_t mode
)
408 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
413 sort_name(const void *const A
, const void *const B
)
416 mode_t data1
= (*(Entry
*)A
).mode
;
417 mode_t data2
= (*(Entry
*)B
).mode
;
421 } else if (data1
== data2
) {
422 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
435 len
= strnlen(cdir
, MAX_P
);
439 for (i
= len
- 1; i
> 1; i
--) {
446 cdir
[len
- counter
- 1] = '\0';
454 size_t counter
, len
, i
;
458 len
= strnlen(str
, MAX_N
);
460 for (i
= len
- 1; i
> 0; i
--) {
468 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
469 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
475 get_fdt(char *result
, time_t status
)
478 localtime_r(&status
, <
);
479 return strftime(result
, MAX_DTF
, dtfmt
, <
);
483 get_fgrp(gid_t status
)
488 result
= ecalloc(MAX_GRPN
, sizeof(char));
489 gr
= getgrgid(status
);
491 (void)snprintf(result
, MAX_GRPN
, "%u", status
);
493 strncpy(result
, gr
->gr_name
, MAX_GRPN
);
495 result
[MAX_GRPN
- 1] = '\0';
500 get_fperm(mode_t mode
)
505 const char chars
[] = "rwxrwxrwx";
506 buf
= ecalloc(11, sizeof(char));
510 else if (S_ISREG(mode
))
512 else if (S_ISLNK(mode
))
514 else if (S_ISBLK(mode
))
516 else if (S_ISCHR(mode
))
518 else if (S_ISFIFO(mode
))
520 else if (S_ISSOCK(mode
))
525 for (i
= 1; i
< 10; i
++) {
526 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
534 get_fsize(off_t size
)
536 char *result
; /* need to be freed */
542 result_len
= 6; /* 9999X/0 */
543 result
= ecalloc(result_len
, sizeof(char));
545 while (size
>= 1000) {
570 if (snprintf(result
, result_len
, OFF_T
"%c", size
, unit
) < 0)
571 strncat(result
, "???", result_len
);
577 get_fullpath(char *first
, char *second
)
581 full_path
= ecalloc(MAX_P
, sizeof(char));
583 if (strncmp(first
, "/", MAX_P
) == 0)
584 (void)snprintf(full_path
, MAX_P
, "/%s", second
);
586 (void)snprintf(full_path
, MAX_P
, "%s/%s", first
, second
);
592 get_fusr(uid_t status
)
597 result
= ecalloc(MAX_USRN
, sizeof(char));
598 pw
= getpwuid(status
);
600 (void)snprintf(result
, MAX_USRN
, "%u", status
);
602 strncpy(result
, pw
->pw_name
, MAX_USRN
);
604 result
[MAX_USRN
- 1] = '\0';
609 get_dirsize(char *fullpath
, off_t
*fullsize
)
614 struct dirent
*entry
;
617 dir
= opendir(fullpath
);
622 while ((entry
= readdir(dir
)) != 0) {
623 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
624 strncmp(entry
->d_name
, "..", 3) == 0))
627 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
628 if (lstat(ent_full
, &status
) == 0) {
629 mode
= status
.st_mode
;
631 get_dirsize(ent_full
, fullsize
);
634 *fullsize
+= status
.st_size
;
645 get_hicol(Cpair
*col
, mode_t mode
)
650 else if (S_ISLNK(mode
))
652 else if (chech_execf(mode
) > 0)
657 delent(const Arg
*arg
)
665 inp_conf
= ecalloc(conf_len
, sizeof(char));
666 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
667 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
669 return; /* canceled by user or wrong inp_conf */
674 tmp
[0] = CURSOR(cpane
).name
;
675 if (spawn(rm_cmd
, rm_cmd_len
, tmp
, 1, NULL
) < 0) {
676 print_error(strerror(errno
));
682 calcdir(const Arg
*arg
)
686 if (!S_ISDIR(CURSOR(cpane
).mode
))
692 fullsize
= ecalloc(1, sizeof(off_t
));
693 get_dirsize(CURSOR(cpane
).name
, fullsize
);
694 csize
= get_fsize(*fullsize
);
696 CURSOR(cpane
).size
= *fullsize
;
697 print_info(cpane
, csize
);
704 char *user_input
, *path
;
706 user_input
= ecalloc(MAX_USRI
, sizeof(char));
707 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
712 path
= ecalloc(MAX_P
, sizeof(char));
713 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
719 PERROR(mkdir(path
, ndir_perm
) < 0);
728 char *user_input
, *path
;
731 user_input
= ecalloc(MAX_USRI
, sizeof(char));
732 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
737 path
= ecalloc(MAX_P
, sizeof(char));
738 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
744 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
747 print_error(strerror(errno
));
748 else if (close(rf
) < 0)
749 print_error(strerror(errno
));
755 mv_ver(const Arg
*arg
)
760 if (cpane
->hdir
- arg
->i
< 1) /* first line */
763 if (cpane
->hdir
- arg
->i
> cpane
->dirc
) /* last line */
766 if (cpane
->firstrow
> 0 && arg
->i
> 0 &&
767 cpane
->hdir
<= (cpane
->firstrow
+ arg
->i
)) { /* scroll up */
768 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
769 rm_hi(cpane
, cpane
->hdir
- 1);
770 cpane
->hdir
= cpane
->hdir
- arg
->i
;
772 add_hi(cpane
, cpane
->hdir
- 1);
776 if (cpane
->hdir
- cpane
->firstrow
>= scrheight
+ arg
->i
&&
777 arg
->i
< 0) { /* scroll down */
778 cpane
->firstrow
= cpane
->firstrow
- arg
->i
;
779 rm_hi(cpane
, cpane
->hdir
- 1);
780 cpane
->hdir
= cpane
->hdir
- arg
->i
;
782 add_hi(cpane
, cpane
->hdir
- 1);
786 rm_hi(cpane
, cpane
->hdir
- 1);
787 cpane
->hdir
= cpane
->hdir
- arg
->i
;
788 add_hi(cpane
, cpane
->hdir
- 1);
789 print_info(cpane
, NULL
);
795 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
799 get_dirp(cpane
->dirn
);
800 if (check_dir(cpane
->dirn
) < 0) {
801 print_error(strerror(errno
));
806 cpane
->firstrow
= cpane
->parent_firstrow
;
807 cpane
->hdir
= cpane
->parent_row
;
808 PERROR(listdir(cpane
) < 0);
809 cpane
->parent_firstrow
= 0;
810 cpane
->parent_row
= 1;
814 mvbtm(const Arg
*arg
)
818 if (cpane
->dirc
> scrheight
) {
819 rm_hi(cpane
, cpane
->hdir
- 1);
820 cpane
->hdir
= cpane
->dirc
;
821 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
823 add_hi(cpane
, cpane
->hdir
- 1);
825 rm_hi(cpane
, cpane
->hdir
- 1);
826 cpane
->hdir
= cpane
->dirc
;
827 add_hi(cpane
, cpane
->hdir
- 1);
829 print_info(cpane
, NULL
);
833 mvfwd(const Arg
*arg
)
839 switch (check_dir(CURSOR(cpane
).name
)) {
841 strncpy(cpane
->dirn
, CURSOR(cpane
).name
, MAX_P
);
842 cpane
->parent_row
= cpane
->hdir
;
843 cpane
->parent_firstrow
= cpane
->firstrow
;
847 PERROR(listdir(cpane
) < 0);
849 case 1: /* not a directory open file */
852 s
= opnf(CURSOR(cpane
).name
);
857 print_error("process failed non-zero exit");
859 case -1: /* failed to open directory */
860 print_error(strerror(errno
));
865 mvtop(const Arg
*arg
)
869 if (cpane
->dirc
> scrheight
) {
870 rm_hi(cpane
, cpane
->hdir
- 1);
874 add_hi(cpane
, cpane
->hdir
- 1);
876 rm_hi(cpane
, cpane
->hdir
- 1);
878 add_hi(cpane
, cpane
->hdir
- 1);
879 print_info(cpane
, NULL
);
884 bkmrk(const Arg
*arg
)
886 if (check_dir((char *)arg
->v
) != 0) {
887 print_error(strerror(errno
));
892 strncpy(cpane
->dirn
, (char *)arg
->v
, MAX_P
);
894 cpane
->parent_row
= 1;
896 PERROR(listdir(cpane
) < 0);
900 get_usrinput(char *result
, size_t max_chars
, const char *fmt
, ...)
903 size_t i
, cpos
, startat
;
911 startat
= vsnprintf(msg
, MAX_N
, fmt
, vl
) + 1;
915 print_tb(msg
, 1, theight
- 1, cprompt
.fg
, cprompt
.bg
);
916 tb_set_cursor(startat
+ 1, theight
- 1);
919 while (tb_poll_event(&fev
) != 0) {
922 if (fev
.key
== TB_KEY_ESC
) {
923 tb_set_cursor(-1, -1);
928 if (fev
.key
== TB_KEY_BACKSPACE
||
929 fev
.key
== TB_KEY_BACKSPACE2
) {
930 if (BETWEEN(cpos
, 2, max_chars
)) {
931 result
[i
- 1] = '\0';
934 print_xstatus(' ', startat
+ cpos
);
936 startat
+ cpos
, theight
- 1);
939 } else if (fev
.key
== TB_KEY_ENTER
) {
940 tb_set_cursor(-1, -1);
941 result
[cpos
- 1] = '\0';
944 } else if (fev
.key
) { /* disable other TB_KEY_* */
948 if (cpos
< max_chars
) {
950 (char)fev
.ch
, (startat
+ cpos
));
951 result
[i
] = (char)fev
.ch
;
952 tb_set_cursor((startat
+ cpos
+ 1),
962 case TB_EVENT_RESIZE
:
965 print_tb(msg
, 1, theight
- 1, cprompt
.fg
, cprompt
.bg
);
966 print_tb(result
, startat
+ 1, theight
- 1, cstatus
.fg
,
984 for (c
= 0; c
< LEN(rules
); c
++)
985 for (d
= 0; d
< rules
[c
].exlen
; d
++)
986 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
992 spawn(const void *com_argv
, size_t com_argc
, const void *f_argv
, size_t f_argc
,
999 argc
= com_argc
+ f_argc
+ 2;
1002 memcpy(argv
, com_argv
, com_argc
* sizeof(char *)); /* command */
1003 memcpy(&argv
[com_argc
], f_argv
, f_argc
* sizeof(char *)); /* files */
1005 argv
[argc
- 2] = fn
;
1006 argv
[argc
- 1] = NULL
;
1013 execvp(argv
[0], argv
);
1016 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1020 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1036 if (c
< 0) /* extension not found open in editor */
1037 return spawn(editor
, 1, NULL
, 0, fn
);
1039 return spawn((char **)rules
[c
].v
, rules
[c
].vlen
, NULL
, 0, fn
);
1045 #if defined(_SYS_INOTIFY_H)
1046 inotify_fd
= inotify_init();
1049 #elif defined(_SYS_EVENT_H_)
1050 gtimeout
.tv_sec
= 1;
1059 addwatch(Pane
*pane
)
1061 #if defined(_SYS_INOTIFY_H)
1062 return pane
->inotify_wd
= inotify_add_watch(inotify_fd
, pane
->dirn
,
1063 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1064 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1065 #elif defined(_SYS_EVENT_H_)
1066 pane
->event_fd
= open(pane
->dirn
, O_RDONLY
);
1067 if (pane
->event_fd
< 0)
1068 return pane
->event_fd
;
1069 EV_SET(&evlist
[pane
->pane_id
], pane
->event_fd
, EVFILT_VNODE
,
1071 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
| NOTE_RENAME
|
1072 NOTE_REVOKE
| NOTE_WRITE
,
1081 #if defined(_SYS_INOTIFY_H)
1084 struct inotify_event
*event
;
1085 const size_t events
= 32;
1086 const size_t evbuflen
=
1087 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1090 if (cpane
->inotify_wd
< 0)
1092 r
= read(inotify_fd
, buf
, evbuflen
);
1096 for (p
= buf
; p
< buf
+ r
;) {
1097 event
= (struct inotify_event
*)p
;
1104 p
+= sizeof(struct inotify_event
) + event
->len
;
1106 #elif defined(_SYS_EVENT_H_)
1107 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1115 #if defined(_SYS_INOTIFY_H)
1116 if (pane
->inotify_wd
>= 0)
1117 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1118 #elif defined(_SYS_EVENT_H_)
1119 close(pane
->event_fd
);
1126 pthread_cancel(fsev_thread
);
1127 #if defined(__linux__)
1128 pthread_join(fsev_thread
, NULL
);
1130 rmwatch(&panes
[Left
]);
1131 rmwatch(&panes
[Right
]);
1132 #if defined(_SYS_INOTIFY_H)
1134 #elif defined(_SYS_EVENT_H_)
1140 toggle_df(const Arg
*arg
)
1142 show_dotfiles
= !show_dotfiles
;
1143 PERROR(listdir(&panes
[Left
]));
1144 PERROR(listdir(&panes
[Right
]));
1149 start_filter(const Arg
*arg
)
1151 if (cpane
->dirc
< 1)
1154 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1155 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1159 cpane
->filter
= user_input
;
1160 if (listdir(cpane
) < 0)
1161 print_error("no match");
1162 cpane
->filter
= NULL
;
1167 start_vmode(const Arg
*arg
)
1169 if (cpane
->dirc
< 1)
1171 struct tb_event fev
;
1172 if (sel_indexes
!= NULL
) {
1177 sel_indexes
= ecalloc(cpane
->dirc
, sizeof(size_t));
1178 sel_indexes
[0] = cpane
->hdir
;
1180 print_prompt("-- VISUAL --");
1182 while (tb_poll_event(&fev
) != 0) {
1185 grabkeys(&fev
, vkeys
, vkeyslen
);
1186 if (cont_vmode
== -1)
1195 exit_vmode(const Arg
*arg
)
1197 refresh_pane(cpane
);
1198 add_hi(cpane
, cpane
->hdir
- 1);
1203 selup(const Arg
*arg
)
1206 print_prompt("-- VISUAL --");
1207 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1209 if (cpane
->hdir
< sel_indexes
[0]) {
1210 sel_indexes
[index
] = cpane
->hdir
;
1211 add_hi(cpane
, sel_indexes
[index
]);
1212 } else if (index
< cpane
->dirc
) {
1213 sel_indexes
[index
+ 1] = 0;
1215 if (cpane
->dirc
>= scrheight
||
1216 cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1222 seldwn(const Arg
*arg
)
1225 print_prompt("-- VISUAL --");
1226 int index
= abs(cpane
->hdir
- sel_indexes
[0]);
1228 if (cpane
->hdir
> sel_indexes
[0]) {
1229 sel_indexes
[index
] = cpane
->hdir
;
1230 add_hi(cpane
, sel_indexes
[index
] - 2);
1232 sel_indexes
[index
+ 1] = 0;
1234 if (cpane
->dirc
>= scrheight
||
1235 cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1241 selall(const Arg
*arg
)
1244 for (i
= 0; i
< cpane
->dirc
; i
++) {
1245 sel_indexes
[i
] = i
+ 1;
1254 for (i
= 0; i
< cpane
->dirc
; i
++) {
1255 if (sel_indexes
[i
] < (scrheight
+ cpane
->firstrow
) &&
1257 cpane
->firstrow
) { /* checks if in the frame of the directories */
1258 add_hi(cpane
, sel_indexes
[i
] - 1);
1269 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1270 if (sel_indexes
[j
] != 0)
1282 if (sel_files
!= NULL
) {
1283 for (i
= 0; i
< sel_len
; i
++) {
1285 sel_files
[i
] = NULL
;
1299 sel_files
= ecalloc(sel_len
, sizeof(char *));
1301 for (i
= 0; i
< sel_len
; i
++) {
1302 sel_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1303 strncpy(sel_files
[i
], cpane
->direntr
[sel_indexes
[i
] - 1].name
,
1309 selynk(const Arg
*arg
)
1312 refresh_pane(cpane
);
1313 add_hi(cpane
, cpane
->hdir
- 1);
1314 print_status(cprompt
, "%zu files are yanked", sel_len
);
1319 seldel(const Arg
*arg
)
1323 inp_conf
= ecalloc(delconf_len
, sizeof(char));
1324 if ((get_usrinput(inp_conf
, delconf_len
, "delete file (yes) ?") < 0) ||
1325 (strncmp(inp_conf
, delconf
, delconf_len
) != 0)) {
1327 return; /* canceled by user or wrong inp_conf */
1333 if (spawn(rm_cmd
, rm_cmd_len
, sel_files
, sel_len
, NULL
) < 0)
1334 print_error(strerror(errno
));
1336 print_status(cprompt
, "%zu files are deleted", sel_len
);
1343 paste(const Arg
*arg
)
1345 if (sel_files
== NULL
) {
1346 print_error("nothing to paste");
1350 if (spawn(cp_cmd
, cp_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1351 print_error(strerror(errno
));
1353 print_status(cprompt
, "%zu files are copied", sel_len
);
1359 selmv(const Arg
*arg
)
1361 if (sel_files
== NULL
) {
1362 print_error("nothing to move");
1366 if (spawn(mv_cmd
, mv_cmd_len
, sel_files
, sel_len
, cpane
->dirn
) < 0)
1367 print_error(strerror(errno
));
1369 print_status(cprompt
, "%zu files are moved", sel_len
);
1375 rname(const Arg
*arg
)
1377 if (cpane
->dirc
< 1)
1379 char new_name
[MAX_P
];
1382 input_name
= ecalloc(MAX_N
, sizeof(char));
1384 if (get_usrinput(input_name
, MAX_N
, "rename: %s",
1385 basename(CURSOR(cpane
).name
)) < 0) {
1390 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1392 print_error(strerror(errno
));
1396 char *rename_cmd
[] = { "mv", CURSOR(cpane
).name
, new_name
};
1397 PERROR(spawn(rename_cmd
, 3, NULL
, 0, NULL
) < 0);
1403 yank(const Arg
*arg
)
1405 if (cpane
->dirc
< 1)
1410 sel_files
= ecalloc(sel_len
, sizeof(char *));
1411 sel_files
[0] = ecalloc(MAX_P
, sizeof(char));
1412 strncpy(sel_files
[0], CURSOR(cpane
).name
, MAX_P
);
1413 print_status(cprompt
, "1 file is yanked", sel_len
);
1417 switch_pane(const Arg
*arg
)
1419 if (cpane
->dirc
> 0)
1420 rm_hi(cpane
, cpane
->hdir
- 1);
1421 cpane
= &panes
[pane_idx
^= 1];
1422 if (cpane
->dirc
> 0) {
1423 add_hi(cpane
, cpane
->hdir
- 1);
1424 print_info(cpane
, NULL
);
1431 quit(const Arg
*arg
)
1433 if (cont_vmode
== -1) { /* check if selection was allocated */
1435 if (sel_files
!= NULL
)
1438 free(panes
[Left
].direntr
);
1439 free(panes
[Right
].direntr
);
1446 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1450 for (i
= 0; i
< max_keys
; i
++) {
1451 if (event
->ch
!= 0) {
1452 if (event
->ch
== key
[i
].evkey
.ch
) {
1453 key
[i
].func(&key
[i
].arg
);
1456 } else if (event
->key
!= 0) {
1457 if (event
->key
== key
[i
].evkey
.key
) {
1458 key
[i
].func(&key
[i
].arg
);
1468 struct timespec tim
;
1470 tim
.tv_nsec
= 5000000L; /* 0.005 sec */
1473 if (read_events() > READEVSZ
) {
1474 kill(main_pid
, SIGUSR1
);
1475 nanosleep(&tim
, NULL
);
1485 while (tb_poll_event(&ev
) != 0) {
1488 grabkeys(&ev
, nkeys
, nkeyslen
);
1491 case TB_EVENT_RESIZE
:
1502 refresh_pane(Pane
*pane
)
1504 size_t y
, dyn_max
, start_from
;
1505 hwidth
= (twidth
/ 2) - 4;
1509 start_from
= pane
->firstrow
;
1510 dyn_max
= MIN(pane
->dirc
, (scrheight
- 1) + pane
->firstrow
);
1512 /* print each entry in directory */
1513 while (start_from
< dyn_max
) {
1514 get_hicol(&col
, pane
->direntr
[start_from
].mode
);
1515 print_row(pane
, start_from
, col
);
1521 print_info(pane
, NULL
);
1525 /* print current directory title */
1526 pane
->dircol
.fg
|= TB_BOLD
;
1527 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", hwidth
, pane
->dirn
);
1531 set_direntr(Pane
*pane
, struct dirent
*entry
, DIR *dir
, char *filter
)
1538 tmpfull = get_fullpath(pane->dirn, entry->d_name); \
1539 strncpy(pane->direntr[i].name, tmpfull, MAX_N); \
1540 if (lstat(tmpfull, &status) == 0) { \
1541 pane->direntr[i].size = status.st_size; \
1542 pane->direntr[i].mode = status.st_mode; \
1543 pane->direntr[i].group = status.st_gid; \
1544 pane->direntr[i].user = status.st_uid; \
1545 pane->direntr[i].dt = status.st_mtime; \
1552 erealloc(pane
->direntr
, (10 + pane
->dirc
) * sizeof(Entry
));
1553 while ((entry
= readdir(dir
)) != 0) {
1554 if (show_dotfiles
== 1) {
1555 if (entry
->d_name
[0] == '.' &&
1556 (entry
->d_name
[1] == '\0' ||
1557 entry
->d_name
[1] == '.'))
1560 if (entry
->d_name
[0] == '.')
1564 if (filter
== NULL
) {
1566 } else if (filter
!= NULL
) {
1567 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1580 struct dirent
*entry
;
1581 int filtercount
= 0;
1582 size_t oldc
= pane
->dirc
;
1586 dir
= opendir(pane
->dirn
);
1590 /* get content and filter sum */
1591 while ((entry
= readdir(dir
)) != 0) {
1592 if (pane
->filter
!= NULL
) {
1593 if (strcasestr(entry
->d_name
, pane
->filter
) != NULL
)
1595 } else { /* no filter */
1600 if (pane
->filter
== NULL
) {
1605 if (pane
->filter
!= NULL
) {
1606 if (filtercount
> 0) {
1607 pane
->dirc
= filtercount
;
1610 } else if (filtercount
== 0) {
1611 if (closedir(dir
) < 0)
1618 /* print current directory title */
1619 pane
->dircol
.fg
|= TB_BOLD
;
1620 printf_tb(pane
->dirx
, 0, pane
->dircol
, " %.*s ", hwidth
, pane
->dirn
);
1622 if (pane
->filter
== NULL
) /* dont't watch when filtering */
1623 if (addwatch(pane
) < 0)
1624 print_error("can't add watch");
1626 /* empty directory */
1627 if (pane
->dirc
== 0) {
1629 if (closedir(dir
) < 0)
1634 rewinddir(dir
); /* reset position */
1636 pane
, entry
, dir
, pane
->filter
); /* create array of entries */
1637 qsort(pane
->direntr
, pane
->dirc
, sizeof(Entry
), sort_name
);
1640 if (pane
->hdir
> pane
->dirc
)
1641 pane
->hdir
= pane
->dirc
;
1643 if (pane
== cpane
&& pane
->dirc
> 0)
1644 add_hi(pane
, pane
->hdir
- 1);
1646 if (closedir(dir
) < 0)
1656 panes
[Right
].dirx
= (twidth
/ 2) + 2;
1657 refresh_pane(&panes
[Left
]);
1658 refresh_pane(&panes
[Right
]);
1659 if (cpane
->dirc
> 0)
1660 add_hi(cpane
, cpane
->hdir
- 1);
1667 editor
[0] = getenv("EDITOR");
1670 if (editor
[0] == NULL
)
1680 home
= getenv("HOME");
1683 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1684 strncpy(cwd
, home
, MAX_P
);
1686 pane_idx
= Left
; /* cursor pane */
1687 cpane
= &panes
[pane_idx
];
1689 panes
[Left
].ex
= (twidth
/ 2) - 1;
1690 panes
[Left
].pane_id
= 0;
1691 panes
[Left
].dirx
= 2;
1692 panes
[Left
].dircol
= cpanell
;
1693 panes
[Left
].firstrow
= 0;
1694 panes
[Left
].direntr
= ecalloc(0, sizeof(Entry
));
1695 strncpy(panes
[Left
].dirn
, cwd
, MAX_P
);
1696 panes
[Left
].hdir
= 1;
1697 panes
[Left
].inotify_wd
= -1;
1698 panes
[Left
].parent_row
= 1;
1700 panes
[Right
].ex
= twidth
- 1;
1701 panes
[Right
].pane_id
= 1;
1702 panes
[Right
].dirx
= (twidth
/ 2) + 2;
1703 panes
[Right
].dircol
= cpanelr
;
1704 panes
[Right
].firstrow
= 0;
1705 panes
[Right
].direntr
= ecalloc(0, sizeof(Entry
));
1706 strncpy(panes
[Right
].dirn
, home
, MAX_P
);
1707 panes
[Right
].hdir
= 1;
1708 panes
[Right
].inotify_wd
= -1;
1709 panes
[Right
].parent_row
= 1;
1716 theight
= tb_height();
1717 twidth
= tb_width();
1718 hwidth
= (twidth
/ 2) - 4;
1719 scrheight
= theight
- 2;
1721 /* 2 horizontal lines */
1722 for (i
= 1; i
< twidth
- 1; ++i
) {
1723 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1724 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1727 /* 4 vertical lines */
1728 for (i
= 1; i
< theight
- 1; ++i
) {
1729 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1731 (twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
, cframe
.bg
);
1732 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1734 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1738 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1739 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1740 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1741 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1743 /* 2 middel top and bottom */
1744 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1746 (twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1753 PERROR(listdir(&panes
[Left
]));
1754 PERROR(listdir(&panes
[Right
]));
1761 struct sigaction sa
;
1763 main_pid
= getpid();
1764 sa
.sa_handler
= th_handler
;
1765 sigemptyset(&sa
.sa_mask
);
1766 sa
.sa_flags
= SA_RESTART
;
1767 return sigaction(SIGUSR1
, &sa
, NULL
);
1773 switch (tb_init()) {
1774 case TB_EFAILED_TO_OPEN_TTY
:
1775 die("TB_EFAILED_TO_OPEN_TTY");
1776 case TB_EUNSUPPORTED_TERMINAL
:
1777 die("TB_EUNSUPPORTED_TERMINAL");
1778 case TB_EPIPE_TRAP_ERROR
:
1779 die("TB_EUNSUPPORTED_TERMINAL");
1783 die("UNKNOWN FAILURE");
1786 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1787 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1788 die("output error");
1792 PERROR(start_signal() < 0);
1793 PERROR(fsev_init() < 0);
1794 PERROR(listdir(&panes
[Left
]) < 0);
1795 PERROR(listdir(&panes
[Right
]) < 0);
1798 pthread_create(&fsev_thread
, NULL
, read_th
, NULL
);
1803 main(int argc
, char *argv
[])
1805 #if defined(__OpenBSD__)
1806 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1809 #endif /* __OpenBSD__ */
1812 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1813 die("sfm-" VERSION
);
1815 die("usage: sfm [-v]");