1 /* See LICENSE file for copyright and license details. */
4 #include <sys/resource.h>
9 #include <sys/inotify.h>
10 #elif defined(__FreeBSD__) || defined(__NetBSD__) ||\
11 defined(__OpenBSD__) || defined(__APPLE__)
12 #include <sys/event.h>
37 #define CURSOR_NAME cpane->direntr[cpane->hdir - 1].name
40 enum { AddHi
, NoHi
}; /* add highlight in listdir */
59 char dirn
[MAX_P
]; // dir name cwd
60 Entry
*direntr
; // dir entries
61 int dirx
; // pane cwd x pos
62 int dirc
; // dir entries sum
63 int hdir
; // highlighted dir
65 int parent_row
; // FIX
84 uint16_t key
; /* one of the TB_KEY_* constants */
85 uint32_t ch
; /* unicode character */
93 /* function declarations */
94 static void print_tb(const char *, int, int, uint16_t, uint16_t);
95 static void printf_tb(int, int, Cpair
, const char *, ...);
96 static void print_status(Cpair
, const char *, ...);
97 static void print_xstatus(char, int);
98 static void print_error(char *);
99 static void print_prompt(char *);
100 static void print_info(void);
101 static void print_row(Pane
*, size_t, Cpair
);
102 static void clear(int, int, int, uint16_t);
103 static void clear_status(void);
104 static void clear_pane(void);
105 static void add_hi(Pane
*, size_t);
106 static void rm_hi(Pane
*, size_t);
107 static void float_to_string(float, char *);
108 static int check_dir(char *);
109 static mode_t
chech_execf(mode_t
);
110 static int sort_name(const void *const, const void *const);
111 static void get_dirp(char *);
112 static char *get_ext(char *);
113 static int get_fdt(char *, size_t, time_t);
114 static char *get_fgrp(gid_t
, size_t);
115 static char *get_finfo(Entry
*);
116 static char *get_fperm(mode_t
);
117 static char *get_fsize(off_t
);
118 static char *get_fullpath(char *, char *);
119 static char *get_fusr(uid_t
, size_t);
120 static void get_dirsize(char *, off_t
*);
121 static void get_hicol(Cpair
*, mode_t
);
122 static int delent(char *);
123 static void calcdir(void);
124 static void crnd(void);
125 static void crnf(void);
126 static void delfd(void);
127 static void mvbk(void);
128 static void mvbtm(void);
129 static void mvdwn(void);
130 static void mvdwns(void);
131 static void mvfor(void);
132 static void mvmid(void);
133 static void mvtop(void);
134 static void mvup(void);
135 static void mvups(void);
136 static void scrdwn(void);
137 static void scrdwns(void);
138 static void scrup(void);
139 static void scrups(void);
140 static int get_usrinput(char*, size_t, const char*, ...);
141 static int frules(char *);
142 static int spawn(const void *, char *);
143 static int opnf(char *);
144 static int fsev_init(void);
145 static int addwatch(void);
146 static int read_events(void);
147 static void rmwatch(Pane
*);
148 static void fsev_shdn(void);
149 static ssize_t
findbm(uint32_t);
150 static void filter(void);
151 static void selection(void);
152 static void selup(void);
153 static void seldwn(void);
154 static void selynk(void);
155 static void selcan(void);
156 static void selall(void);
157 static void selref(void);
158 static void selcalc(void);
159 static void selpst(void);
160 static void selmv(void);
161 static void seldel(void);
162 static void selrename(void);
163 static char *get_path_hdir(int);
164 static void init_files(void);
165 static void free_files(void);
166 static void yank(void);
167 static void rname(void);
168 static void xrename(char *);
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 *);
175 static int listdir(int, char *);
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;
189 #if defined _SYS_INOTIFY_H
190 static int inotify_fd
;
191 #elif defined _SYS_EVENT_H_
193 struct kevent evlist
[2]; /* events we want to monitor */
194 struct kevent chlist
[2]; /* events that were triggered */
195 static struct timespec gtimeout
;
198 /* configuration, allows nested code to access above variables */
201 /* function implementations */
203 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
205 while (*str
!= '\0') {
207 str
+= tb_utf8_char_to_unicode(&uni
, str
);
208 tb_change_cell(x
, y
, uni
, fg
, bg
);
214 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
219 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
221 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
225 print_status(Cpair col
, const char *fmt
, ...)
230 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
233 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
237 print_xstatus(char c
, int x
)
240 (void)tb_utf8_char_to_unicode(&uni
, &c
);
241 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
245 print_error(char *errmsg
)
247 print_status(cerr
, errmsg
);
251 print_prompt(char *prompt
)
253 print_status(cprompt
, prompt
);
260 fileinfo
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
261 print_status(cstatus
, "%d/%d %s", cpane
->hdir
, cpane
->dirc
, fileinfo
);
266 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
271 char lnk_full
[MAX_P
];
274 width
= (twidth
/ 2) - 4;
275 result
= basename(pane
->direntr
[entpos
].name
);
277 y
= entpos
- cpane
->firstrow
+ 1;
279 if (S_ISLNK(pane
->direntr
[entpos
].mode
) &&
280 realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
281 strncpy(lnk_full
, pane
->direntr
[entpos
].name
, MAX_N
);
282 strcat(lnk_full
, " -> ");
283 strncat(lnk_full
, buf
, MAX_N
);
287 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
291 clear(int sx
, int ex
, int y
, uint16_t bg
)
293 /* clear line from to */
294 /* x = line number vertical */
295 /* y = column number horizontal */
297 for (i
= sx
; i
< ex
; i
++) {
298 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
305 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
312 y
= 0, i
= 0, ex
= 0;
314 if (cpane
->pane_id
== pane_l
.pane_id
)
315 ex
= (twidth
/ 2) - 1;
316 else if (cpane
->pane_id
== pane_r
.pane_id
)
319 while (i
< scrheight
) {
320 clear(cpane
->dirx
, ex
, y
, TB_DEFAULT
);
326 for (y
= cpane
->dirx
; y
< ex
; ++y
) {
327 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
332 add_hi(Pane
*pane
, size_t entpos
)
335 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
336 col
.fg
|= TB_REVERSE
| TB_BOLD
;
337 col
.bg
|= TB_REVERSE
;
338 print_row(pane
, entpos
, col
);
342 rm_hi(Pane
*pane
, size_t entpos
)
345 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
346 print_row(pane
, entpos
, col
);
350 float_to_string(float f
, char *r
)
352 int length
, length2
, i
, number
, position
,
353 tenth
; /* length is size of decimal part, length2 is size of tenth part */
356 f
= (float)(int)(f
* 10) / 10;
363 /* Calculate length2 tenth part */
364 while ((number2
- (float)number
) != 0.0 &&
365 !((number2
- (float)number
) < 0.0)) {
367 number2
= f
* (float)tenth
;
368 number
= (int)number2
;
373 /* Calculate length decimal part */
374 for (length
= (f
> 1.0) ? 0 : 1; f
> 1.0; length
++)
378 length
= length
+ 1 + length2
;
379 number
= (int)number2
;
382 for (i
= length
; i
>= 0; i
--) {
385 else if (i
== (position
))
388 r
[i
] = (char)(number
% 10) + '0';
394 for (i
= length
; i
>= 0; i
--) {
398 r
[i
] = (char)(number
% 10) + '0';
406 check_dir(char *path
)
412 if (errno
== ENOTDIR
) {
419 if (closedir(dir
) < 0)
426 chech_execf(mode_t mode
)
429 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
434 sort_name(const void *const A
, const void *const B
)
437 mode_t data1
= (*(Entry
*)A
).mode
;
438 mode_t data2
= (*(Entry
*)B
).mode
;
442 } else if (data1
== data2
) {
443 result
= strcmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
);
461 for (i
= len
- 1; i
> 1; i
--) {
468 cdir
[len
-counter
-1] = '\0';
476 size_t counter
, len
, i
;
482 for (i
= len
- 1; i
> 0; i
--) {
490 ext
= ecalloc(counter
+ 1, sizeof(char));
491 strncpy(ext
, &str
[len
- counter
], counter
);
496 get_fdt(char *result
, size_t reslen
, time_t status
)
499 localtime_r(&status
, <
);
500 return strftime(result
, reslen
, dtfmt
, <
);
504 get_fgrp(gid_t status
, size_t len
)
509 result
= ecalloc(len
, sizeof(char));
510 gr
= getgrgid(status
);
512 (void)snprintf(result
, len
- 1, "%u", status
);
514 strncpy(result
, gr
->gr_name
, len
- 1);
520 get_finfo(Entry
*cursor
)
522 char *sz
, *rst
, *ur
, *gr
, *td
, *prm
;
523 size_t szlen
, prmlen
, urlen
, grlen
, tdlen
, rstlen
;
527 urlen
= grlen
= tdlen
= 32;
528 rstlen
= szlen
+ prmlen
+ urlen
+ grlen
+ tdlen
;
529 rst
= ecalloc(rstlen
, sizeof(char));
531 if (show_perm
== 1) {
532 prm
= get_fperm(cursor
->mode
);
533 strncpy(rst
, prm
, prmlen
);
539 ur
= get_fusr(cursor
->user
, urlen
);
540 gr
= get_fgrp(cursor
->group
, grlen
);
541 strncat(rst
, ur
, urlen
);
543 strncat(rst
, gr
, grlen
);
550 td
= ecalloc(tdlen
, sizeof(char));
551 if (get_fdt(td
, tdlen
, cursor
->td
) > 0) {
552 strncat(rst
, td
, tdlen
);
558 if (show_size
== 1 && S_ISREG(cursor
->mode
)) {
559 sz
= get_fsize(cursor
->size
);
560 strncat(rst
, sz
, szlen
);
568 get_fperm(mode_t mode
)
573 const char chars
[] = "rwxrwxrwx";
574 buf
= ecalloc(11, sizeof(char));
578 else if (S_ISREG(mode
))
580 else if (S_ISLNK(mode
))
582 else if (S_ISBLK(mode
))
584 else if (S_ISCHR(mode
))
586 else if (S_ISFIFO(mode
))
588 else if (S_ISSOCK(mode
))
593 for (i
= 1; i
< 10; i
++) {
594 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
602 get_fsize(off_t size
)
604 /* need to be freed */
610 Rsize
= ecalloc(10, sizeof(char));
613 while (lsize
>= 1000.0) {
618 float_to_string(lsize
, Rsize
);
642 get_fullpath(char *first
, char *second
)
645 size_t full_path_len
;
647 full_path_len
= strlen(first
) + strlen(second
) + 2;
648 full_path
= ecalloc(full_path_len
, sizeof(char));
650 if (strcmp(first
, "/") == 0) {
651 (void)snprintf(full_path
, full_path_len
, "/%s", second
);
654 (void)snprintf(full_path
, full_path_len
, "%s/%s", first
,
662 get_fusr(uid_t status
, size_t len
)
667 result
= ecalloc(len
, sizeof(char));
668 pw
= getpwuid(status
);
670 (void)snprintf(result
, len
- 1, "%u", status
);
672 strncpy(result
, pw
->pw_name
, len
- 1);
678 get_dirsize(char *fullpath
, off_t
*fullsize
)
683 struct dirent
*entry
;
686 dir
= opendir(fullpath
);
691 while ((entry
= readdir(dir
)) != 0) {
692 if ((strcmp(entry
->d_name
, ".") == 0 ||
693 strcmp(entry
->d_name
, "..") == 0))
696 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
697 if (lstat(ent_full
, &status
) == 0) {
698 mode
= status
.st_mode
;
700 get_dirsize(ent_full
, fullsize
);
703 *fullsize
+= status
.st_size
;
714 get_hicol(Cpair
*col
, mode_t mode
)
719 else if (S_ISLNK(mode
))
721 else if (chech_execf(mode
) > 0)
726 delent(char *fullpath
)
729 char *rm_cmd
[] = { "rm", "-rf", NULL
};
731 confirmation
= ecalloc(2, sizeof(char));
732 if ((get_usrinput(confirmation
, 2, "delete file (Y) ?") < 0) ||
733 (strcmp(confirmation
, "Y") != 0)) {
735 return 1; /* canceled by user or wrong confirmation */
739 return spawn(rm_cmd
, fullpath
);
749 if (S_ISDIR(cpane
->direntr
[cpane
->hdir
- 1].mode
)) {
750 fullsize
= ecalloc(50, sizeof(off_t
));
751 get_dirsize(CURSOR_NAME
, fullsize
);
752 csize
= get_fsize(*fullsize
);
753 result
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
756 print_status(cstatus
, "%d/%d %s%s", cpane
->hdir
, cpane
->dirc
,
767 char *user_input
, *path
;
770 user_input
= ecalloc(MAX_USRI
, sizeof(char));
771 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
776 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
777 path
= ecalloc(pathlen
, sizeof(char));
778 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
784 if (mkdir(path
, ndir_perm
) < 0)
785 print_error(strerror(errno
));
786 else if (listdir(AddHi
, NULL
) < 0)
787 print_error(strerror(errno
));
796 char *user_input
, *path
;
800 user_input
= ecalloc(MAX_USRI
, sizeof(char));
801 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
806 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
807 path
= ecalloc(pathlen
, sizeof(char));
808 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
814 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
817 print_error(strerror(errno
));
820 print_error(strerror(errno
));
821 else if (listdir(AddHi
, NULL
) < 0)
822 print_error(strerror(errno
));
832 switch (delent(CURSOR_NAME
)) {
834 print_error(strerror(errno
));
837 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
839 if (listdir(AddHi
, NULL
) < 0)
840 print_error(strerror(errno
));
848 get_dirp(cpane
->dirn
);
849 if (check_dir(cpane
->dirn
) < 0) {
850 print_error(strerror(errno
));
856 cpane
->hdir
= cpane
->parent_row
;
857 if (listdir(AddHi
, NULL
) < 0)
858 print_error(strerror(errno
));
859 cpane
->parent_row
= 1;
867 if (cpane
->dirc
> scrheight
) {
868 rm_hi(cpane
, cpane
->hdir
- 1);
869 cpane
->hdir
= cpane
->dirc
;
870 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
872 add_hi(cpane
, cpane
->hdir
- 1);
874 rm_hi(cpane
, cpane
->hdir
- 1);
875 cpane
->hdir
= cpane
->dirc
;
876 add_hi(cpane
, cpane
->hdir
- 1);
886 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
887 rm_hi(cpane
, cpane
->hdir
- 1);
889 add_hi(cpane
, cpane
->hdir
- 1);
891 mvdwns(); /* scroll */
900 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
902 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
904 rm_hi(cpane
, cpane
->hdir
- 1);
907 add_hi(cpane
, cpane
->hdir
- 1);
908 } else if (cpane
->hdir
< cpane
->dirc
) {
909 rm_hi(cpane
, cpane
->hdir
- 1);
911 add_hi(cpane
, cpane
->hdir
- 1);
923 switch (check_dir(CURSOR_NAME
)) {
925 strcpy(cpane
->dirn
, CURSOR_NAME
);
926 cpane
->parent_row
= cpane
->hdir
;
929 if (listdir(AddHi
, NULL
) < 0)
930 print_error(strerror(errno
));
932 case 1: /* not a directory open file */
934 s
= opnf(CURSOR_NAME
);
939 print_error("process failed non-zero exit");
941 case -1: /* failed to open directory */
942 print_error(strerror(errno
));
951 rm_hi(cpane
, cpane
->hdir
- 1);
952 if (cpane
->dirc
< scrheight
/ 2)
953 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
955 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
956 add_hi(cpane
, cpane
->hdir
- 1);
965 if (cpane
->dirc
> scrheight
) {
966 rm_hi(cpane
, cpane
->hdir
- 1);
970 add_hi(cpane
, cpane
->hdir
- 1);
972 rm_hi(cpane
, cpane
->hdir
- 1);
974 add_hi(cpane
, cpane
->hdir
- 1);
984 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
985 rm_hi(cpane
, cpane
->hdir
- 1);
987 add_hi(cpane
, cpane
->hdir
- 1);
989 mvups(); /* scroll */
998 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
1000 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
1002 rm_hi(cpane
, cpane
->hdir
- 1);
1005 add_hi(cpane
, cpane
->hdir
- 1);
1006 } else if (cpane
->hdir
> 1) {
1007 rm_hi(cpane
, cpane
->hdir
- 1);
1009 add_hi(cpane
, cpane
->hdir
- 1);
1016 if (cpane
->dirc
< 1)
1018 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
1019 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
1020 rm_hi(cpane
, cpane
->hdir
- 1);
1021 cpane
->hdir
+= scrmv
;
1022 add_hi(cpane
, cpane
->hdir
- 1);
1037 real
= cpane
->hdir
- cpane
->firstrow
;
1038 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
1040 if (real
+ scrmv
+ 1 > scrheight
&&
1041 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
1042 cpane
->firstrow
+= dynmv
;
1043 rm_hi(cpane
, cpane
->hdir
- 1);
1044 cpane
->hdir
+= scrmv
;
1046 add_hi(cpane
, cpane
->hdir
- 1);
1048 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
1049 rm_hi(cpane
, cpane
->hdir
- 1);
1050 cpane
->hdir
+= scrmv
;
1051 add_hi(cpane
, cpane
->hdir
- 1);
1061 if (cpane
->dirc
< 1)
1063 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1064 if (cpane
->hdir
> scrmv
) {
1065 rm_hi(cpane
, cpane
->hdir
- 1);
1066 cpane
->hdir
= cpane
->hdir
- scrmv
;
1067 add_hi(cpane
, cpane
->hdir
- 1);
1081 real
= cpane
->hdir
- cpane
->firstrow
;
1082 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1084 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1085 cpane
->firstrow
-= dynmv
;
1086 rm_hi(cpane
, cpane
->hdir
- 1);
1087 cpane
->hdir
-= scrmv
;
1089 add_hi(cpane
, cpane
->hdir
- 1);
1091 if (cpane
->hdir
> scrmv
+ 1) {
1092 rm_hi(cpane
, cpane
->hdir
- 1);
1093 cpane
->hdir
-= scrmv
;
1094 add_hi(cpane
, cpane
->hdir
- 1);
1102 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
1104 int height
= tb_height();
1106 struct tb_event fev
;
1107 size_t counter
= (size_t)1;
1119 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
1121 print_tb(buf
, 1, height
-1, col
.fg
, col
.bg
);
1122 startat
= name_size
+ 1;
1123 tb_set_cursor((int)(startat
+ 1), height
-1);
1126 while (tb_poll_event(&fev
) != 0) {
1129 if (fev
.key
== TB_KEY_ESC
) {
1130 tb_set_cursor(-1, -1);
1135 if (fev
.key
== TB_KEY_BACKSPACE
||
1136 fev
.key
== TB_KEY_BACKSPACE2
) {
1137 if (BETWEEN(counter
, 2, sout
)) {
1141 print_xstatus(empty
, startat
+ counter
);
1142 tb_set_cursor(startat
+ counter
,
1146 } else if (fev
.key
== TB_KEY_ENTER
) {
1147 tb_set_cursor(-1, -1);
1148 out
[counter
- 1] = '\0';
1152 if (counter
< sout
) {
1153 print_xstatus((char)fev
.ch
,
1154 (startat
+ counter
));
1155 out
[x
] = (char)fev
.ch
;
1156 tb_set_cursor((startat
+ counter
+ 1),
1179 for (c
= 0; c
< LEN(rules
); c
++)
1180 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1181 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1187 spawn(const void *v
, char *fn
)
1196 while (((char **)v
)[x
++] != NULL
)
1199 char *argv
[argc
+ 2];
1200 for ( x
= 0; x
< argc
; x
++)
1201 argv
[x
] = ((char **)v
)[x
];
1204 argv
[argc
+ 1] = NULL
;
1211 execvp(argv
[0], argv
);
1214 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1218 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1234 if (c
< 0) /* extension not found open in editor */
1235 return spawn(editor
, fn
);
1237 return spawn((char **)rules
[c
].v
, fn
);
1243 #if defined _SYS_INOTIFY_H
1244 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1247 #elif defined _SYS_EVENT_H_
1258 #if defined _SYS_INOTIFY_H
1259 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1260 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1261 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1262 #elif defined _SYS_EVENT_H_
1263 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1264 if (cpane
->event_fd
< 0)
1265 return cpane
->event_fd
;
1266 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
,
1267 EVFILT_VNODE
, EV_ADD
| EV_CLEAR
,
1268 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
1269 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
, 0, NULL
);
1277 #if defined _SYS_INOTIFY_H
1280 struct inotify_event
*event
;
1281 const size_t events
= 32;
1282 const size_t evbuflen
=
1283 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1286 if (cpane
->inotify_wd
< 0)
1288 r
= read(inotify_fd
, buf
, evbuflen
);
1292 for (p
= buf
; p
< buf
+ r
;) {
1293 event
= (struct inotify_event
*)p
;
1300 p
+= sizeof(struct inotify_event
) + event
->len
;
1302 #elif defined _SYS_EVENT_H_
1303 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1311 #if defined _SYS_INOTIFY_H
1312 if (pane
->inotify_wd
>= 0)
1313 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1314 #elif defined _SYS_EVENT_H_
1315 close(pane
->event_fd
);
1325 #if defined _SYS_INOTIFY_H
1327 #elif defined _SYS_EVENT_H_
1333 findbm(uint32_t event
)
1337 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1338 if (event
== bmarks
[i
].ch
) {
1339 if (check_dir(bmarks
[i
].path
) != 0) {
1340 print_error(strerror(errno
));
1352 if (cpane
->dirc
< 1)
1355 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1356 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1360 if (listdir(AddHi
, user_input
) < 0)
1361 print_error("no match");
1368 struct tb_event fev
;
1369 if (cpane
->selection
!= NULL
) {
1370 free(cpane
->selection
);
1371 cpane
->selection
= NULL
;
1373 cpane
->selection
= ecalloc(cpane
->dirc
, sizeof(size_t));
1374 cpane
->selection
[0] = cpane
->hdir
;
1375 add_hi(cpane
, cpane
->selection
[0] - 1);
1377 while (tb_poll_event(&fev
) != 0) {
1380 grabkeys(&fev
, skeys
, skeyslen
);
1393 print_prompt("VISUAL");
1394 int index
= abs(cpane
->hdir
- cpane
->selection
[0]);
1396 if (cpane
->hdir
> cpane
->selection
[0]) {
1397 cpane
->selection
[index
] = cpane
->hdir
;
1398 add_hi(cpane
, cpane
->selection
[index
] - 2);
1400 cpane
->selection
[index
+ 1] = 0;
1402 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1411 print_prompt("VISUAL");
1412 int index
= abs(cpane
->hdir
- cpane
->selection
[0]);
1414 if (cpane
->hdir
< cpane
->selection
[0]) {
1415 cpane
->selection
[index
] = cpane
->hdir
;
1416 add_hi(cpane
, cpane
->selection
[index
]);
1417 } else if (index
< cpane
->dirc
) {
1418 cpane
->selection
[index
+ 1] = 0;
1420 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1429 for (i
= 0; i
< cpane
->dirc
; i
++) {
1430 if (cpane
->selection
[i
] < (scrheight
+ cpane
->firstrow
) && cpane
->selection
[i
] > cpane
->firstrow
) { /* checks if in the frame of the directories */
1431 add_hi(cpane
, cpane
->selection
[i
] - 1);
1440 add_hi(cpane
, cpane
->hdir
- 1);
1441 print_prompt("Cancel");
1449 for (i
= 0; i
< cpane
->dirc
; i
++) {
1450 cpane
->selection
[i
] = i
+ 1;
1461 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1462 if (cpane
->selection
[j
] != 0)
1472 for (size_t i
= 0; i
< selection_size
; i
++) {
1487 files
= ecalloc(selection_size
, sizeof(char*));
1489 for (size_t i
= 0; i
< selection_size
; i
++) {
1490 files
[i
] = ecalloc(MAX_P
, sizeof(char));
1491 char *tmp
= get_path_hdir(cpane
->selection
[i
]);
1492 strcpy(files
[i
], tmp
);
1501 if (listdir(AddHi
, NULL
) < 0)
1502 print_error(strerror(errno
));
1503 print_status(cprompt
, "%d files are yanked", selection_size
);
1513 confirmation
= ecalloc((size_t)2, sizeof(char));
1515 confirmation
, (size_t)2,"delete directory (Y) ?") < 0) ||
1516 (strcmp(confirmation
, "Y") != 0)) {
1518 if (listdir(AddHi
, NULL
) < 0)
1519 print_error(strerror(errno
));
1526 for (i
= 0; i
< selection_size
; i
++) {
1527 if (delent(files
[i
]) < 0)
1528 print_error(strerror(errno
));
1531 print_status(cprompt
, "%d files are deleted", selection_size
);
1539 if (files
== NULL
) {
1545 for (i
= 0; i
< selection_size
; i
++) {
1546 char *cp_cmd
[] = { "cp", "-rf", files
[i
], cpane
->dirn
, NULL
};
1547 spawn(cp_cmd
, NULL
);
1551 print_status(cprompt
, "%d files are copied", selection_size
);
1558 if (files
== NULL
) {
1564 for (i
= 0; i
< selection_size
; i
++) {
1565 char *mv_cmd
[] = { "mv", files
[i
], cpane
->dirn
, NULL
};
1566 spawn(mv_cmd
, NULL
);
1570 print_status(cprompt
, "%d files are copied", selection_size
);
1578 for (size_t i
= 0; i
< selection_size
; i
++) {
1581 if (listdir(AddHi
, NULL
) < 0)
1582 print_error(strerror(errno
));
1591 new_name
= ecalloc(MAX_N
, sizeof(char));
1593 if (get_usrinput(new_name
, MAX_N
, "rename: %s", basename(path
)) < 0) {
1597 char *rename_cmd
[] = {"mv", path
, new_name
, NULL
};
1599 if (spawn(rename_cmd
, NULL
) < 0)
1600 print_error(strerror(errno
));
1606 get_path_hdir(int Ndir
)
1609 fullpath
= ecalloc(MAX_P
, sizeof(char));
1610 strcpy(fullpath
, cpane
->dirn
);
1611 strcat(fullpath
, "/");
1612 strcat(fullpath
, cpane
->direntr
[Ndir
-1].name
);
1620 if (cpane
->selection
!= NULL
) {
1621 free(cpane
->selection
);
1622 cpane
->selection
= NULL
;
1624 cpane
->selection
= ecalloc(2, sizeof(size_t));
1625 cpane
->selection
[0] = cpane
->hdir
;
1632 if (cpane
->selection
!= NULL
) {
1633 free(cpane
->selection
);
1634 cpane
->selection
= NULL
;
1636 cpane
->selection
= ecalloc(2, sizeof(size_t));
1637 cpane
->selection
[0] = cpane
->hdir
;
1644 if (cpane
->dirc
> 0)
1645 rm_hi(cpane
, cpane
->hdir
- 1);
1646 if (cpane
== &pane_l
)
1648 else if (cpane
== &pane_r
)
1650 if (cpane
->dirc
> 0) {
1651 add_hi(cpane
, cpane
->hdir
- 1);
1661 if (sl
== -1) { /* check if selection was allocated */
1662 free(cpane
->selection
);
1666 free(pane_l
.direntr
);
1667 free(pane_r
.direntr
);
1674 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1679 for (i
= 0; i
< max_keys
; i
++) {
1680 if (event
->ch
!= 0) {
1681 if (event
->ch
== key
[i
].evkey
.ch
) {
1685 } else if (event
->key
!= 0) {
1686 if (event
->key
== key
[i
].evkey
.key
) {
1694 b
= findbm(event
->ch
);
1698 strcpy(cpane
->dirn
, bmarks
[b
].path
);
1699 cpane
->firstrow
= 0;
1700 cpane
->parent_row
= 1;
1702 if (listdir(AddHi
, NULL
) < 0)
1703 print_error(strerror(errno
));
1712 int t
= tb_peek_event(&ev
, 2000);
1718 if (t
== 1) /* keyboard event */
1719 grabkeys(&ev
, nkeys
, nkeyslen
);
1720 else if (t
== 2) /* resize event */
1722 else if (t
== 0) /* filesystem event */
1723 if (read_events() > 0)
1724 if (listdir(AddHi
, NULL
) < 0)
1725 print_error(strerror(errno
));
1736 size_t y
, dyn_max
, start_from
;
1738 width
= (twidth
/ 2) - 4;
1742 start_from
= cpane
->firstrow
;
1743 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1745 /* print each entry in directory */
1746 while (start_from
< dyn_max
) {
1747 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1748 print_row(cpane
, start_from
, col
);
1753 if (cpane
->dirc
> 0)
1758 /* print current directory title */
1759 cpane
->dircol
.fg
|= TB_BOLD
;
1760 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1764 set_direntr(struct dirent
*entry
, DIR *dir
)
1770 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1771 while ((entry
= readdir(dir
)) != 0) {
1772 if ((strcmp(entry
->d_name
, ".") == 0 ||
1773 strcmp(entry
->d_name
, "..") == 0))
1776 char *full
= get_fullpath(cpane
->dirn
, entry
->d_name
);
1777 strcpy(cpane
->direntr
[i
].name
, full
);
1778 if (lstat(full
, &status
) == 0) {
1779 cpane
->direntr
[i
].size
= status
.st_size
;
1780 cpane
->direntr
[i
].mode
= status
.st_mode
;
1781 cpane
->direntr
[i
].group
= status
.st_gid
;
1782 cpane
->direntr
[i
].user
= status
.st_uid
;
1783 cpane
->direntr
[i
].td
= status
.st_mtime
;
1793 listdir(int hi
, char *filter
)
1796 struct dirent
*entry
;
1799 int filtercount
= 0;
1800 size_t oldc
= cpane
->dirc
;
1802 width
= (twidth
/ 2) - 4;
1806 dir
= opendir(cpane
->dirn
);
1810 /* get content and filter sum */
1811 while ((entry
= readdir(dir
)) != 0) {
1812 if (filter
!= NULL
) {
1813 if (strstr(entry
->d_name
, filter
) != NULL
)
1815 } else { /* no filter */
1820 if (filter
== NULL
) {
1825 if (filter
!= NULL
) {
1826 if (filtercount
> 0) {
1828 cpane
->dirc
= filtercount
;
1831 } else if (filtercount
== 0) {
1832 if (closedir(dir
) < 0)
1839 /* print current directory title */
1840 cpane
->dircol
.fg
|= TB_BOLD
;
1841 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1844 print_error("can't add watch");
1846 /* empty directory */
1847 if (cpane
->dirc
== 0) {
1849 if (closedir(dir
) < 0)
1854 rewinddir(dir
); /* reset position */
1855 set_direntr(entry
, dir
); /* create array of entries */
1856 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1860 add_hi(cpane
, cpane
->hdir
- 1);
1862 if (closedir(dir
) < 0)
1870 /* TODO need refactoring */
1873 pane_r
.dirx
= (twidth
/ 2) + 2;
1875 if (cpane
== &pane_l
) {
1880 if (cpane
->dirc
> 0)
1881 add_hi(&pane_l
, pane_l
.hdir
- 1);
1882 } else if (cpane
== &pane_r
) {
1887 if (cpane
->dirc
> 0)
1888 add_hi(&pane_r
, pane_r
.hdir
- 1);
1897 editor
[0] = getenv("EDITOR");
1900 if (editor
[0] == NULL
)
1910 home
= getenv("HOME");
1913 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1914 strncpy(cwd
, home
, MAX_P
);
1918 pane_l
.dircol
= cpanell
;
1919 pane_l
.firstrow
= 0;
1920 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1921 strcpy(pane_l
.dirn
, cwd
);
1923 pane_l
.inotify_wd
= -1;
1924 pane_l
.parent_row
= 1;
1927 pane_r
.dirx
= (twidth
/ 2) + 2;
1928 pane_r
.dircol
= cpanelr
;
1929 pane_r
.firstrow
= 0;
1930 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1931 strcpy(pane_r
.dirn
, home
);
1933 pane_r
.inotify_wd
= -1;
1934 pane_r
.parent_row
= 1;
1941 theight
= tb_height();
1942 twidth
= tb_width();
1943 scrheight
= theight
- 2;
1945 /* 2 horizontal lines */
1946 for (i
= 1; i
< twidth
- 1; ++i
) {
1947 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1948 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1951 /* 3 vertical lines */
1952 for (i
= 1; i
< theight
- 1; ++i
) {
1953 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1954 tb_change_cell((twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
,
1956 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1960 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1961 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1962 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1963 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1965 /* 2 middel top and bottom */
1966 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1967 tb_change_cell((twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1975 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1976 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1977 die("output error");
1982 if (fsev_init() < 0)
1983 print_error(strerror(errno
));
1985 if (listdir(NoHi
, NULL
) < 0)
1986 print_error(strerror(errno
));
1988 if (listdir(AddHi
, NULL
) < 0)
1989 print_error(strerror(errno
));
1995 main(int argc
, char *argv
[])
1998 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
2001 #endif /* __OpenBSD__ */
2004 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
2005 die("sfm-" VERSION
);
2007 die("usage: sfm [-v]");