1 /* See LICENSE file for copyright and license details. */
5 #elif defined(__APPLE__)
6 #define _DARWIN_C_SOURCE
9 #include <sys/resource.h>
13 #if defined(__linux__)
14 #include <sys/inotify.h>
15 #elif defined(__FreeBSD__) || defined(__NetBSD__) ||\
16 defined(__OpenBSD__) || defined(__APPLE__)
17 #include <sys/event.h>
42 #define MAX_STATUS 255
47 #define CURSOR cpane->direntr[cpane->hdir - 1]
50 enum { AddHi
, NoHi
}; /* add highlight in listdir */
69 char dirn
[MAX_P
]; // dir name cwd
71 Entry
*direntr
; // dir entries
72 int dirx
; // pane cwd x pos
73 int dirc
; // dir entries sum
74 int hdir
; // highlighted dir
77 int parent_row
; // FIX
95 uint16_t key
; /* one of the TB_KEY_* constants */
96 uint32_t ch
; /* unicode character */
104 /* function declarations */
105 static void print_tb(const char *, int, int, uint16_t, uint16_t);
106 static void printf_tb(int, int, Cpair
, const char *, ...);
107 static void print_status(Cpair
, const char *, ...);
108 static void print_xstatus(char, int);
109 static void print_error(char *);
110 static void print_prompt(char *);
111 static void print_info(char *);
112 static void print_row(Pane
*, size_t, Cpair
);
113 static void clear(int, int, int, uint16_t);
114 static void clear_status(void);
115 static void clear_pane(void);
116 static void add_hi(Pane
*, size_t);
117 static void rm_hi(Pane
*, size_t);
118 static int check_dir(char *);
119 static mode_t
chech_execf(mode_t
);
120 static int sort_name(const void *const, const void *const);
121 static void get_dirp(char *);
122 static char *get_ext(char *);
123 static int get_fdt(char *, time_t);
124 static char *get_fgrp(gid_t
);
125 static char *get_fperm(mode_t
);
126 static char *get_fsize(off_t
);
127 static char *get_fullpath(char *, char *);
128 static char *get_fusr(uid_t
);
129 static void get_dirsize(char *, off_t
*);
130 static void get_hicol(Cpair
*, mode_t
);
131 static int delent(char *);
132 static void calcdir(void);
133 static void crnd(void);
134 static void crnf(void);
135 static void delfd(void);
136 static void mvbk(void);
137 static void mvbtm(void);
138 static void mvdwn(void);
139 static void mvdwns(void);
140 static void mvfwd(void);
141 static void mvmid(void);
142 static void mvtop(void);
143 static void mvup(void);
144 static void mvups(void);
145 static void scrdwn(void);
146 static void scrdwns(void);
147 static void scrup(void);
148 static void scrups(void);
149 static int get_usrinput(char*, size_t, const char*, ...);
150 static int frules(char *);
151 static int spawn(const void *, char *);
152 static int opnf(char *);
153 static int fsev_init(void);
154 static int addwatch(void);
155 static int read_events(void);
156 static void rmwatch(Pane
*);
157 static void fsev_shdn(void);
158 static ssize_t
findbm(uint32_t);
159 static void start_filter(void);
160 static void start_vmode(void);
161 static void exit_vmode(void);
162 static void selup(void);
163 static void seldwn(void);
164 static void selall(void);
165 static void selref(void);
166 static void selynk(void);
167 static void selcalc(void);
168 static void paste(void);
169 static void selmv(void);
170 static void seldel(void);
171 static void init_files(void);
172 static void free_files(void);
173 static void yank(void);
174 static void rname(void);
175 static void switch_pane(void);
176 static void quit(void);
177 static void grabkeys(struct tb_event
*, Key
*, size_t);
178 static void start_ev(void);
179 static void refresh_pane(void);
180 static void set_direntr(struct dirent
*, DIR *, char *);
181 static int listdir(int);
182 static void t_resize(void);
183 static void set_panes(void);
184 static void draw_frame(void);
185 static void start(void);
187 /* global variables */
188 static Pane pane_r
, pane_l
, *cpane
;
189 static char *editor
[2];
190 static char fed
[] = "vi";
191 static int theight
, twidth
, scrheight
;
192 static size_t selection_size
= 0;
193 static char yank_file
[MAX_P
];
194 static int *selection
;
195 static int cont_vmode
= 0;
196 static char **selected_files
;
197 #if defined _SYS_INOTIFY_H
198 static int inotify_fd
;
199 #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
;
206 /* configuration, allows nested code to access above variables */
209 /* function implementations */
211 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
213 while (*str
!= '\0') {
215 str
+= tb_utf8_char_to_unicode(&uni
, str
);
216 tb_change_cell(x
, y
, uni
, fg
, bg
);
222 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
227 (void)vsnprintf(buf
, MAX_LINE
, fmt
, vl
);
229 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
233 print_status(Cpair col
, const char *fmt
, ...)
235 char buf
[MAX_STATUS
];
238 (void)vsnprintf(buf
, MAX_STATUS
, fmt
, vl
);
241 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
245 print_xstatus(char c
, int x
)
248 (void)tb_utf8_char_to_unicode(&uni
, &c
);
249 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
253 print_error(char *errmsg
)
255 print_status(cerr
, errmsg
);
259 print_prompt(char *prompt
)
261 print_status(cprompt
, prompt
);
265 print_info(char *dirsize
)
267 char *sz
, *ur
, *gr
, *dt
, *prm
;
269 dt
= ecalloc(MAX_DTF
, sizeof(char));
271 prm
= get_fperm(CURSOR
.mode
);
272 ur
= get_fusr(CURSOR
.user
);
273 gr
= get_fgrp(CURSOR
.group
);
275 if (get_fdt(dt
, CURSOR
.dt
) < 0)
278 if (S_ISREG(CURSOR
.mode
)) {
279 sz
= get_fsize(CURSOR
.size
);
281 if (dirsize
== NULL
) {
282 sz
= ecalloc(1, sizeof(char));
289 print_status(cstatus
, "%02d/%02d %s %s:%s %s %s",
290 cpane
->hdir
, cpane
->dirc
, prm
, ur
, gr
, dt
, sz
);
300 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
305 char lnk_full
[MAX_P
];
308 width
= (twidth
/ 2) - 4;
309 result
= basename(pane
->direntr
[entpos
].name
);
311 y
= entpos
- cpane
->firstrow
+ 1;
313 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
314 if (realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
315 (void)snprintf(lnk_full
, MAX_N
,
316 "%s -> %s", result
, buf
);
321 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
325 clear(int sx
, int ex
, int y
, uint16_t bg
)
327 /* clear line from to */
328 /* x = line number vertical */
329 /* y = column number horizontal */
331 for (i
= sx
; i
< ex
; i
++) {
332 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
339 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
346 y
= 0, i
= 0, ex
= 0;
348 if (cpane
->pane_id
== pane_l
.pane_id
)
349 ex
= (twidth
/ 2) - 1;
350 else if (cpane
->pane_id
== pane_r
.pane_id
)
353 while (i
< scrheight
) {
354 clear(cpane
->dirx
, ex
, y
, TB_DEFAULT
);
360 for (y
= cpane
->dirx
; y
< ex
; ++y
) {
361 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
366 add_hi(Pane
*pane
, size_t entpos
)
369 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
370 col
.fg
|= TB_REVERSE
| TB_BOLD
;
371 col
.bg
|= TB_REVERSE
;
372 print_row(pane
, entpos
, col
);
376 rm_hi(Pane
*pane
, size_t entpos
)
379 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
380 print_row(pane
, entpos
, col
);
384 check_dir(char *path
)
390 if (errno
== ENOTDIR
) {
397 if (closedir(dir
) < 0)
404 chech_execf(mode_t mode
)
407 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
412 sort_name(const void *const A
, const void *const B
)
415 mode_t data1
= (*(Entry
*)A
).mode
;
416 mode_t data2
= (*(Entry
*)B
).mode
;
420 } else if (data1
== data2
) {
421 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
434 len
= strnlen(cdir
, MAX_P
);
438 for (i
= len
- 1; i
> 1; i
--) {
445 cdir
[len
-counter
-1] = '\0';
453 size_t counter
, len
, i
;
457 len
= strnlen(str
, MAX_N
);
459 for (i
= len
- 1; i
> 0; i
--) {
467 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
468 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
474 get_fdt(char *result
, time_t status
)
477 localtime_r(&status
, <
);
478 return strftime(result
, MAX_DTF
, dtfmt
, <
);
482 get_fgrp(gid_t status
)
487 result
= ecalloc(MAX_GRPN
, sizeof(char));
488 gr
= getgrgid(status
);
490 (void)snprintf(result
, MAX_GRPN
, "%u", status
);
492 strncpy(result
, gr
->gr_name
, MAX_GRPN
);
494 result
[MAX_GRPN
- 1] = '\0';
499 get_fperm(mode_t mode
)
504 const char chars
[] = "rwxrwxrwx";
505 buf
= ecalloc(11, sizeof(char));
509 else if (S_ISREG(mode
))
511 else if (S_ISLNK(mode
))
513 else if (S_ISBLK(mode
))
515 else if (S_ISCHR(mode
))
517 else if (S_ISFIFO(mode
))
519 else if (S_ISSOCK(mode
))
524 for (i
= 1; i
< 10; i
++) {
525 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
533 get_fsize(off_t size
)
535 char *result
; /* need to be freed */
541 result_len
= 6; /* 9999X/0 */
542 result
= ecalloc(result_len
, sizeof(char));
544 while (size
>= 1000) {
569 if (snprintf(result
, result_len
, "%ld%c", size
, unit
) < 0)
570 strncat(result
, "???", result_len
);
576 get_fullpath(char *first
, char *second
)
580 full_path
= ecalloc(MAX_P
, sizeof(char));
582 if (strncmp(first
, "/", MAX_P
) == 0)
583 (void)snprintf(full_path
, MAX_P
, "/%s", second
);
585 (void)snprintf(full_path
, MAX_P
, "%s/%s", first
, second
);
591 get_fusr(uid_t status
)
596 result
= ecalloc(MAX_USRN
, sizeof(char));
597 pw
= getpwuid(status
);
599 (void)snprintf(result
, MAX_USRN
, "%u", status
);
601 strncpy(result
, pw
->pw_name
, MAX_USRN
);
603 result
[MAX_USRN
- 1] = '\0';
608 get_dirsize(char *fullpath
, off_t
*fullsize
)
613 struct dirent
*entry
;
616 dir
= opendir(fullpath
);
621 while ((entry
= readdir(dir
)) != 0) {
622 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
623 strncmp(entry
->d_name
, "..", 3) == 0))
626 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
627 if (lstat(ent_full
, &status
) == 0) {
628 mode
= status
.st_mode
;
630 get_dirsize(ent_full
, fullsize
);
633 *fullsize
+= status
.st_size
;
644 get_hicol(Cpair
*col
, mode_t mode
)
649 else if (S_ISLNK(mode
))
651 else if (chech_execf(mode
) > 0)
656 delent(char *fullpath
)
662 inp_conf
= ecalloc(conf_len
, sizeof(char));
663 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
664 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
666 return 1; /* canceled by user or wrong inp_conf */
670 return spawn(rm_cmd
, fullpath
);
676 if (!S_ISDIR(CURSOR
.mode
))
682 fullsize
= ecalloc(1, sizeof(off_t
));
683 get_dirsize(CURSOR
.name
, fullsize
);
684 csize
= get_fsize(*fullsize
);
686 CURSOR
.size
= *fullsize
;
694 char *user_input
, *path
;
696 user_input
= ecalloc(MAX_USRI
, sizeof(char));
697 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
702 path
= ecalloc(MAX_P
, sizeof(char));
703 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
709 if (mkdir(path
, ndir_perm
) < 0)
710 print_error(strerror(errno
));
719 char *user_input
, *path
;
722 user_input
= ecalloc(MAX_USRI
, sizeof(char));
723 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
728 path
= ecalloc(MAX_P
, sizeof(char));
729 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
735 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
738 print_error(strerror(errno
));
741 print_error(strerror(errno
));
750 switch (delent(CURSOR
.name
)) {
752 print_error(strerror(errno
));
755 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
764 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
768 get_dirp(cpane
->dirn
);
769 if (check_dir(cpane
->dirn
) < 0) {
770 print_error(strerror(errno
));
775 cpane
->firstrow
= cpane
->parent_firstrow
;
776 cpane
->hdir
= cpane
->parent_row
;
777 if (listdir(AddHi
) < 0)
778 print_error(strerror(errno
));
779 cpane
->parent_firstrow
= 0;
780 cpane
->parent_row
= 1;
788 if (cpane
->dirc
> scrheight
) {
789 rm_hi(cpane
, cpane
->hdir
- 1);
790 cpane
->hdir
= cpane
->dirc
;
791 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
793 add_hi(cpane
, cpane
->hdir
- 1);
795 rm_hi(cpane
, cpane
->hdir
- 1);
796 cpane
->hdir
= cpane
->dirc
;
797 add_hi(cpane
, cpane
->hdir
- 1);
807 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
808 rm_hi(cpane
, cpane
->hdir
- 1);
810 add_hi(cpane
, cpane
->hdir
- 1);
812 mvdwns(); /* scroll */
821 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
823 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
825 rm_hi(cpane
, cpane
->hdir
- 1);
828 add_hi(cpane
, cpane
->hdir
- 1);
829 } else if (cpane
->hdir
< cpane
->dirc
) {
830 rm_hi(cpane
, cpane
->hdir
- 1);
832 add_hi(cpane
, cpane
->hdir
- 1);
844 switch (check_dir(CURSOR
.name
)) {
846 strncpy(cpane
->dirn
, CURSOR
.name
, MAX_P
);
847 cpane
->parent_row
= cpane
->hdir
;
848 cpane
->parent_firstrow
= cpane
->firstrow
;
851 if (listdir(AddHi
) < 0)
852 print_error(strerror(errno
));
854 case 1: /* not a directory open file */
856 s
= opnf(CURSOR
.name
);
861 print_error("process failed non-zero exit");
863 case -1: /* failed to open directory */
864 print_error(strerror(errno
));
873 rm_hi(cpane
, cpane
->hdir
- 1);
874 if (cpane
->dirc
< scrheight
/ 2)
875 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
877 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
878 add_hi(cpane
, cpane
->hdir
- 1);
887 if (cpane
->dirc
> scrheight
) {
888 rm_hi(cpane
, cpane
->hdir
- 1);
892 add_hi(cpane
, cpane
->hdir
- 1);
894 rm_hi(cpane
, cpane
->hdir
- 1);
896 add_hi(cpane
, cpane
->hdir
- 1);
906 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
907 rm_hi(cpane
, cpane
->hdir
- 1);
909 add_hi(cpane
, cpane
->hdir
- 1);
911 mvups(); /* scroll */
920 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
922 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
924 rm_hi(cpane
, cpane
->hdir
- 1);
927 add_hi(cpane
, cpane
->hdir
- 1);
928 } else if (cpane
->hdir
> 1) {
929 rm_hi(cpane
, cpane
->hdir
- 1);
931 add_hi(cpane
, cpane
->hdir
- 1);
940 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
941 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
942 rm_hi(cpane
, cpane
->hdir
- 1);
943 cpane
->hdir
+= scrmv
;
944 add_hi(cpane
, cpane
->hdir
- 1);
959 real
= cpane
->hdir
- cpane
->firstrow
;
960 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
962 if (real
+ scrmv
+ 1 > scrheight
&&
963 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
964 cpane
->firstrow
+= dynmv
;
965 rm_hi(cpane
, cpane
->hdir
- 1);
966 cpane
->hdir
+= scrmv
;
968 add_hi(cpane
, cpane
->hdir
- 1);
970 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
971 rm_hi(cpane
, cpane
->hdir
- 1);
972 cpane
->hdir
+= scrmv
;
973 add_hi(cpane
, cpane
->hdir
- 1);
985 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
986 if (cpane
->hdir
> scrmv
) {
987 rm_hi(cpane
, cpane
->hdir
- 1);
988 cpane
->hdir
= cpane
->hdir
- scrmv
;
989 add_hi(cpane
, cpane
->hdir
- 1);
1003 real
= cpane
->hdir
- cpane
->firstrow
;
1004 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1006 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1007 cpane
->firstrow
-= dynmv
;
1008 rm_hi(cpane
, cpane
->hdir
- 1);
1009 cpane
->hdir
-= scrmv
;
1011 add_hi(cpane
, cpane
->hdir
- 1);
1013 if (cpane
->hdir
> scrmv
+ 1) {
1014 rm_hi(cpane
, cpane
->hdir
- 1);
1015 cpane
->hdir
-= scrmv
;
1016 add_hi(cpane
, cpane
->hdir
- 1);
1024 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
1026 int height
= tb_height();
1028 struct tb_event fev
;
1029 size_t counter
= (size_t)1;
1041 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
1043 print_tb(buf
, 1, height
-1, col
.fg
, col
.bg
);
1044 startat
= name_size
+ 1;
1045 tb_set_cursor((int)(startat
+ 1), height
-1);
1048 while (tb_poll_event(&fev
) != 0) {
1051 if (fev
.key
== TB_KEY_ESC
) {
1052 tb_set_cursor(-1, -1);
1057 if (fev
.key
== TB_KEY_BACKSPACE
||
1058 fev
.key
== TB_KEY_BACKSPACE2
) {
1059 if (BETWEEN(counter
, 2, sout
)) {
1063 print_xstatus(empty
, startat
+ counter
);
1064 tb_set_cursor(startat
+ counter
,
1068 } else if (fev
.key
== TB_KEY_ENTER
) {
1069 tb_set_cursor(-1, -1);
1070 out
[counter
- 1] = '\0';
1074 if (counter
< sout
) {
1075 print_xstatus((char)fev
.ch
,
1076 (startat
+ counter
));
1077 out
[x
] = (char)fev
.ch
;
1078 tb_set_cursor((startat
+ counter
+ 1),
1101 for (c
= 0; c
< LEN(rules
); c
++)
1102 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1103 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1109 spawn(const void *v
, char *fn
)
1111 int ws
, x
, argc
, fd
;
1118 while (((char **)v
)[x
++] != NULL
)
1121 char *argv
[argc
+ 2];
1122 for ( x
= 0; x
< argc
; x
++)
1123 argv
[x
] = ((char **)v
)[x
];
1126 argv
[argc
+ 1] = NULL
;
1133 fd
= open("/dev/null",O_WRONLY
);
1134 dup2(fd
, STDERR_FILENO
);
1135 execvp(argv
[0], argv
);
1139 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1143 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1159 if (c
< 0) /* extension not found open in editor */
1160 return spawn(editor
, fn
);
1162 return spawn((char **)rules
[c
].v
, fn
);
1168 #if defined _SYS_INOTIFY_H
1169 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1172 #elif defined _SYS_EVENT_H_
1183 #if defined _SYS_INOTIFY_H
1184 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1185 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1186 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1187 #elif defined _SYS_EVENT_H_
1188 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1189 if (cpane
->event_fd
< 0)
1190 return cpane
->event_fd
;
1191 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
,
1192 EVFILT_VNODE
, EV_ADD
| EV_CLEAR
,
1193 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
1194 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
, 0, NULL
);
1202 #if defined _SYS_INOTIFY_H
1205 struct inotify_event
*event
;
1206 const size_t events
= 32;
1207 const size_t evbuflen
=
1208 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1211 if (cpane
->inotify_wd
< 0)
1213 r
= read(inotify_fd
, buf
, evbuflen
);
1217 for (p
= buf
; p
< buf
+ r
;) {
1218 event
= (struct inotify_event
*)p
;
1225 p
+= sizeof(struct inotify_event
) + event
->len
;
1227 #elif defined _SYS_EVENT_H_
1228 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1236 #if defined _SYS_INOTIFY_H
1237 if (pane
->inotify_wd
>= 0)
1238 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1239 #elif defined _SYS_EVENT_H_
1240 close(pane
->event_fd
);
1250 #if defined _SYS_INOTIFY_H
1252 #elif defined _SYS_EVENT_H_
1258 findbm(uint32_t event
)
1262 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1263 if (event
== bmarks
[i
].ch
) {
1264 if (check_dir(bmarks
[i
].path
) != 0) {
1265 print_error(strerror(errno
));
1277 if (cpane
->dirc
< 1)
1280 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1281 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1285 cpane
->filter
= user_input
;
1286 if (listdir(AddHi
) < 0)
1287 print_error("no match");
1288 cpane
->filter
= NULL
;
1295 struct tb_event fev
;
1296 if (selection
!= NULL
) {
1301 selection
= ecalloc(cpane
->dirc
, sizeof(size_t));
1302 selection
[0] = cpane
->hdir
;
1304 print_prompt("-- VISUAL --");
1306 while (tb_poll_event(&fev
) != 0) {
1309 grabkeys(&fev
, vkeys
, vkeyslen
);
1310 if (cont_vmode
== -1)
1322 add_hi(cpane
, cpane
->hdir
- 1);
1330 print_prompt("-- VISUAL --");
1331 int index
= abs(cpane
->hdir
- selection
[0]);
1333 if (cpane
->hdir
< selection
[0]) {
1334 selection
[index
] = cpane
->hdir
;
1335 add_hi(cpane
, selection
[index
]);
1336 } else if (index
< cpane
->dirc
) {
1337 selection
[index
+ 1] = 0;
1339 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1348 print_prompt("-- VISUAL --");
1349 int index
= abs(cpane
->hdir
- selection
[0]);
1351 if (cpane
->hdir
> selection
[0]) {
1352 selection
[index
] = cpane
->hdir
;
1353 add_hi(cpane
, selection
[index
] - 2);
1355 selection
[index
+ 1] = 0;
1357 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1366 for (i
= 0; i
< cpane
->dirc
; i
++) {
1367 selection
[i
] = i
+ 1;
1376 for (i
= 0; i
< cpane
->dirc
; i
++) {
1377 if (selection
[i
] < (scrheight
+ cpane
->firstrow
) && selection
[i
] > cpane
->firstrow
) { /* checks if in the frame of the directories */
1378 add_hi(cpane
, selection
[i
] - 1);
1389 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1390 if (selection
[j
] != 0)
1402 if (selected_files
!= NULL
) {
1403 for (i
= 0; i
< selection_size
; i
++) {
1404 free(selected_files
[i
]);
1405 selected_files
[i
] = NULL
;
1407 free(selected_files
);
1408 selected_files
= NULL
;
1419 selected_files
= ecalloc(selection_size
, sizeof(char*));
1421 for (i
= 0; i
< selection_size
; i
++) {
1422 selected_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1423 strncpy(selected_files
[i
], cpane
->direntr
[selection
[i
] -1].name
, MAX_P
); /* TODO use pointer */
1432 add_hi(cpane
, cpane
->hdir
-1);
1433 print_status(cprompt
, "%zu files are yanked", selection_size
);
1442 char conf
[] = "yes";
1445 inp_conf
= ecalloc(conf_len
, sizeof(char));
1446 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
1447 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
1449 return; /* canceled by user or wrong inp_conf */
1454 for (i
= 0; i
< selection_size
; i
++) {
1455 spawn(rm_cmd
, selected_files
[i
]);
1458 cpane
->hdir
= cpane
->dirc
- selection_size
;
1459 print_status(cprompt
, "%zu files are deleted", selection_size
);
1468 if (strnlen(yank_file
, MAX_P
) != 0) {
1469 print_status(cprompt
, "coping");
1470 if (spawn(cp_cmd
, cpane
->dirn
) != 0)
1471 print_error("coping failed");
1473 print_status(cprompt
, "file copied");
1474 yank_file
[0] = '\0'; /* set yank_file len 0 */
1478 print_error("nothing to paste");
1480 if (selected_files
== NULL
)
1483 for (i
= 0; i
< selection_size
; i
++) {
1484 char *selcp_cmd
[] = { "cp", "-r", selected_files
[i
], cpane
->dirn
, NULL
};
1485 spawn(selcp_cmd
,NULL
);
1487 print_status(cprompt
, "%zu files are copied", selection_size
);
1496 if (strnlen(yank_file
, MAX_P
) != 0) {
1497 print_status(cprompt
, "moving");
1498 if (spawn(mv_cmd
, cpane
->dirn
) != 0)
1499 print_error("moving failed");
1501 print_status(cprompt
, "file moved");
1502 yank_file
[0] = '\0'; /* set yank_file len 0 */
1506 print_error("nothing to move");
1508 if (selected_files
== NULL
)
1511 for (i
= 0; i
< selection_size
; i
++) {
1512 char *selmv_cmd
[] = { "mv", selected_files
[i
], cpane
->dirn
, NULL
};
1513 spawn(selmv_cmd
,NULL
);
1515 print_status(cprompt
, "%zu files are moved", selection_size
);
1522 char new_name
[MAX_P
];
1525 input_name
= ecalloc(MAX_N
, sizeof(char));
1527 if (get_usrinput(input_name
, MAX_N
, "rename: %s", basename(CURSOR
.name
)) < 0) {
1532 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1534 print_error(strerror(errno
));
1538 char *rename_cmd
[] = { "mv", CURSOR
.name
, new_name
, NULL
};
1539 if (spawn(rename_cmd
, NULL
) < 0)
1540 print_error(strerror(errno
));
1548 strncpy(yank_file
, CURSOR
.name
, MAX_P
);
1549 print_status(cprompt
, "1 file is yanked", selection_size
);
1556 if (cpane
->dirc
> 0)
1557 rm_hi(cpane
, cpane
->hdir
- 1);
1558 if (cpane
== &pane_l
)
1560 else if (cpane
== &pane_r
)
1562 if (cpane
->dirc
> 0) {
1563 add_hi(cpane
, cpane
->hdir
- 1);
1573 if (cont_vmode
== -1) { /* check if selection was allocated */
1575 if (selected_files
!= NULL
)
1578 free(pane_l
.direntr
);
1579 free(pane_r
.direntr
);
1586 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1591 for (i
= 0; i
< max_keys
; i
++) {
1592 if (event
->ch
!= 0) {
1593 if (event
->ch
== key
[i
].evkey
.ch
) {
1597 } else if (event
->key
!= 0) {
1598 if (event
->key
== key
[i
].evkey
.key
) {
1606 b
= findbm(event
->ch
);
1610 strncpy(cpane
->dirn
, bmarks
[b
].path
, MAX_P
);
1611 cpane
->firstrow
= 0;
1612 cpane
->parent_row
= 1;
1614 if (listdir(AddHi
) < 0)
1615 print_error(strerror(errno
));
1624 int t
= tb_peek_event(&ev
, 2000);
1630 if (t
== 1) /* keyboard event */
1631 grabkeys(&ev
, nkeys
, nkeyslen
);
1632 else if (t
== 2) /* resize event */
1634 else if (t
== 0) /* filesystem event */
1635 if (read_events() > 0) { /* TODO need refactoring */
1636 if (cpane
== &pane_l
) {
1638 if (listdir(NoHi
) < 0)
1639 print_error(strerror(errno
));
1641 if (listdir(AddHi
) < 0)
1642 print_error(strerror(errno
));
1643 } else if (cpane
== &pane_r
) {
1645 if (listdir(NoHi
) < 0)
1646 print_error(strerror(errno
));
1648 if (listdir(AddHi
) < 0)
1649 print_error(strerror(errno
));
1662 size_t y
, dyn_max
, start_from
;
1664 width
= (twidth
/ 2) - 4;
1668 start_from
= cpane
->firstrow
;
1669 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1671 /* print each entry in directory */
1672 while (start_from
< dyn_max
) {
1673 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1674 print_row(cpane
, start_from
, col
);
1679 if (cpane
->dirc
> 0)
1684 /* print current directory title */
1685 cpane
->dircol
.fg
|= TB_BOLD
;
1686 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1690 set_direntr(struct dirent
*entry
, DIR *dir
, char *filter
)
1698 tmpfull = get_fullpath(cpane->dirn, entry->d_name); \
1699 strncpy(cpane->direntr[i].name, tmpfull, MAX_N); \
1700 if (lstat(tmpfull, &status) == 0) { \
1701 cpane->direntr[i].size = status.st_size; \
1702 cpane->direntr[i].mode = status.st_mode; \
1703 cpane->direntr[i].group = status.st_gid; \
1704 cpane->direntr[i].user = status.st_uid; \
1705 cpane->direntr[i].dt = status.st_mtime; \
1709 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1710 while ((entry
= readdir(dir
)) != 0) {
1711 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
1712 strncmp(entry
->d_name
, "..", 3) == 0))
1715 if (filter
== NULL
) {
1717 } else if (filter
!= NULL
) {
1718 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1732 struct dirent
*entry
;
1734 int filtercount
= 0;
1735 size_t oldc
= cpane
->dirc
;
1737 width
= (twidth
/ 2) - 4;
1740 dir
= opendir(cpane
->dirn
);
1744 /* get content and filter sum */
1745 while ((entry
= readdir(dir
)) != 0) {
1746 if (cpane
->filter
!= NULL
) {
1747 if (strcasestr(entry
->d_name
, cpane
->filter
) != NULL
)
1749 } else { /* no filter */
1754 if (cpane
->filter
== NULL
) {
1759 if (cpane
->filter
!= NULL
) {
1760 if (filtercount
> 0) {
1761 cpane
->dirc
= filtercount
;
1764 } else if (filtercount
== 0) {
1765 if (closedir(dir
) < 0)
1772 /* print current directory title */
1773 cpane
->dircol
.fg
|= TB_BOLD
;
1774 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1776 if (cpane
->filter
== NULL
) /* dont't watch when filtering */
1778 print_error("can't add watch");
1780 /* empty directory */
1781 if (cpane
->dirc
== 0) {
1783 if (closedir(dir
) < 0)
1788 rewinddir(dir
); /* reset position */
1789 set_direntr(entry
, dir
, cpane
->filter
); /* create array of entries */
1790 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1794 add_hi(cpane
, cpane
->hdir
- 1);
1796 if (closedir(dir
) < 0)
1804 /* TODO need refactoring */
1807 pane_r
.dirx
= (twidth
/ 2) + 2;
1809 if (cpane
== &pane_l
) {
1814 if (cpane
->dirc
> 0)
1815 add_hi(&pane_l
, pane_l
.hdir
- 1);
1816 } else if (cpane
== &pane_r
) {
1821 if (cpane
->dirc
> 0)
1822 add_hi(&pane_r
, pane_r
.hdir
- 1);
1831 editor
[0] = getenv("EDITOR");
1834 if (editor
[0] == NULL
)
1844 home
= getenv("HOME");
1847 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1848 strncpy(cwd
, home
, MAX_P
);
1852 pane_l
.dircol
= cpanell
;
1853 pane_l
.firstrow
= 0;
1854 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1855 strncpy(pane_l
.dirn
, cwd
, MAX_P
);
1857 pane_l
.inotify_wd
= -1;
1858 pane_l
.parent_row
= 1;
1861 pane_r
.dirx
= (twidth
/ 2) + 2;
1862 pane_r
.dircol
= cpanelr
;
1863 pane_r
.firstrow
= 0;
1864 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1865 strncpy(pane_r
.dirn
, home
, MAX_P
);
1867 pane_r
.inotify_wd
= -1;
1868 pane_r
.parent_row
= 1;
1875 theight
= tb_height();
1876 twidth
= tb_width();
1877 scrheight
= theight
- 2;
1879 /* 2 horizontal lines */
1880 for (i
= 1; i
< twidth
- 1; ++i
) {
1881 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1882 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1885 /* 4 vertical lines */
1886 for (i
= 1; i
< theight
- 1; ++i
) {
1887 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1888 tb_change_cell((twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
,
1890 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1892 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1896 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1897 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1898 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1899 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1901 /* 2 middel top and bottom */
1902 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1903 tb_change_cell((twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1911 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1912 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1913 die("output error");
1918 if (fsev_init() < 0)
1919 print_error(strerror(errno
));
1921 if (listdir(NoHi
) < 0)
1922 print_error(strerror(errno
));
1924 if (listdir(AddHi
) < 0)
1925 print_error(strerror(errno
));
1931 main(int argc
, char *argv
[])
1934 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1937 #endif /* __OpenBSD__ */
1940 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1941 die("sfm-" VERSION
);
1943 die("usage: sfm [-v]");