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
) &&
282 realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
283 strncpy(lnk_full
, pane
->direntr
[entpos
].name
, MAX_N
);
284 strcat(lnk_full
, " -> ");
285 strncat(lnk_full
, buf
, MAX_N
);
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
= strcmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
);
407 for (i
= len
- 1; i
> 1; i
--) {
414 cdir
[len
-counter
-1] = '\0';
422 size_t counter
, len
, i
;
428 for (i
= len
- 1; i
> 0; i
--) {
436 ext
= ecalloc(counter
+ 1, sizeof(char));
437 strncpy(ext
, &str
[len
- counter
], counter
);
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 */
557 result_len
= 6; /* 9999X/0 */
558 result
= ecalloc(result_len
, sizeof(char));
561 while (lsize
>= 1000) {
586 (void)snprintf(result
, result_len
, "%d%c", lsize
, hsize
);
592 get_fullpath(char *first
, char *second
)
595 size_t full_path_len
;
597 full_path_len
= strlen(first
) + strlen(second
) + 2;
598 full_path
= ecalloc(full_path_len
, sizeof(char));
600 if (strcmp(first
, "/") == 0) {
601 (void)snprintf(full_path
, full_path_len
, "/%s", second
);
604 (void)snprintf(full_path
, full_path_len
, "%s/%s", first
,
612 get_fusr(uid_t status
, size_t len
)
617 result
= ecalloc(len
, sizeof(char));
618 pw
= getpwuid(status
);
620 (void)snprintf(result
, len
- 1, "%u", status
);
622 strncpy(result
, pw
->pw_name
, len
- 1);
628 get_dirsize(char *fullpath
, off_t
*fullsize
)
633 struct dirent
*entry
;
636 dir
= opendir(fullpath
);
641 while ((entry
= readdir(dir
)) != 0) {
642 if ((strcmp(entry
->d_name
, ".") == 0 ||
643 strcmp(entry
->d_name
, "..") == 0))
646 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
647 if (lstat(ent_full
, &status
) == 0) {
648 mode
= status
.st_mode
;
650 get_dirsize(ent_full
, fullsize
);
653 *fullsize
+= status
.st_size
;
664 get_hicol(Cpair
*col
, mode_t mode
)
669 else if (S_ISLNK(mode
))
671 else if (chech_execf(mode
) > 0)
676 delent(char *fullpath
)
682 inp_conf
= ecalloc(conf_len
, sizeof(char));
683 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
684 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
686 return 1; /* canceled by user or wrong inp_conf */
690 return spawn(rm_cmd
, fullpath
);
700 if (S_ISDIR(cpane
->direntr
[cpane
->hdir
- 1].mode
)) {
701 fullsize
= ecalloc(50, sizeof(off_t
));
702 get_dirsize(CURSOR_NAME
, fullsize
);
703 csize
= get_fsize(*fullsize
);
704 result
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
707 print_status(cstatus
, "%d/%d %s%s", cpane
->hdir
, cpane
->dirc
,
718 char *user_input
, *path
;
721 user_input
= ecalloc(MAX_USRI
, sizeof(char));
722 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
727 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
728 path
= ecalloc(pathlen
, sizeof(char));
729 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
735 if (mkdir(path
, ndir_perm
) < 0)
736 print_error(strerror(errno
));
745 char *user_input
, *path
;
749 user_input
= ecalloc(MAX_USRI
, sizeof(char));
750 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
755 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
756 path
= ecalloc(pathlen
, sizeof(char));
757 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
763 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
766 print_error(strerror(errno
));
769 print_error(strerror(errno
));
778 switch (delent(CURSOR_NAME
)) {
780 print_error(strerror(errno
));
783 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
792 get_dirp(cpane
->dirn
);
793 if (check_dir(cpane
->dirn
) < 0) {
794 print_error(strerror(errno
));
799 cpane
->firstrow
= cpane
->parent_firstrow
;
800 cpane
->hdir
= cpane
->parent_row
;
801 if (listdir(AddHi
) < 0)
802 print_error(strerror(errno
));
803 cpane
->parent_firstrow
= 0;
804 cpane
->parent_row
= 1;
812 if (cpane
->dirc
> scrheight
) {
813 rm_hi(cpane
, cpane
->hdir
- 1);
814 cpane
->hdir
= cpane
->dirc
;
815 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
817 add_hi(cpane
, cpane
->hdir
- 1);
819 rm_hi(cpane
, cpane
->hdir
- 1);
820 cpane
->hdir
= cpane
->dirc
;
821 add_hi(cpane
, cpane
->hdir
- 1);
831 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
832 rm_hi(cpane
, cpane
->hdir
- 1);
834 add_hi(cpane
, cpane
->hdir
- 1);
836 mvdwns(); /* scroll */
845 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
847 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
849 rm_hi(cpane
, cpane
->hdir
- 1);
852 add_hi(cpane
, cpane
->hdir
- 1);
853 } else if (cpane
->hdir
< cpane
->dirc
) {
854 rm_hi(cpane
, cpane
->hdir
- 1);
856 add_hi(cpane
, cpane
->hdir
- 1);
868 switch (check_dir(CURSOR_NAME
)) {
870 strcpy(cpane
->dirn
, CURSOR_NAME
);
871 cpane
->parent_row
= cpane
->hdir
;
872 cpane
->parent_firstrow
= cpane
->firstrow
;
875 if (listdir(AddHi
) < 0)
876 print_error(strerror(errno
));
878 case 1: /* not a directory open file */
880 s
= opnf(CURSOR_NAME
);
885 print_error("process failed non-zero exit");
887 case -1: /* failed to open directory */
888 print_error(strerror(errno
));
897 rm_hi(cpane
, cpane
->hdir
- 1);
898 if (cpane
->dirc
< scrheight
/ 2)
899 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
901 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
902 add_hi(cpane
, cpane
->hdir
- 1);
911 if (cpane
->dirc
> scrheight
) {
912 rm_hi(cpane
, cpane
->hdir
- 1);
916 add_hi(cpane
, cpane
->hdir
- 1);
918 rm_hi(cpane
, cpane
->hdir
- 1);
920 add_hi(cpane
, cpane
->hdir
- 1);
930 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
931 rm_hi(cpane
, cpane
->hdir
- 1);
933 add_hi(cpane
, cpane
->hdir
- 1);
935 mvups(); /* scroll */
944 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
946 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
948 rm_hi(cpane
, cpane
->hdir
- 1);
951 add_hi(cpane
, cpane
->hdir
- 1);
952 } else if (cpane
->hdir
> 1) {
953 rm_hi(cpane
, cpane
->hdir
- 1);
955 add_hi(cpane
, cpane
->hdir
- 1);
964 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
965 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
966 rm_hi(cpane
, cpane
->hdir
- 1);
967 cpane
->hdir
+= scrmv
;
968 add_hi(cpane
, cpane
->hdir
- 1);
983 real
= cpane
->hdir
- cpane
->firstrow
;
984 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
986 if (real
+ scrmv
+ 1 > scrheight
&&
987 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
988 cpane
->firstrow
+= dynmv
;
989 rm_hi(cpane
, cpane
->hdir
- 1);
990 cpane
->hdir
+= scrmv
;
992 add_hi(cpane
, cpane
->hdir
- 1);
994 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
995 rm_hi(cpane
, cpane
->hdir
- 1);
996 cpane
->hdir
+= scrmv
;
997 add_hi(cpane
, cpane
->hdir
- 1);
1007 if (cpane
->dirc
< 1)
1009 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1010 if (cpane
->hdir
> scrmv
) {
1011 rm_hi(cpane
, cpane
->hdir
- 1);
1012 cpane
->hdir
= cpane
->hdir
- scrmv
;
1013 add_hi(cpane
, cpane
->hdir
- 1);
1027 real
= cpane
->hdir
- cpane
->firstrow
;
1028 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1030 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1031 cpane
->firstrow
-= dynmv
;
1032 rm_hi(cpane
, cpane
->hdir
- 1);
1033 cpane
->hdir
-= scrmv
;
1035 add_hi(cpane
, cpane
->hdir
- 1);
1037 if (cpane
->hdir
> scrmv
+ 1) {
1038 rm_hi(cpane
, cpane
->hdir
- 1);
1039 cpane
->hdir
-= scrmv
;
1040 add_hi(cpane
, cpane
->hdir
- 1);
1048 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
1050 int height
= tb_height();
1052 struct tb_event fev
;
1053 size_t counter
= (size_t)1;
1065 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
1067 print_tb(buf
, 1, height
-1, col
.fg
, col
.bg
);
1068 startat
= name_size
+ 1;
1069 tb_set_cursor((int)(startat
+ 1), height
-1);
1072 while (tb_poll_event(&fev
) != 0) {
1075 if (fev
.key
== TB_KEY_ESC
) {
1076 tb_set_cursor(-1, -1);
1081 if (fev
.key
== TB_KEY_BACKSPACE
||
1082 fev
.key
== TB_KEY_BACKSPACE2
) {
1083 if (BETWEEN(counter
, 2, sout
)) {
1087 print_xstatus(empty
, startat
+ counter
);
1088 tb_set_cursor(startat
+ counter
,
1092 } else if (fev
.key
== TB_KEY_ENTER
) {
1093 tb_set_cursor(-1, -1);
1094 out
[counter
- 1] = '\0';
1098 if (counter
< sout
) {
1099 print_xstatus((char)fev
.ch
,
1100 (startat
+ counter
));
1101 out
[x
] = (char)fev
.ch
;
1102 tb_set_cursor((startat
+ counter
+ 1),
1125 for (c
= 0; c
< LEN(rules
); c
++)
1126 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1127 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1133 spawn(const void *v
, char *fn
)
1135 int ws
, x
, argc
, fd
;
1142 while (((char **)v
)[x
++] != NULL
)
1145 char *argv
[argc
+ 2];
1146 for ( x
= 0; x
< argc
; x
++)
1147 argv
[x
] = ((char **)v
)[x
];
1150 argv
[argc
+ 1] = NULL
;
1157 fd
= open("/dev/null",O_WRONLY
);
1158 dup2(fd
, STDERR_FILENO
);
1159 execvp(argv
[0], argv
);
1163 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1167 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1183 if (c
< 0) /* extension not found open in editor */
1184 return spawn(editor
, fn
);
1186 return spawn((char **)rules
[c
].v
, fn
);
1192 #if defined _SYS_INOTIFY_H
1193 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1196 #elif defined _SYS_EVENT_H_
1207 #if defined _SYS_INOTIFY_H
1208 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1209 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1210 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1211 #elif defined _SYS_EVENT_H_
1212 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1213 if (cpane
->event_fd
< 0)
1214 return cpane
->event_fd
;
1215 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
,
1216 EVFILT_VNODE
, EV_ADD
| EV_CLEAR
,
1217 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
1218 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
, 0, NULL
);
1226 #if defined _SYS_INOTIFY_H
1229 struct inotify_event
*event
;
1230 const size_t events
= 32;
1231 const size_t evbuflen
=
1232 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1235 if (cpane
->inotify_wd
< 0)
1237 r
= read(inotify_fd
, buf
, evbuflen
);
1241 for (p
= buf
; p
< buf
+ r
;) {
1242 event
= (struct inotify_event
*)p
;
1249 p
+= sizeof(struct inotify_event
) + event
->len
;
1251 #elif defined _SYS_EVENT_H_
1252 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1260 #if defined _SYS_INOTIFY_H
1261 if (pane
->inotify_wd
>= 0)
1262 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1263 #elif defined _SYS_EVENT_H_
1264 close(pane
->event_fd
);
1274 #if defined _SYS_INOTIFY_H
1276 #elif defined _SYS_EVENT_H_
1282 findbm(uint32_t event
)
1286 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1287 if (event
== bmarks
[i
].ch
) {
1288 if (check_dir(bmarks
[i
].path
) != 0) {
1289 print_error(strerror(errno
));
1301 if (cpane
->dirc
< 1)
1304 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1305 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1309 cpane
->filter
= user_input
;
1310 if (listdir(AddHi
) < 0)
1311 print_error("no match");
1312 cpane
->filter
= NULL
;
1319 struct tb_event fev
;
1320 if (selection
!= NULL
) {
1325 selection
= ecalloc(cpane
->dirc
, sizeof(size_t));
1326 selection
[0] = cpane
->hdir
;
1328 print_prompt("-- VISUAL --");
1330 while (tb_poll_event(&fev
) != 0) {
1333 grabkeys(&fev
, skeys
, skeyslen
);
1334 if (cont_vmode
== -1)
1346 add_hi(cpane
, cpane
->hdir
- 1);
1354 print_prompt("-- VISUAL --");
1355 int index
= abs(cpane
->hdir
- selection
[0]);
1357 if (cpane
->hdir
< selection
[0]) {
1358 selection
[index
] = cpane
->hdir
;
1359 add_hi(cpane
, selection
[index
]);
1360 } else if (index
< cpane
->dirc
) {
1361 selection
[index
+ 1] = 0;
1363 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1372 print_prompt("-- VISUAL --");
1373 int index
= abs(cpane
->hdir
- selection
[0]);
1375 if (cpane
->hdir
> selection
[0]) {
1376 selection
[index
] = cpane
->hdir
;
1377 add_hi(cpane
, selection
[index
] - 2);
1379 selection
[index
+ 1] = 0;
1381 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1390 for (i
= 0; i
< cpane
->dirc
; i
++) {
1391 selection
[i
] = i
+ 1;
1400 for (i
= 0; i
< cpane
->dirc
; i
++) {
1401 if (selection
[i
] < (scrheight
+ cpane
->firstrow
) && selection
[i
] > cpane
->firstrow
) { /* checks if in the frame of the directories */
1402 add_hi(cpane
, selection
[i
] - 1);
1413 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1414 if (selection
[j
] != 0)
1426 if (selected_files
!= NULL
) {
1427 for (i
= 0; i
< selection_size
; i
++) {
1428 free(selected_files
[i
]);
1429 selected_files
[i
] = NULL
;
1431 free(selected_files
);
1432 selected_files
= NULL
;
1443 selected_files
= ecalloc(selection_size
, sizeof(char*));
1445 for (i
= 0; i
< selection_size
; i
++) {
1446 selected_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1447 // strcpy(selected_files[i], "\"");
1448 strcpy(selected_files
[i
], cpane
->direntr
[selection
[i
] -1].name
);
1449 // strcat(selected_files[i], "\"");
1458 add_hi(cpane
, cpane
->hdir
-1);
1459 print_status(cprompt
, "%zu files are yanked", selection_size
);
1468 char conf
[] = "yes";
1471 inp_conf
= ecalloc(conf_len
, sizeof(char));
1472 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
1473 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
1475 return; /* canceled by user or wrong inp_conf */
1480 for (i
= 0; i
< selection_size
; i
++) {
1481 spawn(rm_cmd
, selected_files
[i
]);
1484 cpane
->hdir
= cpane
->dirc
- selection_size
;
1485 print_status(cprompt
, "%zu files are deleted", selection_size
);
1494 if (strlen(yank_file
) != 0) {
1495 print_status(cprompt
, "coping");
1496 if (spawn(cp_cmd
, cpane
->dirn
) != 0)
1497 print_error("coping failed");
1499 print_status(cprompt
, "file copied");
1500 yank_file
[0] = '\0'; /* set yank_file len 0 */
1504 print_error("nothing to paste");
1506 if (selected_files
== NULL
)
1509 for (i
= 0; i
< selection_size
; i
++) {
1510 char *selcp_cmd
[] = { "cp", "-r", selected_files
[i
], cpane
->dirn
, NULL
};
1511 spawn(selcp_cmd
,NULL
);
1513 print_status(cprompt
, "%zu files are copied", selection_size
);
1522 if (strlen(yank_file
) != 0) {
1523 print_status(cprompt
, "moving");
1524 if (spawn(mv_cmd
, cpane
->dirn
) != 0)
1525 print_error("moving failed");
1527 print_status(cprompt
, "file moved");
1528 yank_file
[0] = '\0'; /* set yank_file len 0 */
1532 print_error("nothing to move");
1534 if (selected_files
== NULL
)
1537 for (i
= 0; i
< selection_size
; i
++) {
1538 char *selmv_cmd
[] = { "mv", selected_files
[i
], cpane
->dirn
, NULL
};
1539 spawn(selmv_cmd
,NULL
);
1541 print_status(cprompt
, "%zu files are moved", selection_size
);
1548 char new_name
[MAX_P
];
1551 input_name
= ecalloc(MAX_N
, sizeof(char));
1553 if (get_usrinput(input_name
, MAX_N
, "rename: %s", basename(CURSOR_NAME
)) < 0) {
1558 strcpy(new_name
, cpane
->dirn
);
1559 strcat(new_name
, "/");
1560 strcat(new_name
, input_name
);
1562 char *rename_cmd
[] = { "mv", CURSOR_NAME
, new_name
, NULL
};
1563 if (spawn(rename_cmd
, NULL
) < 0)
1564 print_error(strerror(errno
));
1572 strncpy(yank_file
, CURSOR_NAME
, MAX_P
);
1573 print_status(cprompt
, "1 file is yanked", selection_size
);
1580 if (cpane
->dirc
> 0)
1581 rm_hi(cpane
, cpane
->hdir
- 1);
1582 if (cpane
== &pane_l
)
1584 else if (cpane
== &pane_r
)
1586 if (cpane
->dirc
> 0) {
1587 add_hi(cpane
, cpane
->hdir
- 1);
1597 if (cont_vmode
== -1) { /* check if selection was allocated */
1599 if (selected_files
!= NULL
)
1602 free(pane_l
.direntr
);
1603 free(pane_r
.direntr
);
1610 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1615 for (i
= 0; i
< max_keys
; i
++) {
1616 if (event
->ch
!= 0) {
1617 if (event
->ch
== key
[i
].evkey
.ch
) {
1621 } else if (event
->key
!= 0) {
1622 if (event
->key
== key
[i
].evkey
.key
) {
1630 b
= findbm(event
->ch
);
1634 strcpy(cpane
->dirn
, bmarks
[b
].path
);
1635 cpane
->firstrow
= 0;
1636 cpane
->parent_row
= 1;
1638 if (listdir(AddHi
) < 0)
1639 print_error(strerror(errno
));
1648 int t
= tb_peek_event(&ev
, 2000);
1654 if (t
== 1) /* keyboard event */
1655 grabkeys(&ev
, nkeys
, nkeyslen
);
1656 else if (t
== 2) /* resize event */
1658 else if (t
== 0) /* filesystem event */
1659 if (read_events() > 0) { /* TODO need refactoring */
1660 if (cpane
== &pane_l
) {
1662 if (listdir(NoHi
) < 0)
1663 print_error(strerror(errno
));
1665 if (listdir(AddHi
) < 0)
1666 print_error(strerror(errno
));
1667 } else if (cpane
== &pane_r
) {
1669 if (listdir(NoHi
) < 0)
1670 print_error(strerror(errno
));
1672 if (listdir(AddHi
) < 0)
1673 print_error(strerror(errno
));
1686 size_t y
, dyn_max
, start_from
;
1688 width
= (twidth
/ 2) - 4;
1692 start_from
= cpane
->firstrow
;
1693 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1695 /* print each entry in directory */
1696 while (start_from
< dyn_max
) {
1697 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1698 print_row(cpane
, start_from
, col
);
1703 if (cpane
->dirc
> 0)
1708 /* print current directory title */
1709 cpane
->dircol
.fg
|= TB_BOLD
;
1710 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1714 set_direntr(struct dirent
*entry
, DIR *dir
, char *filter
)
1722 tmpfull = get_fullpath(cpane->dirn, entry->d_name); \
1723 strcpy(cpane->direntr[i].name, tmpfull); \
1724 if (lstat(tmpfull, &status) == 0) { \
1725 cpane->direntr[i].size = status.st_size; \
1726 cpane->direntr[i].mode = status.st_mode; \
1727 cpane->direntr[i].group = status.st_gid; \
1728 cpane->direntr[i].user = status.st_uid; \
1729 cpane->direntr[i].td = status.st_mtime; \
1733 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1734 while ((entry
= readdir(dir
)) != 0) {
1735 if ((strcmp(entry
->d_name
, ".") == 0 ||
1736 strcmp(entry
->d_name
, "..") == 0))
1739 if (filter
!= NULL
) {
1740 if (strcasestr(entry
->d_name
, filter
) != NULL
) {
1745 if (filter
== NULL
) {
1757 struct dirent
*entry
;
1759 int filtercount
= 0;
1760 size_t oldc
= cpane
->dirc
;
1762 width
= (twidth
/ 2) - 4;
1765 dir
= opendir(cpane
->dirn
);
1769 /* get content and filter sum */
1770 while ((entry
= readdir(dir
)) != 0) {
1771 if (cpane
->filter
!= NULL
) {
1772 if (strcasestr(entry
->d_name
, cpane
->filter
) != NULL
)
1774 } else { /* no filter */
1779 if (cpane
->filter
== NULL
) {
1784 if (cpane
->filter
!= NULL
) {
1785 if (filtercount
> 0) {
1787 cpane
->dirc
= filtercount
;
1790 } else if (filtercount
== 0) {
1791 if (closedir(dir
) < 0)
1798 /* print current directory title */
1799 cpane
->dircol
.fg
|= TB_BOLD
;
1800 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1802 if (cpane
->filter
== NULL
) /* dont't watch when filtering */
1804 print_error("can't add watch");
1806 /* empty directory */
1807 if (cpane
->dirc
== 0) {
1809 if (closedir(dir
) < 0)
1814 rewinddir(dir
); /* reset position */
1815 set_direntr(entry
, dir
, cpane
->filter
); /* create array of entries */
1816 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1820 add_hi(cpane
, cpane
->hdir
- 1);
1822 if (closedir(dir
) < 0)
1830 /* TODO need refactoring */
1833 pane_r
.dirx
= (twidth
/ 2) + 2;
1835 if (cpane
== &pane_l
) {
1840 if (cpane
->dirc
> 0)
1841 add_hi(&pane_l
, pane_l
.hdir
- 1);
1842 } else if (cpane
== &pane_r
) {
1847 if (cpane
->dirc
> 0)
1848 add_hi(&pane_r
, pane_r
.hdir
- 1);
1857 editor
[0] = getenv("EDITOR");
1860 if (editor
[0] == NULL
)
1870 home
= getenv("HOME");
1873 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1874 strncpy(cwd
, home
, MAX_P
);
1878 pane_l
.dircol
= cpanell
;
1879 pane_l
.firstrow
= 0;
1880 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1881 strcpy(pane_l
.dirn
, cwd
);
1883 pane_l
.inotify_wd
= -1;
1884 pane_l
.parent_row
= 1;
1887 pane_r
.dirx
= (twidth
/ 2) + 2;
1888 pane_r
.dircol
= cpanelr
;
1889 pane_r
.firstrow
= 0;
1890 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1891 strcpy(pane_r
.dirn
, home
);
1893 pane_r
.inotify_wd
= -1;
1894 pane_r
.parent_row
= 1;
1901 theight
= tb_height();
1902 twidth
= tb_width();
1903 scrheight
= theight
- 2;
1905 /* 2 horizontal lines */
1906 for (i
= 1; i
< twidth
- 1; ++i
) {
1907 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1908 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1911 /* 4 vertical lines */
1912 for (i
= 1; i
< theight
- 1; ++i
) {
1913 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1914 tb_change_cell((twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
,
1916 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1918 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1922 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1923 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1924 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1925 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1927 /* 2 middel top and bottom */
1928 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1929 tb_change_cell((twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1937 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1938 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1939 die("output error");
1944 if (fsev_init() < 0)
1945 print_error(strerror(errno
));
1947 if (listdir(NoHi
) < 0)
1948 print_error(strerror(errno
));
1950 if (listdir(AddHi
) < 0)
1951 print_error(strerror(errno
));
1957 main(int argc
, char *argv
[])
1960 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1963 #endif /* __OpenBSD__ */
1966 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1967 die("sfm-" VERSION
);
1969 die("usage: sfm [-v]");