1 /* See LICENSE file for copyright and license details. */
7 #include <sys/resource.h>
11 #if defined(__linux__)
12 #include <sys/inotify.h>
13 #elif defined(__FreeBSD__) || defined(__NetBSD__) ||\
14 defined(__OpenBSD__) || defined(__APPLE__)
15 #include <sys/event.h>
40 #define CURSOR_NAME cpane->direntr[cpane->hdir - 1].name
43 enum { AddHi
, NoHi
}; /* add highlight in listdir */
62 char dirn
[MAX_P
]; // dir name cwd
64 Entry
*direntr
; // dir entries
65 int dirx
; // pane cwd x pos
66 int dirc
; // dir entries sum
67 int hdir
; // highlighted dir
70 int parent_row
; // FIX
88 uint16_t key
; /* one of the TB_KEY_* constants */
89 uint32_t ch
; /* unicode character */
97 /* function declarations */
98 static void print_tb(const char *, int, int, uint16_t, uint16_t);
99 static void printf_tb(int, int, Cpair
, const char *, ...);
100 static void print_status(Cpair
, const char *, ...);
101 static void print_xstatus(char, int);
102 static void print_error(char *);
103 static void print_prompt(char *);
104 static void print_info(void);
105 static void print_row(Pane
*, size_t, Cpair
);
106 static void clear(int, int, int, uint16_t);
107 static void clear_status(void);
108 static void clear_pane(void);
109 static void add_hi(Pane
*, size_t);
110 static void rm_hi(Pane
*, size_t);
111 static int check_dir(char *);
112 static mode_t
chech_execf(mode_t
);
113 static int sort_name(const void *const, const void *const);
114 static void get_dirp(char *);
115 static char *get_ext(char *);
116 static int get_fdt(char *, size_t, time_t);
117 static char *get_fgrp(gid_t
, size_t);
118 static char *get_finfo(Entry
*);
119 static char *get_fperm(mode_t
);
120 static char *get_fsize(off_t
);
121 static char *get_fullpath(char *, char *);
122 static char *get_fusr(uid_t
, size_t);
123 static void get_dirsize(char *, off_t
*);
124 static void get_hicol(Cpair
*, mode_t
);
125 static int delent(char *);
126 static void calcdir(void);
127 static void crnd(void);
128 static void crnf(void);
129 static void delfd(void);
130 static void mvbk(void);
131 static void mvbtm(void);
132 static void mvdwn(void);
133 static void mvdwns(void);
134 static void mvfor(void);
135 static void mvmid(void);
136 static void mvtop(void);
137 static void mvup(void);
138 static void mvups(void);
139 static void scrdwn(void);
140 static void scrdwns(void);
141 static void scrup(void);
142 static void scrups(void);
143 static int get_usrinput(char*, size_t, const char*, ...);
144 static int frules(char *);
145 static int spawn(const void *, char *);
146 static int opnf(char *);
147 static int fsev_init(void);
148 static int addwatch(void);
149 static int read_events(void);
150 static void rmwatch(Pane
*);
151 static void fsev_shdn(void);
152 static ssize_t
findbm(uint32_t);
153 static void start_filter(void);
154 static void start_vmode(void);
155 static void exit_vmode(void);
156 static void selup(void);
157 static void seldwn(void);
158 static void selall(void);
159 static void selref(void);
160 static void selynk(void);
161 static void selcalc(void);
162 static void paste(void);
163 static void selmv(void);
164 static void seldel(void);
165 static void init_files(void);
166 static void free_files(void);
167 static void yank(void);
168 static void rname(void);
169 static void switch_pane(void);
170 static void quit(void);
171 static void grabkeys(struct tb_event
*, Key
*, size_t);
172 static void start_ev(void);
173 static void refresh_pane(void);
174 static void set_direntr(struct dirent
*, DIR *, char *);
175 static int listdir(int);
176 static void t_resize(void);
177 static void set_panes(void);
178 static void draw_frame(void);
179 static void start(void);
181 /* global variables */
182 static Pane pane_r
, pane_l
, *cpane
;
183 static char *editor
[2];
184 static char fed
[] = "vi";
185 static int theight
, twidth
, scrheight
;
186 static size_t selection_size
= 0;
187 static char yank_file
[MAX_P
];
188 static int *selection
;
189 static int cont_vmode
= 0;
190 static char **selected_files
;
191 #if defined _SYS_INOTIFY_H
192 static int inotify_fd
;
193 #elif defined _SYS_EVENT_H_
195 struct kevent evlist
[2]; /* events we want to monitor */
196 struct kevent chlist
[2]; /* events that were triggered */
197 static struct timespec gtimeout
;
200 /* configuration, allows nested code to access above variables */
203 /* function implementations */
205 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
207 while (*str
!= '\0') {
209 str
+= tb_utf8_char_to_unicode(&uni
, str
);
210 tb_change_cell(x
, y
, uni
, fg
, bg
);
216 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
221 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
223 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
227 print_status(Cpair col
, const char *fmt
, ...)
232 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
235 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
239 print_xstatus(char c
, int x
)
242 (void)tb_utf8_char_to_unicode(&uni
, &c
);
243 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
247 print_error(char *errmsg
)
249 print_status(cerr
, errmsg
);
253 print_prompt(char *prompt
)
255 print_status(cprompt
, prompt
);
262 fileinfo
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
263 print_status(cstatus
, "%d/%d %s", cpane
->hdir
, cpane
->dirc
, fileinfo
);
268 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
273 char lnk_full
[MAX_P
];
276 width
= (twidth
/ 2) - 4;
277 result
= basename(pane
->direntr
[entpos
].name
);
279 y
= entpos
- cpane
->firstrow
+ 1;
281 if (S_ISLNK(pane
->direntr
[entpos
].mode
) != 0) {
282 if (realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
283 (void)snprintf(lnk_full
, MAX_N
,
284 "%s -> %s", result
, buf
);
289 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
293 clear(int sx
, int ex
, int y
, uint16_t bg
)
295 /* clear line from to */
296 /* x = line number vertical */
297 /* y = column number horizontal */
299 for (i
= sx
; i
< ex
; i
++) {
300 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
307 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
314 y
= 0, i
= 0, ex
= 0;
316 if (cpane
->pane_id
== pane_l
.pane_id
)
317 ex
= (twidth
/ 2) - 1;
318 else if (cpane
->pane_id
== pane_r
.pane_id
)
321 while (i
< scrheight
) {
322 clear(cpane
->dirx
, ex
, y
, TB_DEFAULT
);
328 for (y
= cpane
->dirx
; y
< ex
; ++y
) {
329 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
334 add_hi(Pane
*pane
, size_t entpos
)
337 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
338 col
.fg
|= TB_REVERSE
| TB_BOLD
;
339 col
.bg
|= TB_REVERSE
;
340 print_row(pane
, entpos
, col
);
344 rm_hi(Pane
*pane
, size_t entpos
)
347 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
348 print_row(pane
, entpos
, col
);
352 check_dir(char *path
)
358 if (errno
== ENOTDIR
) {
365 if (closedir(dir
) < 0)
372 chech_execf(mode_t mode
)
375 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
380 sort_name(const void *const A
, const void *const B
)
383 mode_t data1
= (*(Entry
*)A
).mode
;
384 mode_t data2
= (*(Entry
*)B
).mode
;
388 } else if (data1
== data2
) {
389 result
= strncmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
, MAX_N
);
402 len
= strnlen(cdir
, MAX_P
);
406 for (i
= len
- 1; i
> 1; i
--) {
413 cdir
[len
-counter
-1] = '\0';
421 size_t counter
, len
, i
;
425 len
= strnlen(str
, MAX_N
);
427 for (i
= len
- 1; i
> 0; i
--) {
435 ext
= ecalloc(MAX_EXT
+ 1, sizeof(char));
436 strncpy(ext
, &str
[len
- counter
], MAX_EXT
);
442 get_fdt(char *result
, size_t reslen
, time_t status
)
445 localtime_r(&status
, <
);
446 return strftime(result
, reslen
, dtfmt
, <
);
450 get_fgrp(gid_t status
, size_t len
)
455 result
= ecalloc(len
, sizeof(char));
456 gr
= getgrgid(status
);
458 (void)snprintf(result
, len
- 1, "%u", status
);
460 strncpy(result
, gr
->gr_name
, len
- 1);
466 get_finfo(Entry
*cursor
)
468 char *sz
, *rst
, *ur
, *gr
, *td
, *prm
;
469 size_t szlen
, prmlen
, urlen
, grlen
, tdlen
, rstlen
;
473 urlen
= grlen
= tdlen
= 32;
474 rstlen
= szlen
+ prmlen
+ urlen
+ grlen
+ tdlen
;
475 rst
= ecalloc(rstlen
, sizeof(char));
477 if (show_perm
== 1) {
478 prm
= get_fperm(cursor
->mode
);
479 strncpy(rst
, prm
, prmlen
);
485 ur
= get_fusr(cursor
->user
, urlen
);
486 gr
= get_fgrp(cursor
->group
, grlen
);
487 strncat(rst
, ur
, urlen
);
489 strncat(rst
, gr
, grlen
);
496 td
= ecalloc(tdlen
, sizeof(char));
497 if (get_fdt(td
, tdlen
, cursor
->td
) > 0) {
498 strncat(rst
, td
, tdlen
);
504 if (show_size
== 1 && S_ISREG(cursor
->mode
)) {
505 sz
= get_fsize(cursor
->size
);
506 strncat(rst
, sz
, szlen
);
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
, "%ld%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
, size_t len
)
611 result
= ecalloc(len
, sizeof(char));
612 pw
= getpwuid(status
);
614 (void)snprintf(result
, len
- 1, "%u", status
);
616 strncpy(result
, pw
->pw_name
, len
- 1);
622 get_dirsize(char *fullpath
, off_t
*fullsize
)
627 struct dirent
*entry
;
630 dir
= opendir(fullpath
);
635 while ((entry
= readdir(dir
)) != 0) {
636 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
637 strncmp(entry
->d_name
, "..", 3) == 0))
640 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
641 if (lstat(ent_full
, &status
) == 0) {
642 mode
= status
.st_mode
;
644 get_dirsize(ent_full
, fullsize
);
647 *fullsize
+= status
.st_size
;
658 get_hicol(Cpair
*col
, mode_t mode
)
663 else if (S_ISLNK(mode
))
665 else if (chech_execf(mode
) > 0)
670 delent(char *fullpath
)
676 inp_conf
= ecalloc(conf_len
, sizeof(char));
677 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
678 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
680 return 1; /* canceled by user or wrong inp_conf */
684 return spawn(rm_cmd
, fullpath
);
690 if (!S_ISDIR(cpane
->direntr
[cpane
->hdir
- 1].mode
))
697 fullsize
= ecalloc(50, sizeof(off_t
));
698 get_dirsize(CURSOR_NAME
, fullsize
);
699 csize
= get_fsize(*fullsize
);
700 result
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
703 print_status(cstatus
, "%d/%d %s%s",
704 cpane
->hdir
, cpane
->dirc
, result
, csize
);
713 char *user_input
, *path
;
715 user_input
= ecalloc(MAX_USRI
, sizeof(char));
716 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
721 path
= ecalloc(MAX_P
, sizeof(char));
722 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
728 if (mkdir(path
, ndir_perm
) < 0)
729 print_error(strerror(errno
));
738 char *user_input
, *path
;
741 user_input
= ecalloc(MAX_USRI
, sizeof(char));
742 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
747 path
= ecalloc(MAX_P
, sizeof(char));
748 if (snprintf(path
, MAX_P
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
754 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
757 print_error(strerror(errno
));
760 print_error(strerror(errno
));
769 switch (delent(CURSOR_NAME
)) {
771 print_error(strerror(errno
));
774 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
783 if (cpane
->dirn
[0] == '/' && cpane
->dirn
[1] == '\0') { /* cwd = / */
787 get_dirp(cpane
->dirn
);
788 if (check_dir(cpane
->dirn
) < 0) {
789 print_error(strerror(errno
));
794 cpane
->firstrow
= cpane
->parent_firstrow
;
795 cpane
->hdir
= cpane
->parent_row
;
796 if (listdir(AddHi
) < 0)
797 print_error(strerror(errno
));
798 cpane
->parent_firstrow
= 0;
799 cpane
->parent_row
= 1;
807 if (cpane
->dirc
> scrheight
) {
808 rm_hi(cpane
, cpane
->hdir
- 1);
809 cpane
->hdir
= cpane
->dirc
;
810 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
812 add_hi(cpane
, cpane
->hdir
- 1);
814 rm_hi(cpane
, cpane
->hdir
- 1);
815 cpane
->hdir
= cpane
->dirc
;
816 add_hi(cpane
, cpane
->hdir
- 1);
826 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
827 rm_hi(cpane
, cpane
->hdir
- 1);
829 add_hi(cpane
, cpane
->hdir
- 1);
831 mvdwns(); /* scroll */
840 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
842 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
844 rm_hi(cpane
, cpane
->hdir
- 1);
847 add_hi(cpane
, cpane
->hdir
- 1);
848 } else if (cpane
->hdir
< cpane
->dirc
) {
849 rm_hi(cpane
, cpane
->hdir
- 1);
851 add_hi(cpane
, cpane
->hdir
- 1);
863 switch (check_dir(CURSOR_NAME
)) {
865 strncpy(cpane
->dirn
, CURSOR_NAME
, MAX_P
);
866 cpane
->parent_row
= cpane
->hdir
;
867 cpane
->parent_firstrow
= cpane
->firstrow
;
870 if (listdir(AddHi
) < 0)
871 print_error(strerror(errno
));
873 case 1: /* not a directory open file */
875 s
= opnf(CURSOR_NAME
);
880 print_error("process failed non-zero exit");
882 case -1: /* failed to open directory */
883 print_error(strerror(errno
));
892 rm_hi(cpane
, cpane
->hdir
- 1);
893 if (cpane
->dirc
< scrheight
/ 2)
894 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
896 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
897 add_hi(cpane
, cpane
->hdir
- 1);
906 if (cpane
->dirc
> scrheight
) {
907 rm_hi(cpane
, cpane
->hdir
- 1);
911 add_hi(cpane
, cpane
->hdir
- 1);
913 rm_hi(cpane
, cpane
->hdir
- 1);
915 add_hi(cpane
, cpane
->hdir
- 1);
925 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
926 rm_hi(cpane
, cpane
->hdir
- 1);
928 add_hi(cpane
, cpane
->hdir
- 1);
930 mvups(); /* scroll */
939 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
941 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
943 rm_hi(cpane
, cpane
->hdir
- 1);
946 add_hi(cpane
, cpane
->hdir
- 1);
947 } else if (cpane
->hdir
> 1) {
948 rm_hi(cpane
, cpane
->hdir
- 1);
950 add_hi(cpane
, cpane
->hdir
- 1);
959 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
960 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
961 rm_hi(cpane
, cpane
->hdir
- 1);
962 cpane
->hdir
+= scrmv
;
963 add_hi(cpane
, cpane
->hdir
- 1);
978 real
= cpane
->hdir
- cpane
->firstrow
;
979 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
981 if (real
+ scrmv
+ 1 > scrheight
&&
982 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
983 cpane
->firstrow
+= dynmv
;
984 rm_hi(cpane
, cpane
->hdir
- 1);
985 cpane
->hdir
+= scrmv
;
987 add_hi(cpane
, cpane
->hdir
- 1);
989 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
990 rm_hi(cpane
, cpane
->hdir
- 1);
991 cpane
->hdir
+= scrmv
;
992 add_hi(cpane
, cpane
->hdir
- 1);
1002 if (cpane
->dirc
< 1)
1004 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1005 if (cpane
->hdir
> scrmv
) {
1006 rm_hi(cpane
, cpane
->hdir
- 1);
1007 cpane
->hdir
= cpane
->hdir
- scrmv
;
1008 add_hi(cpane
, cpane
->hdir
- 1);
1022 real
= cpane
->hdir
- cpane
->firstrow
;
1023 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1025 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1026 cpane
->firstrow
-= dynmv
;
1027 rm_hi(cpane
, cpane
->hdir
- 1);
1028 cpane
->hdir
-= scrmv
;
1030 add_hi(cpane
, cpane
->hdir
- 1);
1032 if (cpane
->hdir
> scrmv
+ 1) {
1033 rm_hi(cpane
, cpane
->hdir
- 1);
1034 cpane
->hdir
-= scrmv
;
1035 add_hi(cpane
, cpane
->hdir
- 1);
1043 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
1045 int height
= tb_height();
1047 struct tb_event fev
;
1048 size_t counter
= (size_t)1;
1060 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
1062 print_tb(buf
, 1, height
-1, col
.fg
, col
.bg
);
1063 startat
= name_size
+ 1;
1064 tb_set_cursor((int)(startat
+ 1), height
-1);
1067 while (tb_poll_event(&fev
) != 0) {
1070 if (fev
.key
== TB_KEY_ESC
) {
1071 tb_set_cursor(-1, -1);
1076 if (fev
.key
== TB_KEY_BACKSPACE
||
1077 fev
.key
== TB_KEY_BACKSPACE2
) {
1078 if (BETWEEN(counter
, 2, sout
)) {
1082 print_xstatus(empty
, startat
+ counter
);
1083 tb_set_cursor(startat
+ counter
,
1087 } else if (fev
.key
== TB_KEY_ENTER
) {
1088 tb_set_cursor(-1, -1);
1089 out
[counter
- 1] = '\0';
1093 if (counter
< sout
) {
1094 print_xstatus((char)fev
.ch
,
1095 (startat
+ counter
));
1096 out
[x
] = (char)fev
.ch
;
1097 tb_set_cursor((startat
+ counter
+ 1),
1120 for (c
= 0; c
< LEN(rules
); c
++)
1121 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1122 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1128 spawn(const void *v
, char *fn
)
1130 int ws
, x
, argc
, fd
;
1137 while (((char **)v
)[x
++] != NULL
)
1140 char *argv
[argc
+ 2];
1141 for ( x
= 0; x
< argc
; x
++)
1142 argv
[x
] = ((char **)v
)[x
];
1145 argv
[argc
+ 1] = NULL
;
1152 fd
= open("/dev/null",O_WRONLY
);
1153 dup2(fd
, STDERR_FILENO
);
1154 execvp(argv
[0], argv
);
1158 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1162 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1178 if (c
< 0) /* extension not found open in editor */
1179 return spawn(editor
, fn
);
1181 return spawn((char **)rules
[c
].v
, fn
);
1187 #if defined _SYS_INOTIFY_H
1188 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1191 #elif defined _SYS_EVENT_H_
1202 #if defined _SYS_INOTIFY_H
1203 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1204 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1205 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1206 #elif defined _SYS_EVENT_H_
1207 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1208 if (cpane
->event_fd
< 0)
1209 return cpane
->event_fd
;
1210 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
,
1211 EVFILT_VNODE
, EV_ADD
| EV_CLEAR
,
1212 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
1213 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
, 0, NULL
);
1221 #if defined _SYS_INOTIFY_H
1224 struct inotify_event
*event
;
1225 const size_t events
= 32;
1226 const size_t evbuflen
=
1227 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1230 if (cpane
->inotify_wd
< 0)
1232 r
= read(inotify_fd
, buf
, evbuflen
);
1236 for (p
= buf
; p
< buf
+ r
;) {
1237 event
= (struct inotify_event
*)p
;
1244 p
+= sizeof(struct inotify_event
) + event
->len
;
1246 #elif defined _SYS_EVENT_H_
1247 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1255 #if defined _SYS_INOTIFY_H
1256 if (pane
->inotify_wd
>= 0)
1257 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1258 #elif defined _SYS_EVENT_H_
1259 close(pane
->event_fd
);
1269 #if defined _SYS_INOTIFY_H
1271 #elif defined _SYS_EVENT_H_
1277 findbm(uint32_t event
)
1281 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1282 if (event
== bmarks
[i
].ch
) {
1283 if (check_dir(bmarks
[i
].path
) != 0) {
1284 print_error(strerror(errno
));
1296 if (cpane
->dirc
< 1)
1299 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1300 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1304 cpane
->filter
= user_input
;
1305 if (listdir(AddHi
) < 0)
1306 print_error("no match");
1307 cpane
->filter
= NULL
;
1314 struct tb_event fev
;
1315 if (selection
!= NULL
) {
1320 selection
= ecalloc(cpane
->dirc
, sizeof(size_t));
1321 selection
[0] = cpane
->hdir
;
1323 print_prompt("-- VISUAL --");
1325 while (tb_poll_event(&fev
) != 0) {
1328 grabkeys(&fev
, skeys
, skeyslen
);
1329 if (cont_vmode
== -1)
1341 add_hi(cpane
, cpane
->hdir
- 1);
1349 print_prompt("-- VISUAL --");
1350 int index
= abs(cpane
->hdir
- selection
[0]);
1352 if (cpane
->hdir
< selection
[0]) {
1353 selection
[index
] = cpane
->hdir
;
1354 add_hi(cpane
, selection
[index
]);
1355 } else if (index
< cpane
->dirc
) {
1356 selection
[index
+ 1] = 0;
1358 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1367 print_prompt("-- VISUAL --");
1368 int index
= abs(cpane
->hdir
- selection
[0]);
1370 if (cpane
->hdir
> selection
[0]) {
1371 selection
[index
] = cpane
->hdir
;
1372 add_hi(cpane
, selection
[index
] - 2);
1374 selection
[index
+ 1] = 0;
1376 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1385 for (i
= 0; i
< cpane
->dirc
; i
++) {
1386 selection
[i
] = i
+ 1;
1395 for (i
= 0; i
< cpane
->dirc
; i
++) {
1396 if (selection
[i
] < (scrheight
+ cpane
->firstrow
) && selection
[i
] > cpane
->firstrow
) { /* checks if in the frame of the directories */
1397 add_hi(cpane
, selection
[i
] - 1);
1408 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1409 if (selection
[j
] != 0)
1421 if (selected_files
!= NULL
) {
1422 for (i
= 0; i
< selection_size
; i
++) {
1423 free(selected_files
[i
]);
1424 selected_files
[i
] = NULL
;
1426 free(selected_files
);
1427 selected_files
= NULL
;
1438 selected_files
= ecalloc(selection_size
, sizeof(char*));
1440 for (i
= 0; i
< selection_size
; i
++) {
1441 selected_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1442 strncpy(selected_files
[i
], cpane
->direntr
[selection
[i
] -1].name
, MAX_P
); /* TODO use pointer */
1451 add_hi(cpane
, cpane
->hdir
-1);
1452 print_status(cprompt
, "%zu files are yanked", selection_size
);
1461 char conf
[] = "yes";
1464 inp_conf
= ecalloc(conf_len
, sizeof(char));
1465 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
1466 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
1468 return; /* canceled by user or wrong inp_conf */
1473 for (i
= 0; i
< selection_size
; i
++) {
1474 spawn(rm_cmd
, selected_files
[i
]);
1477 cpane
->hdir
= cpane
->dirc
- selection_size
;
1478 print_status(cprompt
, "%zu files are deleted", selection_size
);
1487 if (strnlen(yank_file
, MAX_P
) != 0) {
1488 print_status(cprompt
, "coping");
1489 if (spawn(cp_cmd
, cpane
->dirn
) != 0)
1490 print_error("coping failed");
1492 print_status(cprompt
, "file copied");
1493 yank_file
[0] = '\0'; /* set yank_file len 0 */
1497 print_error("nothing to paste");
1499 if (selected_files
== NULL
)
1502 for (i
= 0; i
< selection_size
; i
++) {
1503 char *selcp_cmd
[] = { "cp", "-r", selected_files
[i
], cpane
->dirn
, NULL
};
1504 spawn(selcp_cmd
,NULL
);
1506 print_status(cprompt
, "%zu files are copied", selection_size
);
1515 if (strnlen(yank_file
, MAX_P
) != 0) {
1516 print_status(cprompt
, "moving");
1517 if (spawn(mv_cmd
, cpane
->dirn
) != 0)
1518 print_error("moving failed");
1520 print_status(cprompt
, "file moved");
1521 yank_file
[0] = '\0'; /* set yank_file len 0 */
1525 print_error("nothing to move");
1527 if (selected_files
== NULL
)
1530 for (i
= 0; i
< selection_size
; i
++) {
1531 char *selmv_cmd
[] = { "mv", selected_files
[i
], cpane
->dirn
, NULL
};
1532 spawn(selmv_cmd
,NULL
);
1534 print_status(cprompt
, "%zu files are moved", selection_size
);
1541 char new_name
[MAX_P
];
1544 input_name
= ecalloc(MAX_N
, sizeof(char));
1546 if (get_usrinput(input_name
, MAX_N
, "rename: %s", basename(CURSOR_NAME
)) < 0) {
1551 if (snprintf(new_name
, MAX_P
, "%s/%s", cpane
->dirn
, input_name
) < 0) {
1553 print_error(strerror(errno
));
1557 char *rename_cmd
[] = { "mv", CURSOR_NAME
, new_name
, NULL
};
1558 if (spawn(rename_cmd
, NULL
) < 0)
1559 print_error(strerror(errno
));
1567 strncpy(yank_file
, CURSOR_NAME
, MAX_P
);
1568 print_status(cprompt
, "1 file is yanked", selection_size
);
1575 if (cpane
->dirc
> 0)
1576 rm_hi(cpane
, cpane
->hdir
- 1);
1577 if (cpane
== &pane_l
)
1579 else if (cpane
== &pane_r
)
1581 if (cpane
->dirc
> 0) {
1582 add_hi(cpane
, cpane
->hdir
- 1);
1592 if (cont_vmode
== -1) { /* check if selection was allocated */
1594 if (selected_files
!= NULL
)
1597 free(pane_l
.direntr
);
1598 free(pane_r
.direntr
);
1605 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1610 for (i
= 0; i
< max_keys
; i
++) {
1611 if (event
->ch
!= 0) {
1612 if (event
->ch
== key
[i
].evkey
.ch
) {
1616 } else if (event
->key
!= 0) {
1617 if (event
->key
== key
[i
].evkey
.key
) {
1625 b
= findbm(event
->ch
);
1629 strncpy(cpane
->dirn
, bmarks
[b
].path
, MAX_P
);
1630 cpane
->firstrow
= 0;
1631 cpane
->parent_row
= 1;
1633 if (listdir(AddHi
) < 0)
1634 print_error(strerror(errno
));
1643 int t
= tb_peek_event(&ev
, 2000);
1649 if (t
== 1) /* keyboard event */
1650 grabkeys(&ev
, nkeys
, nkeyslen
);
1651 else if (t
== 2) /* resize event */
1653 else if (t
== 0) /* filesystem event */
1654 if (read_events() > 0) { /* TODO need refactoring */
1655 if (cpane
== &pane_l
) {
1657 if (listdir(NoHi
) < 0)
1658 print_error(strerror(errno
));
1660 if (listdir(AddHi
) < 0)
1661 print_error(strerror(errno
));
1662 } else if (cpane
== &pane_r
) {
1664 if (listdir(NoHi
) < 0)
1665 print_error(strerror(errno
));
1667 if (listdir(AddHi
) < 0)
1668 print_error(strerror(errno
));
1681 size_t y
, dyn_max
, start_from
;
1683 width
= (twidth
/ 2) - 4;
1687 start_from
= cpane
->firstrow
;
1688 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1690 /* print each entry in directory */
1691 while (start_from
< dyn_max
) {
1692 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1693 print_row(cpane
, start_from
, col
);
1698 if (cpane
->dirc
> 0)
1703 /* print current directory title */
1704 cpane
->dircol
.fg
|= TB_BOLD
;
1705 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1709 set_direntr(struct dirent
*entry
, DIR *dir
, char *filter
)
1717 tmpfull = get_fullpath(cpane->dirn, entry->d_name); \
1718 strncpy(cpane->direntr[i].name, tmpfull, MAX_N); \
1719 if (lstat(tmpfull, &status) == 0) { \
1720 cpane->direntr[i].size = status.st_size; \
1721 cpane->direntr[i].mode = status.st_mode; \
1722 cpane->direntr[i].group = status.st_gid; \
1723 cpane->direntr[i].user = status.st_uid; \
1724 cpane->direntr[i].td = status.st_mtime; \
1728 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1729 while ((entry
= readdir(dir
)) != 0) {
1730 if ((strncmp(entry
->d_name
, ".", 2) == 0 ||
1731 strncmp(entry
->d_name
, "..", 3) == 0))
1734 if (filter
== NULL
) {
1736 } else if (filter
!= NULL
) {
1737 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1751 struct dirent
*entry
;
1753 int filtercount
= 0;
1754 size_t oldc
= cpane
->dirc
;
1756 width
= (twidth
/ 2) - 4;
1759 dir
= opendir(cpane
->dirn
);
1763 /* get content and filter sum */
1764 while ((entry
= readdir(dir
)) != 0) {
1765 if (cpane
->filter
!= NULL
) {
1766 if (strcasestr(entry
->d_name
, cpane
->filter
) != NULL
)
1768 } else { /* no filter */
1773 if (cpane
->filter
== NULL
) {
1778 if (cpane
->filter
!= NULL
) {
1779 if (filtercount
> 0) {
1780 cpane
->dirc
= filtercount
;
1783 } else if (filtercount
== 0) {
1784 if (closedir(dir
) < 0)
1791 /* print current directory title */
1792 cpane
->dircol
.fg
|= TB_BOLD
;
1793 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1795 if (cpane
->filter
== NULL
) /* dont't watch when filtering */
1797 print_error("can't add watch");
1799 /* empty directory */
1800 if (cpane
->dirc
== 0) {
1802 if (closedir(dir
) < 0)
1807 rewinddir(dir
); /* reset position */
1808 set_direntr(entry
, dir
, cpane
->filter
); /* create array of entries */
1809 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1813 add_hi(cpane
, cpane
->hdir
- 1);
1815 if (closedir(dir
) < 0)
1823 /* TODO need refactoring */
1826 pane_r
.dirx
= (twidth
/ 2) + 2;
1828 if (cpane
== &pane_l
) {
1833 if (cpane
->dirc
> 0)
1834 add_hi(&pane_l
, pane_l
.hdir
- 1);
1835 } else if (cpane
== &pane_r
) {
1840 if (cpane
->dirc
> 0)
1841 add_hi(&pane_r
, pane_r
.hdir
- 1);
1850 editor
[0] = getenv("EDITOR");
1853 if (editor
[0] == NULL
)
1863 home
= getenv("HOME");
1866 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1867 strncpy(cwd
, home
, MAX_P
);
1871 pane_l
.dircol
= cpanell
;
1872 pane_l
.firstrow
= 0;
1873 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1874 strncpy(pane_l
.dirn
, cwd
, MAX_P
);
1876 pane_l
.inotify_wd
= -1;
1877 pane_l
.parent_row
= 1;
1880 pane_r
.dirx
= (twidth
/ 2) + 2;
1881 pane_r
.dircol
= cpanelr
;
1882 pane_r
.firstrow
= 0;
1883 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1884 strncpy(pane_r
.dirn
, home
, MAX_P
);
1886 pane_r
.inotify_wd
= -1;
1887 pane_r
.parent_row
= 1;
1894 theight
= tb_height();
1895 twidth
= tb_width();
1896 scrheight
= theight
- 2;
1898 /* 2 horizontal lines */
1899 for (i
= 1; i
< twidth
- 1; ++i
) {
1900 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1901 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1904 /* 4 vertical lines */
1905 for (i
= 1; i
< theight
- 1; ++i
) {
1906 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1907 tb_change_cell((twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
,
1909 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1911 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1915 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1916 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1917 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1918 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1920 /* 2 middel top and bottom */
1921 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1922 tb_change_cell((twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1930 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1931 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1932 die("output error");
1937 if (fsev_init() < 0)
1938 print_error(strerror(errno
));
1940 if (listdir(NoHi
) < 0)
1941 print_error(strerror(errno
));
1943 if (listdir(AddHi
) < 0)
1944 print_error(strerror(errno
));
1950 main(int argc
, char *argv
[])
1953 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1956 #endif /* __OpenBSD__ */
1959 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1960 die("sfm-" VERSION
);
1962 die("usage: sfm [-v]");