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 cpane->direntr[cpane->hdir - 1]
54 enum { AddHi
, NoHi
}; /* add highlight in listdir */
73 char dirn
[MAX_P
]; // dir name cwd
75 Entry
*direntr
; // dir entries
76 int dirx
; // pane cwd x pos
77 int dirc
; // dir entries sum
78 int hdir
; // highlighted dir
81 int parent_row
; // FIX
100 uint16_t key
; /* one of the TB_KEY_* constants */
101 uint32_t ch
; /* unicode character */
109 /* function declarations */
110 static void print_tb(const char *, int, int, uint16_t, uint16_t);
111 static void printf_tb(int, int, Cpair
, const char *, ...);
112 static void print_status(Cpair
, const char *, ...);
113 static void print_xstatus(char, int);
114 static void print_error(char *);
115 static void print_prompt(char *);
116 static void print_info(char *);
117 static void print_row(Pane
*, size_t, Cpair
);
118 static void clear(int, int, int, uint16_t);
119 static void clear_status(void);
120 static void clear_pane(void);
121 static void add_hi(Pane
*, size_t);
122 static void rm_hi(Pane
*, size_t);
123 static int check_dir(char *);
124 static mode_t
chech_execf(mode_t
);
125 static int sort_name(const void *const, const void *const);
126 static void get_dirp(char *);
127 static char *get_ext(char *);
128 static int get_fdt(char *, time_t);
129 static char *get_fgrp(gid_t
);
130 static char *get_fperm(mode_t
);
131 static char *get_fsize(off_t
);
132 static char *get_fullpath(char *, char *);
133 static char *get_fusr(uid_t
);
134 static void get_dirsize(char *, off_t
*);
135 static void get_hicol(Cpair
*, mode_t
);
136 static int delent(char *);
137 static void calcdir(void);
138 static void crnd(void);
139 static void crnf(void);
140 static void delfd(void);
141 static void mvbk(void);
142 static void mvbtm(void);
143 static void mvdwn(void);
144 static void mvdwns(void);
145 static void mvfwd(void);
146 static void mvmid(void);
147 static void mvtop(void);
148 static void mvup(void);
149 static void mvups(void);
150 static void scrdwn(void);
151 static void scrdwns(void);
152 static void scrup(void);
153 static void scrups(void);
154 static int get_usrinput(char *, size_t, const char *, ...);
155 static int frules(char *);
156 static int spawn(const void *, size_t, const void *, size_t, char *);
157 static int opnf(char *);
158 static int fsev_init(void);
159 static int addwatch(void);
160 static int read_events(void);
161 static void rmwatch(Pane
*);
162 static void fsev_shdn(void);
163 static ssize_t
findbm(uint32_t);
164 static void start_filter(void);
165 static void start_vmode(void);
166 static void exit_vmode(void);
167 static void selup(void);
168 static void seldwn(void);
169 static void selall(void);
170 static void selref(void);
171 static void selynk(void);
172 static void selcalc(void);
173 static void paste(void);
174 static void selmv(void);
175 static void seldel(void);
176 static void init_files(void);
177 static void free_files(void);
178 static void yank(void);
179 static void rname(void);
180 static void switch_pane(void);
181 static void quit(void);
182 static void grabkeys(struct tb_event
*, Key
*, size_t);
183 static void *read_th(void *arg
);
184 static void start_ev(void);
185 static void refresh_pane(void);
186 static void set_direntr(struct dirent
*, DIR *, char *);
187 static int listdir(int);
188 static void t_resize(void);
189 static void set_panes(void);
190 static void draw_frame(void);
191 static void start(void);
193 /* global variables */
194 static pthread_t fsev_thread
;
195 static Pane pane_r
, pane_l
, *cpane
;
196 static char *editor
[2];
197 static char fed
[] = "vi";
198 static int theight
, twidth
, scrheight
;
199 static size_t selection_size
= 0;
200 static char *yank_file
[MAX_P
];
201 static int *selection
;
202 static int cont_vmode
= 0;
203 static char **selected_files
;
205 #if defined _SYS_INOTIFY_H
207 static int inotify_fd
;
208 #elif defined _SYS_EVENT_H_
211 struct kevent evlist
[2]; /* events we want to monitor */
212 struct kevent chlist
[2]; /* events that were triggered */
213 static struct timespec gtimeout
;
215 #if defined(__linux__) || defined(__FreeBSD__)
217 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
221 /* configuration, allows nested code to access above variables */
224 /* function implementations */
226 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
228 while (*str
!= '\0') {
230 str
+= tb_utf8_char_to_unicode(&uni
, str
);
231 tb_change_cell(x
, y
, uni
, fg
, bg
);
237 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
242 (void)vsnprintf(buf
, MAX_LINE
, fmt
, vl
);
244 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
248 print_status(Cpair col
, const char *fmt
, ...)
250 char buf
[MAX_STATUS
];
253 (void)vsnprintf(buf
, MAX_STATUS
, fmt
, vl
);
256 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
260 print_xstatus(char c
, int x
)
263 (void)tb_utf8_char_to_unicode(&uni
, &c
);
264 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
268 print_error(char *errmsg
)
270 print_status(cerr
, errmsg
);
274 print_prompt(char *prompt
)
276 print_status(cprompt
, prompt
);
280 print_info(char *dirsize
)
282 char *sz
, *ur
, *gr
, *dt
, *prm
;
284 dt
= ecalloc(MAX_DTF
, sizeof(char));
286 prm
= get_fperm(CURSOR
.mode
);
287 ur
= get_fusr(CURSOR
.user
);
288 gr
= get_fgrp(CURSOR
.group
);
290 if (get_fdt(dt
, CURSOR
.dt
) < 0)
293 if (S_ISREG(CURSOR
.mode
)) {
294 sz
= get_fsize(CURSOR
.size
);
296 if (dirsize
== NULL
) {
297 sz
= ecalloc(1, sizeof(char));
304 print_status(cstatus
, "%02d/%02d %s %s:%s %s %s", cpane
->hdir
,
305 cpane
->dirc
, prm
, ur
, gr
, dt
, sz
);
315 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
320 char lnk_full
[MAX_P
];
323 width
= (twidth
/ 2) - 4;
324 result
= basename(pane
->direntr
[entpos
].name
);
326 y
= entpos
- cpane
->firstrow
+ 1;
328 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
329 if (realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
331 lnk_full
, MAX_N
, "%s -> %s", result
, buf
);
336 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
340 clear(int sx
, int ex
, int y
, uint16_t bg
)
342 /* clear line from to */
343 /* x = line number vertical */
344 /* y = column number horizontal */
346 for (i
= sx
; i
< ex
; i
++) {
347 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
354 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
361 y
= 0, i
= 0, ex
= 0;
363 if (cpane
->pane_id
== pane_l
.pane_id
)
364 ex
= (twidth
/ 2) - 1;
365 else if (cpane
->pane_id
== pane_r
.pane_id
)
368 while (i
< scrheight
) {
369 clear(cpane
->dirx
, ex
, y
, TB_DEFAULT
);
375 for (y
= cpane
->dirx
; y
< ex
; ++y
) {
376 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
381 add_hi(Pane
*pane
, size_t entpos
)
384 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
385 col
.fg
|= TB_REVERSE
| TB_BOLD
;
386 col
.bg
|= TB_REVERSE
;
387 print_row(pane
, entpos
, col
);
391 rm_hi(Pane
*pane
, size_t entpos
)
394 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
395 print_row(pane
, entpos
, col
);
399 check_dir(char *path
)
405 if (errno
== ENOTDIR
) {
412 if (closedir(dir
) < 0)
419 chech_execf(mode_t mode
)
422 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
427 sort_name(const void *const A
, const void *const B
)
430 mode_t data1
= (*(Entry
*)A
).mode
;
431 mode_t data2
= (*(Entry
*)B
).mode
;
435 } else if (data1
== data2
) {
436 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
449 len
= strnlen(cdir
, MAX_P
);
453 for (i
= len
- 1; i
> 1; i
--) {
460 cdir
[len
- counter
- 1] = '\0';
468 size_t counter
, len
, i
;
472 len
= strnlen(str
, MAX_N
);
474 for (i
= len
- 1; i
> 0; i
--) {
482 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
483 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
489 get_fdt(char *result
, time_t status
)
492 localtime_r(&status
, <
);
493 return strftime(result
, MAX_DTF
, dtfmt
, <
);
497 get_fgrp(gid_t status
)
502 result
= ecalloc(MAX_GRPN
, sizeof(char));
503 gr
= getgrgid(status
);
505 (void)snprintf(result
, MAX_GRPN
, "%u", status
);
507 strncpy(result
, gr
->gr_name
, MAX_GRPN
);
509 result
[MAX_GRPN
- 1] = '\0';
514 get_fperm(mode_t mode
)
519 const char chars
[] = "rwxrwxrwx";
520 buf
= ecalloc(11, sizeof(char));
524 else if (S_ISREG(mode
))
526 else if (S_ISLNK(mode
))
528 else if (S_ISBLK(mode
))
530 else if (S_ISCHR(mode
))
532 else if (S_ISFIFO(mode
))
534 else if (S_ISSOCK(mode
))
539 for (i
= 1; i
< 10; i
++) {
540 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
548 get_fsize(off_t size
)
550 char *result
; /* need to be freed */
556 result_len
= 6; /* 9999X/0 */
557 result
= ecalloc(result_len
, sizeof(char));
559 while (size
>= 1000) {
584 if (snprintf(result
, result_len
, OFF_T
"%c", size
, unit
) < 0)
585 strncat(result
, "???", result_len
);
591 get_fullpath(char *first
, char *second
)
595 full_path
= ecalloc(MAX_P
, sizeof(char));
597 if (strncmp(first
, "/", MAX_P
) == 0)
598 (void)snprintf(full_path
, MAX_P
, "/%s", second
);
600 (void)snprintf(full_path
, MAX_P
, "%s/%s", first
, second
);
606 get_fusr(uid_t status
)
611 result
= ecalloc(MAX_USRN
, sizeof(char));
612 pw
= getpwuid(status
);
614 (void)snprintf(result
, MAX_USRN
, "%u", status
);
616 strncpy(result
, pw
->pw_name
, MAX_USRN
);
618 result
[MAX_USRN
- 1] = '\0';
623 get_dirsize(char *fullpath
, off_t
*fullsize
)
628 struct dirent
*entry
;
631 dir
= opendir(fullpath
);
636 while ((entry
= readdir(dir
)) != 0) {
637 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
638 strncmp(entry
->d_name
, "..", 3) == 0))
641 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
642 if (lstat(ent_full
, &status
) == 0) {
643 mode
= status
.st_mode
;
645 get_dirsize(ent_full
, fullsize
);
648 *fullsize
+= status
.st_size
;
659 get_hicol(Cpair
*col
, mode_t mode
)
664 else if (S_ISLNK(mode
))
666 else if (chech_execf(mode
) > 0)
671 delent(char *fullpath
)
677 inp_conf
= ecalloc(conf_len
, sizeof(char));
678 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
679 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
681 return 1; /* canceled by user or wrong inp_conf */
685 return spawn(rm_cmd
, rm_cmd_len
, yank_file
, 1, fullpath
);
691 if (!S_ISDIR(CURSOR
.mode
))
697 fullsize
= ecalloc(1, sizeof(off_t
));
698 get_dirsize(CURSOR
.name
, fullsize
);
699 csize
= get_fsize(*fullsize
);
701 CURSOR
.size
= *fullsize
;
709 char *user_input
, *path
;
711 user_input
= ecalloc(MAX_USRI
, sizeof(char));
712 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
717 path
= ecalloc(MAX_P
, sizeof(char));
718 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
724 if (mkdir(path
, ndir_perm
) < 0)
725 print_error(strerror(errno
));
734 char *user_input
, *path
;
737 user_input
= ecalloc(MAX_USRI
, sizeof(char));
738 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
743 path
= ecalloc(MAX_P
, sizeof(char));
744 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
750 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
753 print_error(strerror(errno
));
754 else if (close(rf
) < 0)
755 print_error(strerror(errno
));
764 switch (delent(CURSOR
.name
)) {
766 print_error(strerror(errno
));
769 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
778 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
782 get_dirp(cpane
->dirn
);
783 if (check_dir(cpane
->dirn
) < 0) {
784 print_error(strerror(errno
));
789 cpane
->firstrow
= cpane
->parent_firstrow
;
790 cpane
->hdir
= cpane
->parent_row
;
791 if (listdir(AddHi
) < 0)
792 print_error(strerror(errno
));
793 cpane
->parent_firstrow
= 0;
794 cpane
->parent_row
= 1;
802 if (cpane
->dirc
> scrheight
) {
803 rm_hi(cpane
, cpane
->hdir
- 1);
804 cpane
->hdir
= cpane
->dirc
;
805 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
807 add_hi(cpane
, cpane
->hdir
- 1);
809 rm_hi(cpane
, cpane
->hdir
- 1);
810 cpane
->hdir
= cpane
->dirc
;
811 add_hi(cpane
, cpane
->hdir
- 1);
821 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
822 rm_hi(cpane
, cpane
->hdir
- 1);
824 add_hi(cpane
, cpane
->hdir
- 1);
826 mvdwns(); /* scroll */
835 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
837 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
839 rm_hi(cpane
, cpane
->hdir
- 1);
842 add_hi(cpane
, cpane
->hdir
- 1);
843 } else if (cpane
->hdir
< cpane
->dirc
) {
844 rm_hi(cpane
, cpane
->hdir
- 1);
846 add_hi(cpane
, cpane
->hdir
- 1);
858 switch (check_dir(CURSOR
.name
)) {
860 strncpy(cpane
->dirn
, CURSOR
.name
, MAX_P
);
861 cpane
->parent_row
= cpane
->hdir
;
862 cpane
->parent_firstrow
= cpane
->firstrow
;
865 if (listdir(AddHi
) < 0)
866 print_error(strerror(errno
));
868 case 1: /* not a directory open file */
870 s
= opnf(CURSOR
.name
);
875 print_error("process failed non-zero exit");
877 case -1: /* failed to open directory */
878 print_error(strerror(errno
));
887 rm_hi(cpane
, cpane
->hdir
- 1);
888 if (cpane
->dirc
< scrheight
/ 2)
889 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
891 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
892 add_hi(cpane
, cpane
->hdir
- 1);
901 if (cpane
->dirc
> scrheight
) {
902 rm_hi(cpane
, cpane
->hdir
- 1);
906 add_hi(cpane
, cpane
->hdir
- 1);
908 rm_hi(cpane
, cpane
->hdir
- 1);
910 add_hi(cpane
, cpane
->hdir
- 1);
920 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
921 rm_hi(cpane
, cpane
->hdir
- 1);
923 add_hi(cpane
, cpane
->hdir
- 1);
925 mvups(); /* scroll */
934 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
936 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
938 rm_hi(cpane
, cpane
->hdir
- 1);
941 add_hi(cpane
, cpane
->hdir
- 1);
942 } else if (cpane
->hdir
> 1) {
943 rm_hi(cpane
, cpane
->hdir
- 1);
945 add_hi(cpane
, cpane
->hdir
- 1);
954 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
955 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
956 rm_hi(cpane
, cpane
->hdir
- 1);
957 cpane
->hdir
+= scrmv
;
958 add_hi(cpane
, cpane
->hdir
- 1);
973 real
= cpane
->hdir
- cpane
->firstrow
;
974 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
976 if (real
+ scrmv
+ 1 > scrheight
&&
977 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
978 cpane
->firstrow
+= dynmv
;
979 rm_hi(cpane
, cpane
->hdir
- 1);
980 cpane
->hdir
+= scrmv
;
982 add_hi(cpane
, cpane
->hdir
- 1);
984 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
985 rm_hi(cpane
, cpane
->hdir
- 1);
986 cpane
->hdir
+= scrmv
;
987 add_hi(cpane
, cpane
->hdir
- 1);
999 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1000 if (cpane
->hdir
> scrmv
) {
1001 rm_hi(cpane
, cpane
->hdir
- 1);
1002 cpane
->hdir
= cpane
->hdir
- scrmv
;
1003 add_hi(cpane
, cpane
->hdir
- 1);
1017 real
= cpane
->hdir
- cpane
->firstrow
;
1018 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1020 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1021 cpane
->firstrow
-= dynmv
;
1022 rm_hi(cpane
, cpane
->hdir
- 1);
1023 cpane
->hdir
-= scrmv
;
1025 add_hi(cpane
, cpane
->hdir
- 1);
1027 if (cpane
->hdir
> scrmv
+ 1) {
1028 rm_hi(cpane
, cpane
->hdir
- 1);
1029 cpane
->hdir
-= scrmv
;
1030 add_hi(cpane
, cpane
->hdir
- 1);
1038 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
1040 int height
= tb_height();
1042 struct tb_event fev
;
1043 size_t counter
= (size_t)1;
1055 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
1057 print_tb(buf
, 1, height
- 1, col
.fg
, col
.bg
);
1058 startat
= name_size
+ 1;
1059 tb_set_cursor((int)(startat
+ 1), height
- 1);
1062 while (tb_poll_event(&fev
) != 0) {
1065 if (fev
.key
== TB_KEY_ESC
) {
1066 tb_set_cursor(-1, -1);
1071 if (fev
.key
== TB_KEY_BACKSPACE
||
1072 fev
.key
== TB_KEY_BACKSPACE2
) {
1073 if (BETWEEN(counter
, 2, sout
)) {
1077 print_xstatus(empty
, startat
+ counter
);
1079 startat
+ counter
, theight
- 1);
1082 } else if (fev
.key
== TB_KEY_ENTER
) {
1083 tb_set_cursor(-1, -1);
1084 out
[counter
- 1] = '\0';
1088 if (counter
< sout
) {
1089 print_xstatus((char)fev
.ch
,
1090 (startat
+ counter
));
1091 out
[x
] = (char)fev
.ch
;
1092 tb_set_cursor((startat
+ counter
+ 1),
1115 for (c
= 0; c
< LEN(rules
); c
++)
1116 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1117 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1123 spawn(const void *com_argv
, size_t com_argc
, const void *f_argv
, size_t f_argc
,
1130 argc
= com_argc
+ f_argc
+ 2;
1133 memcpy(argv
, com_argv
, com_argc
* sizeof(char *)); /* command */
1134 memcpy(&argv
[com_argc
], f_argv
, f_argc
* sizeof(char *)); /* files */
1136 argv
[argc
- 2] = fn
;
1137 argv
[argc
- 1] = NULL
;
1144 execvp(argv
[0], argv
);
1147 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1151 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1167 if (c
< 0) /* extension not found open in editor */
1168 return spawn(editor
, 1, NULL
, 0, fn
);
1170 return spawn((char **)rules
[c
].v
, rules
[c
].vlen
, NULL
, 0, fn
);
1176 #if defined _SYS_INOTIFY_H
1177 inotify_fd
= inotify_init();
1180 #elif defined _SYS_EVENT_H_
1181 gtimeout
.tv_sec
= 1;
1192 #if defined _SYS_INOTIFY_H
1193 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1194 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1195 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1196 #elif defined _SYS_EVENT_H_
1197 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1198 if (cpane
->event_fd
< 0)
1199 return cpane
->event_fd
;
1200 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
, EVFILT_VNODE
,
1202 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
| NOTE_RENAME
|
1203 NOTE_REVOKE
| NOTE_WRITE
,
1212 #if defined _SYS_INOTIFY_H
1215 struct inotify_event
*event
;
1216 const size_t events
= 32;
1217 const size_t evbuflen
=
1218 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1221 if (cpane
->inotify_wd
< 0)
1223 r
= read(inotify_fd
, buf
, evbuflen
);
1227 for (p
= buf
; p
< buf
+ r
;) {
1228 event
= (struct inotify_event
*)p
;
1235 p
+= sizeof(struct inotify_event
) + event
->len
;
1237 #elif defined _SYS_EVENT_H_
1238 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1246 #if defined _SYS_INOTIFY_H
1247 if (pane
->inotify_wd
>= 0)
1248 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1249 #elif defined _SYS_EVENT_H_
1250 close(pane
->event_fd
);
1257 pthread_cancel(fsev_thread
);
1259 pthread_join(fsev_thread
, NULL
);
1263 #if defined _SYS_INOTIFY_H
1265 #elif defined _SYS_EVENT_H_
1271 findbm(uint32_t event
)
1275 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1276 if (event
== bmarks
[i
].ch
) {
1277 if (check_dir(bmarks
[i
].path
) != 0) {
1278 print_error(strerror(errno
));
1290 if (cpane
->dirc
< 1)
1293 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1294 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1298 cpane
->filter
= user_input
;
1299 if (listdir(AddHi
) < 0)
1300 print_error("no match");
1301 cpane
->filter
= NULL
;
1308 struct tb_event fev
;
1309 if (selection
!= NULL
) {
1314 selection
= ecalloc(cpane
->dirc
, sizeof(size_t));
1315 selection
[0] = cpane
->hdir
;
1317 print_prompt("-- VISUAL --");
1319 while (tb_poll_event(&fev
) != 0) {
1322 grabkeys(&fev
, vkeys
, vkeyslen
);
1323 if (cont_vmode
== -1)
1335 add_hi(cpane
, cpane
->hdir
- 1);
1343 print_prompt("-- VISUAL --");
1344 int index
= abs(cpane
->hdir
- selection
[0]);
1346 if (cpane
->hdir
< selection
[0]) {
1347 selection
[index
] = cpane
->hdir
;
1348 add_hi(cpane
, selection
[index
]);
1349 } else if (index
< cpane
->dirc
) {
1350 selection
[index
+ 1] = 0;
1352 if (cpane
->dirc
>= scrheight
||
1353 cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1362 print_prompt("-- VISUAL --");
1363 int index
= abs(cpane
->hdir
- selection
[0]);
1365 if (cpane
->hdir
> selection
[0]) {
1366 selection
[index
] = cpane
->hdir
;
1367 add_hi(cpane
, selection
[index
] - 2);
1369 selection
[index
+ 1] = 0;
1371 if (cpane
->dirc
>= scrheight
||
1372 cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1381 for (i
= 0; i
< cpane
->dirc
; i
++) {
1382 selection
[i
] = i
+ 1;
1391 for (i
= 0; i
< cpane
->dirc
; i
++) {
1392 if (selection
[i
] < (scrheight
+ cpane
->firstrow
) &&
1394 cpane
->firstrow
) { /* checks if in the frame of the directories */
1395 add_hi(cpane
, selection
[i
] - 1);
1406 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1407 if (selection
[j
] != 0)
1417 if (selected_files
== NULL
)
1419 free(selected_files
);
1420 selected_files
= NULL
;
1430 selected_files
= ecalloc(selection_size
, sizeof(char *));
1432 for (i
= 0; i
< selection_size
; i
++) {
1433 selected_files
[i
] = cpane
->direntr
[selection
[i
] - 1].name
;
1442 add_hi(cpane
, cpane
->hdir
- 1);
1443 print_status(cprompt
, "%zu files are yanked", selection_size
);
1452 char conf
[] = "yes";
1454 inp_conf
= ecalloc(conf_len
, sizeof(char));
1455 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
1456 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
1458 return; /* canceled by user or wrong inp_conf */
1464 if (spawn(rm_cmd
, rm_cmd_len
, selected_files
, selection_size
, NULL
) < 0)
1465 print_error(strerror(errno
));
1467 print_status(cprompt
, "%zu files are deleted", selection_size
);
1469 if (cpane
->dirc
> 0)
1479 if (yank_file
[0] != NULL
) {
1480 print_status(cprompt
, "coping");
1481 if (spawn(cp_cmd
, cp_cmd_len
, &yank_file
, 1, cpane
->dirn
) != 0)
1482 print_error(strerror(errno
));
1484 print_status(cprompt
, "file copied");
1485 yank_file
[0] = NULL
; /* set yank_file len 0 */
1489 print_error("nothing to paste");
1491 if (selected_files
== NULL
)
1494 if (spawn(cp_cmd
, cp_cmd_len
, selected_files
, selection_size
,
1496 print_error(strerror(errno
));
1498 print_status(cprompt
, "%zu files are copied", selection_size
);
1506 if (yank_file
[0] != NULL
) {
1507 print_status(cprompt
, "moving");
1508 if (spawn(mv_cmd
, mv_cmd_len
, yank_file
, 1, cpane
->dirn
) != 0)
1509 print_error(strerror(errno
));
1511 print_status(cprompt
, "file moved");
1512 yank_file
[0] = NULL
; /* set yank_file len 0 */
1516 print_error("nothing to move");
1518 if (selected_files
== NULL
)
1521 if (spawn(mv_cmd
, mv_cmd_len
, selected_files
, selection_size
,
1523 print_error(strerror(errno
));
1525 print_status(cprompt
, "%zu files are moved", selection_size
);
1533 char new_name
[MAX_P
];
1536 input_name
= ecalloc(MAX_N
, sizeof(char));
1538 if (get_usrinput(input_name
, MAX_N
, "rename: %s",
1539 basename(CURSOR
.name
)) < 0) {
1544 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1546 print_error(strerror(errno
));
1550 char *rename_cmd
[] = { "mv", CURSOR
.name
, new_name
};
1551 if (spawn(rename_cmd
, 3, NULL
, 0, NULL
) < 0)
1552 print_error(strerror(errno
));
1560 yank_file
[0] = CURSOR
.name
;
1561 print_status(cprompt
, "1 file is yanked", selection_size
);
1567 if (cpane
->dirc
> 0)
1568 rm_hi(cpane
, cpane
->hdir
- 1);
1569 if (cpane
== &pane_l
)
1571 else if (cpane
== &pane_r
)
1573 if (cpane
->dirc
> 0) {
1574 add_hi(cpane
, cpane
->hdir
- 1);
1584 if (cont_vmode
== -1) { /* check if selection was allocated */
1586 if (selected_files
!= NULL
)
1589 free(pane_l
.direntr
);
1590 free(pane_r
.direntr
);
1597 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1602 for (i
= 0; i
< max_keys
; i
++) {
1603 if (event
->ch
!= 0) {
1604 if (event
->ch
== key
[i
].evkey
.ch
) {
1608 } else if (event
->key
!= 0) {
1609 if (event
->key
== key
[i
].evkey
.key
) {
1617 b
= findbm(event
->ch
);
1621 strncpy(cpane
->dirn
, bmarks
[b
].path
, MAX_P
);
1622 cpane
->firstrow
= 0;
1623 cpane
->parent_row
= 1;
1625 if (listdir(AddHi
) < 0)
1626 print_error(strerror(errno
));
1632 struct timespec tim
;
1634 tim
.tv_nsec
= 5000000L; /* 0.005 sec */
1637 if (read_events() > READEVSZ
) {
1638 kill(main_pid
, SIGUSR1
);
1639 nanosleep(&tim
, NULL
);
1649 while (tb_poll_event(&ev
) != 0) {
1652 grabkeys(&ev
, nkeys
, nkeyslen
);
1655 case TB_EVENT_RESIZE
:
1668 size_t y
, dyn_max
, start_from
;
1670 width
= (twidth
/ 2) - 4;
1674 start_from
= cpane
->firstrow
;
1675 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1677 /* print each entry in directory */
1678 while (start_from
< dyn_max
) {
1679 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1680 print_row(cpane
, start_from
, col
);
1685 if (cpane
->dirc
> 0)
1690 /* print current directory title */
1691 cpane
->dircol
.fg
|= TB_BOLD
;
1692 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1696 set_direntr(struct dirent
*entry
, DIR *dir
, char *filter
)
1703 tmpfull = get_fullpath(cpane->dirn, entry->d_name); \
1704 strncpy(cpane->direntr[i].name, tmpfull, MAX_N); \
1705 if (lstat(tmpfull, &status) == 0) { \
1706 cpane->direntr[i].size = status.st_size; \
1707 cpane->direntr[i].mode = status.st_mode; \
1708 cpane->direntr[i].group = status.st_gid; \
1709 cpane->direntr[i].user = status.st_uid; \
1710 cpane->direntr[i].dt = status.st_mtime; \
1717 erealloc(cpane
->direntr
, (10 + cpane
->dirc
) * sizeof(Entry
));
1718 while ((entry
= readdir(dir
)) != 0) {
1719 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
1720 strncmp(entry
->d_name
, "..", 3) == 0))
1723 if (filter
== NULL
) {
1725 } else if (filter
!= NULL
) {
1726 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1739 struct dirent
*entry
;
1741 int filtercount
= 0;
1742 size_t oldc
= cpane
->dirc
;
1744 width
= (twidth
/ 2) - 4;
1747 dir
= opendir(cpane
->dirn
);
1751 /* get content and filter sum */
1752 while ((entry
= readdir(dir
)) != 0) {
1753 if (cpane
->filter
!= NULL
) {
1754 if (strcasestr(entry
->d_name
, cpane
->filter
) != NULL
)
1756 } else { /* no filter */
1761 if (cpane
->filter
== NULL
) {
1766 if (cpane
->filter
!= NULL
) {
1767 if (filtercount
> 0) {
1768 cpane
->dirc
= filtercount
;
1771 } else if (filtercount
== 0) {
1772 if (closedir(dir
) < 0)
1779 /* print current directory title */
1780 cpane
->dircol
.fg
|= TB_BOLD
;
1781 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1783 if (cpane
->filter
== NULL
) /* dont't watch when filtering */
1785 print_error("can't add watch");
1787 /* empty directory */
1788 if (cpane
->dirc
== 0) {
1790 if (closedir(dir
) < 0)
1795 rewinddir(dir
); /* reset position */
1796 set_direntr(entry
, dir
, cpane
->filter
); /* create array of entries */
1797 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1800 if (hi
== AddHi
&& cpane
->dirc
> 0)
1801 add_hi(cpane
, cpane
->hdir
- 1);
1803 if (closedir(dir
) < 0)
1811 /* TODO need refactoring */
1814 pane_r
.dirx
= (twidth
/ 2) + 2;
1816 if (cpane
== &pane_l
) {
1821 if (cpane
->dirc
> 0)
1822 add_hi(&pane_l
, pane_l
.hdir
- 1);
1823 } else if (cpane
== &pane_r
) {
1828 if (cpane
->dirc
> 0)
1829 add_hi(&pane_r
, pane_r
.hdir
- 1);
1838 editor
[0] = getenv("EDITOR");
1841 if (editor
[0] == NULL
)
1851 home
= getenv("HOME");
1854 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1855 strncpy(cwd
, home
, MAX_P
);
1859 pane_l
.dircol
= cpanell
;
1860 pane_l
.firstrow
= 0;
1861 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1862 strncpy(pane_l
.dirn
, cwd
, MAX_P
);
1864 pane_l
.inotify_wd
= -1;
1865 pane_l
.parent_row
= 1;
1868 pane_r
.dirx
= (twidth
/ 2) + 2;
1869 pane_r
.dircol
= cpanelr
;
1870 pane_r
.firstrow
= 0;
1871 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1872 strncpy(pane_r
.dirn
, home
, MAX_P
);
1874 pane_r
.inotify_wd
= -1;
1875 pane_r
.parent_row
= 1;
1882 theight
= tb_height();
1883 twidth
= tb_width();
1884 scrheight
= theight
- 2;
1886 /* 2 horizontal lines */
1887 for (i
= 1; i
< twidth
- 1; ++i
) {
1888 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1889 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1892 /* 4 vertical lines */
1893 for (i
= 1; i
< theight
- 1; ++i
) {
1894 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1896 (twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
, cframe
.bg
);
1897 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1899 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1903 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1904 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1905 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1906 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1908 /* 2 middel top and bottom */
1909 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1911 (twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1918 if (cpane
== &pane_l
) {
1920 if (listdir(NoHi
) < 0)
1921 print_error(strerror(errno
));
1923 if (listdir(AddHi
) < 0)
1924 print_error(strerror(errno
));
1925 } else if (cpane
== &pane_r
) {
1927 if (listdir(NoHi
) < 0)
1928 print_error(strerror(errno
));
1930 if (listdir(AddHi
) < 0)
1931 print_error(strerror(errno
));
1939 struct sigaction sa
;
1941 main_pid
= getpid();
1942 sa
.sa_handler
= th_handler
;
1943 sigemptyset(&sa
.sa_mask
);
1944 sa
.sa_flags
= SA_RESTART
;
1945 return sigaction(SIGUSR1
, &sa
, NULL
);
1953 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1954 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1955 die("output error");
1960 if (start_signal() < 0)
1961 print_error(strerror(errno
));
1962 if (fsev_init() < 0)
1963 print_error(strerror(errno
));
1965 if (listdir(NoHi
) < 0)
1966 print_error(strerror(errno
));
1968 if (listdir(AddHi
) < 0)
1969 print_error(strerror(errno
));
1972 pthread_create(&fsev_thread
, NULL
, read_th
, NULL
);
1977 main(int argc
, char *argv
[])
1980 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1983 #endif /* __OpenBSD__ */
1986 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1987 die("sfm-" VERSION
);
1989 die("usage: sfm [-v]");