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>
38 enum { AskDel
, DAskDel
}; /* delete directory */
39 enum { AddHi
, NoHi
}; /* add highlight in listdir */
58 char dirn
[MAX_P
]; // dir name cwd
59 Entry
*direntr
; // dir entries
60 int dirx
; // pane cwd x pos
61 int dirc
; // dir entries sum
62 int hdir
; // highlighted dir
64 int parent_row
; // FIX
82 uint16_t key
; /* one of the TB_KEY_* constants */
83 uint32_t ch
; /* unicode character */
91 /* function declarations */
92 static void print_tb(const char *, int, int, uint16_t, uint16_t);
93 static void printf_tb(int, int, Cpair
, const char *, ...);
94 static void print_status(Cpair
, const char *, ...);
95 static void print_xstatus(char, int);
96 static void print_error(char *);
97 static void print_prompt(char *);
98 static void print_info(void);
99 static void print_row(Pane
*, size_t, Cpair
);
100 static void clear(int, int, int, uint16_t);
101 static void clear_status(void);
102 static void clear_pane(int);
103 static void add_hi(Pane
*, size_t);
104 static void rm_hi(Pane
*, size_t);
105 static void float_to_string(float, char *);
106 static int check_dir(char *);
107 static mode_t
chech_execf(mode_t
);
108 static int sort_name(const void *const, const void *const);
109 static char *get_ext(char *);
110 static int get_fdt(char *, size_t, time_t);
111 static char *get_fgrp(gid_t
, size_t);
112 static char *get_finfo(Entry
*);
113 static char *get_fperm(mode_t
);
114 static char *get_fsize(off_t
);
115 static char *get_fullpath(char *, char *);
116 static char *get_fusr(uid_t
, size_t);
117 static void get_dirsize(char *, off_t
*);
118 static void get_hicol(Cpair
*, mode_t
);
119 static int deldir(char *, int);
120 static int delent(char *);
121 static int delf(char *);
122 static void calcdir(void);
123 static void crnd(void);
124 static void crnf(void);
125 static void delfd(void);
126 static void mvbk(void);
127 static void mvbtm(void);
128 static void mvdwn(void);
129 static void mvdwns(void);
130 static void mvfor(void);
131 static void mvmid(void);
132 static void mvtop(void);
133 static void mvup(void);
134 static void mvups(void);
135 static void scrdwn(void);
136 static void scrdwns(void);
137 static void scrup(void);
138 static void scrups(void);
139 static int get_usrinput(char *, size_t, char *);
140 static int frules(char *);
141 static int spawn(const void *, char *);
142 static int opnf(char *);
143 static int fsev_init(void);
144 static int addwatch(void);
145 static int read_events(void);
146 static void rmwatch(Pane
*);
147 static void fsev_shdn(void);
148 static ssize_t
findbm(uint32_t);
149 static void filter(void);
150 static void switch_pane(void);
151 static void quit(void);
152 static void grabkeys(struct tb_event
*);
153 static void start_ev(void);
154 static void refresh_pane(void);
155 static int listdir(int, char *);
156 static void t_resize(void);
157 static void get_editor(void);
158 static void set_panes(void);
159 static void draw_frame(void);
160 static void start(void);
162 /* global variables */
163 static Pane pane_r
, pane_l
, *cpane
;
164 static char *editor
[2];
165 static char fed
[] = "vi";
166 static int theight
, twidth
, scrheight
;
167 #if defined _SYS_INOTIFY_H
168 static int inotify_fd
;
169 #elif defined _SYS_EVENT_H_
171 struct kevent evlist
[2]; /* events we want to monitor */
172 struct kevent chlist
[2]; /* events that were triggered */
173 static struct timespec gtimeout
;
176 /* configuration, allows nested code to access above variables */
179 /* function implementations */
181 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
183 while (*str
!= '\0') {
185 str
+= tb_utf8_char_to_unicode(&uni
, str
);
186 tb_change_cell(x
, y
, uni
, fg
, bg
);
192 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
197 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
199 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
203 print_status(Cpair col
, const char *fmt
, ...)
208 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
211 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
215 print_xstatus(char c
, int x
)
218 (void)tb_utf8_char_to_unicode(&uni
, &c
);
219 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
223 print_error(char *errmsg
)
225 print_status(cerr
, errmsg
);
229 print_prompt(char *prompt
)
231 print_status(cprompt
, prompt
);
238 fileinfo
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
239 print_status(cstatus
, "%d/%d %s", cpane
->hdir
, cpane
->dirc
, fileinfo
);
244 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
249 char lnk_full
[MAX_P
];
252 width
= (twidth
/ 2) - 4;
253 result
= pane
->direntr
[entpos
].name
;
255 y
= entpos
- cpane
->firstrow
+ 1;
257 if (S_ISLNK(pane
->direntr
[entpos
].mode
) &&
258 realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
259 strncpy(lnk_full
, pane
->direntr
[entpos
].name
, MAX_N
);
260 strcat(lnk_full
, " -> ");
261 strncat(lnk_full
, buf
, MAX_N
);
265 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
269 clear(int sx
, int ex
, int y
, uint16_t bg
)
271 /* clear line from to */
272 /* x = line number vertical */
273 /* y = column number horizontal */
275 for (i
= sx
; i
< ex
; i
++) {
276 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
283 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
290 x
= 0, y
= 0, i
= 0, ex
= 0;
294 ex
= (twidth
/ 2) - 1;
295 } else if (pane
== (twidth
/ 2) + 2) {
296 x
= (twidth
/ 2) + 1;
300 while (i
< scrheight
) {
301 clear(x
, ex
, y
, TB_DEFAULT
);
306 for (y
= x
; y
< ex
; ++y
) {
307 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
312 add_hi(Pane
*pane
, size_t entpos
)
315 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
316 col
.fg
|= TB_REVERSE
| TB_BOLD
;
317 col
.bg
|= TB_REVERSE
;
318 print_row(pane
, entpos
, col
);
322 rm_hi(Pane
*pane
, size_t entpos
)
325 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
326 print_row(pane
, entpos
, col
);
330 float_to_string(float f
, char *r
)
332 int length
, length2
, i
, number
, position
,
333 tenth
; /* length is size of decimal part, length2 is size of tenth part */
336 f
= (float)(int)(f
* 10) / 10;
343 /* Calculate length2 tenth part */
344 while ((number2
- (float)number
) != 0.0 &&
345 !((number2
- (float)number
) < 0.0)) {
347 number2
= f
* (float)tenth
;
348 number
= (int)number2
;
353 /* Calculate length decimal part */
354 for (length
= (f
> 1.0) ? 0 : 1; f
> 1.0; length
++)
358 length
= length
+ 1 + length2
;
359 number
= (int)number2
;
362 for (i
= length
; i
>= 0; i
--) {
365 else if (i
== (position
))
368 r
[i
] = (char)(number
% 10) + '0';
374 for (i
= length
; i
>= 0; i
--) {
378 r
[i
] = (char)(number
% 10) + '0';
386 check_dir(char *path
)
392 if (errno
== ENOTDIR
) {
399 if (closedir(dir
) < 0)
406 chech_execf(mode_t mode
)
409 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
414 sort_name(const void *const A
, const void *const B
)
417 mode_t data1
= (*(Entry
*)A
).mode
;
418 mode_t data2
= (*(Entry
*)B
).mode
;
422 } else if (data1
== data2
) {
423 result
= strcmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
);
435 size_t counter
, len
, i
;
441 for (i
= len
- 1; i
> 0; i
--) {
449 ext
= ecalloc(counter
+ 1, sizeof(char));
450 strncpy(ext
, &str
[len
- counter
], counter
);
455 get_fdt(char *result
, size_t reslen
, time_t status
)
458 localtime_r(&status
, <
);
459 return strftime(result
, reslen
, dtfmt
, <
);
463 get_fgrp(gid_t status
, size_t len
)
468 result
= ecalloc(len
, sizeof(char));
469 gr
= getgrgid(status
);
471 (void)snprintf(result
, len
- 1, "%u", status
);
473 strncpy(result
, gr
->gr_name
, len
- 1);
479 get_finfo(Entry
*cursor
)
481 char *sz
, *rst
, *ur
, *gr
, *td
, *prm
;
482 size_t szlen
, prmlen
, urlen
, grlen
, tdlen
, rstlen
;
486 urlen
= grlen
= tdlen
= 32;
487 rstlen
= szlen
+ prmlen
+ urlen
+ grlen
+ tdlen
;
488 rst
= ecalloc(rstlen
, sizeof(char));
490 if (show_perm
== 1) {
491 prm
= get_fperm(cursor
->mode
);
492 strncpy(rst
, prm
, prmlen
);
498 ur
= get_fusr(cursor
->user
, urlen
);
499 gr
= get_fgrp(cursor
->group
, grlen
);
500 strncat(rst
, ur
, urlen
);
502 strncat(rst
, gr
, grlen
);
509 td
= ecalloc(tdlen
, sizeof(char));
510 if (get_fdt(td
, tdlen
, cursor
->td
) > 0) {
511 strncat(rst
, td
, tdlen
);
517 if (show_size
== 1 && S_ISREG(cursor
->mode
)) {
518 sz
= get_fsize(cursor
->size
);
519 strncat(rst
, sz
, szlen
);
527 get_fperm(mode_t mode
)
532 const char chars
[] = "rwxrwxrwx";
533 buf
= ecalloc(11, sizeof(char));
537 else if (S_ISREG(mode
))
539 else if (S_ISLNK(mode
))
541 else if (S_ISBLK(mode
))
543 else if (S_ISCHR(mode
))
545 else if (S_ISFIFO(mode
))
547 else if (S_ISSOCK(mode
))
552 for (i
= 1; i
< 10; i
++) {
553 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
561 get_fsize(off_t size
)
563 /* need to be freed */
569 Rsize
= ecalloc(10, sizeof(char));
572 while (lsize
>= 1000.0) {
577 float_to_string(lsize
, Rsize
);
601 get_fullpath(char *first
, char *second
)
604 size_t full_path_len
;
606 full_path_len
= strlen(first
) + strlen(second
) + 2;
607 full_path
= ecalloc(full_path_len
, sizeof(char));
609 if (strcmp(first
, "/") == 0) {
610 (void)snprintf(full_path
, full_path_len
, "/%s", second
);
613 (void)snprintf(full_path
, full_path_len
, "%s/%s", first
,
621 get_fusr(uid_t status
, size_t len
)
626 result
= ecalloc(len
, sizeof(char));
627 pw
= getpwuid(status
);
629 (void)snprintf(result
, len
- 1, "%u", status
);
631 strncpy(result
, pw
->pw_name
, len
- 1);
637 get_dirsize(char *fullpath
, off_t
*fullsize
)
642 struct dirent
*entry
;
645 dir
= opendir(fullpath
);
650 while ((entry
= readdir(dir
)) != 0) {
651 if ((strcmp(entry
->d_name
, ".") == 0 ||
652 strcmp(entry
->d_name
, "..") == 0))
655 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
656 if (lstat(ent_full
, &status
) == 0) {
657 mode
= status
.st_mode
;
659 get_dirsize(ent_full
, fullsize
);
662 *fullsize
+= status
.st_size
;
673 get_hicol(Cpair
*col
, mode_t mode
)
678 else if (S_ISLNK(mode
))
680 else if (chech_execf(mode
) > 0)
685 deldir(char *fullpath
, int delchoice
)
687 if (delchoice
== AskDel
) {
689 confirmation
= ecalloc(2, sizeof(char));
690 if ((get_usrinput(confirmation
, 2,
691 "delete directory (Y) ?") < 0) ||
692 (strcmp(confirmation
, "Y") != 0)) {
694 return 1; /* canceled by user or wrong confirmation */
699 if (rmdir(fullpath
) == 0)
700 return 0; /* empty directory */
705 struct dirent
*entry
;
708 dir
= opendir(fullpath
);
713 while ((entry
= readdir(dir
)) != 0) {
714 if ((strcmp(entry
->d_name
, ".") == 0 ||
715 strcmp(entry
->d_name
, "..") == 0))
718 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
719 if (lstat(ent_full
, &status
) == 0) {
720 mode
= status
.st_mode
;
722 if (deldir(ent_full
, DAskDel
) < 0) {
726 } else if (S_ISREG(mode
)) {
727 if (unlink(ent_full
) < 0) {
736 print_status(cstatus
, "gotit");
737 if (closedir(dir
) < 0)
740 return rmdir(fullpath
); /* directory after delete all entries */
744 delent(char *fullpath
)
749 if (lstat(fullpath
, &status
) < 0)
752 mode
= status
.st_mode
;
754 return deldir(fullpath
, AskDel
);
756 return delf(fullpath
);
764 confirmation
= ecalloc(2, sizeof(char));
766 if ((get_usrinput(confirmation
, 2, "delete file (Y) ?") < 0) ||
767 (strcmp(confirmation
, "Y") != 0)) {
769 return 1; /* canceled by user or wrong confirmation */
773 return unlink(fullpath
);
783 if (S_ISDIR(cpane
->direntr
[cpane
->hdir
- 1].mode
)) {
784 fullsize
= ecalloc(50, sizeof(off_t
));
785 get_dirsize(cpane
->direntr
[cpane
->hdir
- 1].name
, fullsize
);
786 csize
= get_fsize(*fullsize
);
787 result
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
790 print_status(cstatus
, "%d/%d %s%s", cpane
->hdir
, cpane
->dirc
,
801 char *user_input
, *path
;
804 user_input
= ecalloc(MAX_USRI
, sizeof(char));
805 if (get_usrinput(user_input
, MAX_USRI
, "new dir") < 0) {
810 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
811 path
= ecalloc(pathlen
, sizeof(char));
812 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
818 if (mkdir(path
, ndir_perm
) < 0)
819 print_error(strerror(errno
));
820 else if (listdir(AddHi
, NULL
) < 0)
821 print_error(strerror(errno
));
830 char *user_input
, *path
;
834 user_input
= ecalloc(MAX_USRI
, sizeof(char));
835 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
840 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
841 path
= ecalloc(pathlen
, sizeof(char));
842 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
848 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
851 print_error(strerror(errno
));
854 print_error(strerror(errno
));
855 else if (listdir(AddHi
, NULL
) < 0)
856 print_error(strerror(errno
));
866 switch (delent(cpane
->direntr
[cpane
->hdir
- 1].name
)) {
868 print_error(strerror(errno
));
871 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
873 if (listdir(AddHi
, NULL
) < 0)
874 print_error(strerror(errno
));
884 getcwd(cpane
->dirn
, MAX_P
);
886 cpane
->hdir
= cpane
->parent_row
;
887 if (listdir(AddHi
, NULL
) < 0)
888 print_error(strerror(errno
));
889 cpane
->parent_row
= 1;
897 if (cpane
->dirc
> scrheight
) {
898 clear_pane(cpane
->dirx
);
899 rm_hi(cpane
, cpane
->hdir
- 1);
900 cpane
->hdir
= cpane
->dirc
;
901 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
903 add_hi(cpane
, cpane
->hdir
- 1);
905 rm_hi(cpane
, cpane
->hdir
- 1);
906 cpane
->hdir
= cpane
->dirc
;
907 add_hi(cpane
, cpane
->hdir
- 1);
917 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
918 rm_hi(cpane
, cpane
->hdir
- 1);
920 add_hi(cpane
, cpane
->hdir
- 1);
922 mvdwns(); /* scroll */
931 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
933 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
935 clear_pane(cpane
->dirx
);
936 rm_hi(cpane
, cpane
->hdir
- 1);
939 add_hi(cpane
, cpane
->hdir
- 1);
940 } else if (cpane
->hdir
< cpane
->dirc
) {
941 rm_hi(cpane
, cpane
->hdir
- 1);
943 add_hi(cpane
, cpane
->hdir
- 1);
955 switch (check_dir(cpane
->direntr
[cpane
->hdir
- 1].name
)) {
957 chdir(cpane
->direntr
[cpane
->hdir
- 1].name
);
958 getcwd(cpane
->dirn
, MAX_P
);
959 cpane
->parent_row
= cpane
->hdir
;
962 if (listdir(AddHi
, NULL
) < 0)
963 print_error(strerror(errno
));
965 case 1: /* not a directory open file */
967 s
= opnf(cpane
->direntr
[cpane
->hdir
- 1].name
);
972 print_error("process failed non-zero exit");
974 case -1: /* failed to open directory */
975 print_error(strerror(errno
));
984 rm_hi(cpane
, cpane
->hdir
- 1);
985 if (cpane
->dirc
< scrheight
/ 2)
986 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
988 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
989 add_hi(cpane
, cpane
->hdir
- 1);
998 if (cpane
->dirc
> scrheight
) {
999 clear_pane(cpane
->dirx
);
1000 rm_hi(cpane
, cpane
->hdir
- 1);
1002 cpane
->firstrow
= 0;
1004 add_hi(cpane
, cpane
->hdir
- 1);
1006 rm_hi(cpane
, cpane
->hdir
- 1);
1008 add_hi(cpane
, cpane
->hdir
- 1);
1016 if (cpane
->dirc
< 1)
1018 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1019 rm_hi(cpane
, cpane
->hdir
- 1);
1021 add_hi(cpane
, cpane
->hdir
- 1);
1023 mvups(); /* scroll */
1032 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
1034 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
1036 clear_pane(cpane
->dirx
);
1037 rm_hi(cpane
, cpane
->hdir
- 1);
1040 add_hi(cpane
, cpane
->hdir
- 1);
1041 } else if (cpane
->hdir
> 1) {
1042 rm_hi(cpane
, cpane
->hdir
- 1);
1044 add_hi(cpane
, cpane
->hdir
- 1);
1051 if (cpane
->dirc
< 1)
1053 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
1054 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
1055 rm_hi(cpane
, cpane
->hdir
- 1);
1056 cpane
->hdir
+= scrmv
;
1057 add_hi(cpane
, cpane
->hdir
- 1);
1072 real
= cpane
->hdir
- cpane
->firstrow
;
1073 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
1075 if (real
+ scrmv
+ 1 > scrheight
&&
1076 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
1077 cpane
->firstrow
+= dynmv
;
1078 clear_pane(cpane
->dirx
);
1079 rm_hi(cpane
, cpane
->hdir
- 1);
1080 cpane
->hdir
+= scrmv
;
1082 add_hi(cpane
, cpane
->hdir
- 1);
1084 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
1085 rm_hi(cpane
, cpane
->hdir
- 1);
1086 cpane
->hdir
+= scrmv
;
1087 add_hi(cpane
, cpane
->hdir
- 1);
1097 if (cpane
->dirc
< 1)
1099 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1100 if (cpane
->hdir
> scrmv
) {
1101 rm_hi(cpane
, cpane
->hdir
- 1);
1102 cpane
->hdir
= cpane
->hdir
- scrmv
;
1103 add_hi(cpane
, cpane
->hdir
- 1);
1117 real
= cpane
->hdir
- cpane
->firstrow
;
1118 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1120 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1121 cpane
->firstrow
-= dynmv
;
1122 clear_pane(cpane
->dirx
);
1123 rm_hi(cpane
, cpane
->hdir
- 1);
1124 cpane
->hdir
-= scrmv
;
1126 add_hi(cpane
, cpane
->hdir
- 1);
1128 if (cpane
->hdir
> scrmv
+ 1) {
1129 rm_hi(cpane
, cpane
->hdir
- 1);
1130 cpane
->hdir
-= scrmv
;
1131 add_hi(cpane
, cpane
->hdir
- 1);
1139 get_usrinput(char *out
, size_t sout
, char *prompt
)
1141 struct tb_event fev
;
1142 size_t startat
, counter
;
1146 startat
= strlen(prompt
) + 1;
1152 print_prompt(prompt
);
1153 tb_set_cursor((startat
+ 1), theight
- 1);
1156 while (tb_poll_event(&fev
) != 0) {
1159 if (fev
.key
== TB_KEY_ESC
) {
1160 tb_set_cursor(-1, -1);
1165 if (fev
.key
== TB_KEY_BACKSPACE
||
1166 fev
.key
== TB_KEY_BACKSPACE2
) {
1167 if (BETWEEN(counter
, 2, sout
)) {
1171 print_xstatus(empty
, startat
+ counter
);
1172 tb_set_cursor(startat
+ counter
,
1176 } else if (fev
.key
== TB_KEY_ENTER
) {
1177 tb_set_cursor(-1, -1);
1178 out
[counter
- 1] = '\0';
1182 if (counter
< sout
) {
1183 print_xstatus((char)fev
.ch
,
1184 (startat
+ counter
));
1185 out
[x
] = (char)fev
.ch
;
1186 tb_set_cursor((startat
+ counter
+ 1),
1209 for (c
= 0; c
< LEN(rules
); c
++)
1210 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1211 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1217 spawn(const void *v
, char *fn
)
1226 while (((char **)v
)[x
++] != NULL
)
1229 char *argv
[argc
+ 2];
1230 for ( x
= 0; x
< argc
; x
++)
1231 argv
[x
] = ((char **)v
)[x
];
1234 argv
[argc
+ 1] = NULL
;
1241 execvp(argv
[0], argv
);
1244 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1248 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1264 if (c
< 0) /* extension not found open in editor */
1265 return spawn(editor
, fn
);
1267 return spawn((char **)rules
[c
].v
, fn
);
1273 #if defined _SYS_INOTIFY_H
1274 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1277 #elif defined _SYS_EVENT_H_
1288 #if defined _SYS_INOTIFY_H
1289 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1290 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1291 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1292 #elif defined _SYS_EVENT_H_
1293 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1294 if (cpane
->event_fd
< 0)
1295 return cpane
->event_fd
;
1296 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
,
1297 EVFILT_VNODE
, EV_ADD
| EV_CLEAR
,
1298 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
1299 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
, 0, NULL
);
1307 #if defined _SYS_INOTIFY_H
1310 struct inotify_event
*event
;
1311 const size_t events
= 32;
1312 const size_t evbuflen
=
1313 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1316 if (cpane
->inotify_wd
< 0)
1318 r
= read(inotify_fd
, buf
, evbuflen
);
1322 for (p
= buf
; p
< buf
+ r
;) {
1323 event
= (struct inotify_event
*)p
;
1330 p
+= sizeof(struct inotify_event
) + event
->len
;
1332 #elif defined _SYS_EVENT_H_
1333 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1341 #if defined _SYS_INOTIFY_H
1342 if (pane
->inotify_wd
>= 0)
1343 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1344 #elif defined _SYS_EVENT_H_
1345 close(pane
->event_fd
);
1355 #if defined _SYS_INOTIFY_H
1357 #elif defined _SYS_EVENT_H_
1363 findbm(uint32_t event
)
1367 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1368 if (event
== bmarks
[i
].ch
) {
1369 if (check_dir(bmarks
[i
].path
) != 0) {
1370 print_error(strerror(errno
));
1382 if (cpane
->dirc
< 1)
1385 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1386 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1390 if (listdir(AddHi
, user_input
) < 0)
1391 print_error("no match");
1398 if (cpane
->dirc
> 0)
1399 rm_hi(cpane
, cpane
->hdir
- 1);
1400 if (cpane
== &pane_l
)
1402 else if (cpane
== &pane_r
)
1405 if (cpane
->dirc
> 0) {
1406 add_hi(cpane
, cpane
->hdir
- 1);
1416 free(pane_l
.direntr
);
1417 free(pane_r
.direntr
);
1424 grabkeys(struct tb_event
*event
)
1429 for (i
= 0; i
< LEN(keys
); i
++) {
1430 if (event
->ch
!= 0) {
1431 if (event
->ch
== keys
[i
].evkey
.ch
) {
1435 } else if (event
->key
!= 0) {
1436 if (event
->key
== keys
[i
].evkey
.key
) {
1444 b
= findbm(event
->ch
);
1448 strcpy(cpane
->dirn
, bmarks
[b
].path
);
1449 cpane
->firstrow
= 0;
1450 cpane
->parent_row
= 1;
1452 if (listdir(AddHi
, NULL
) < 0)
1453 print_error(strerror(errno
));
1462 int t
= tb_peek_event(&ev
, 2000);
1468 if (t
== 1) /* keyboard event */
1470 else if (t
== 2) /* resize event */
1472 else if (t
== 0) /* filesystem event */
1473 if (read_events() > 0)
1474 if (listdir(AddHi
, NULL
) < 0)
1475 print_error(strerror(errno
));
1486 size_t y
, dyn_max
, start_from
;
1488 width
= (twidth
/ 2) - 4;
1492 start_from
= cpane
->firstrow
;
1493 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1495 /* print each entry in directory */
1496 while (start_from
< dyn_max
) {
1497 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1498 print_row(cpane
, start_from
, col
);
1503 if (cpane
->dirc
> 0)
1508 /* print current directory title */
1509 cpane
->dircol
.fg
|= TB_BOLD
;
1510 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1514 listdir(int hi
, char *filter
)
1517 struct dirent
*entry
;
1521 int filtercount
= 0;
1522 size_t oldc
= cpane
->dirc
;
1524 width
= (twidth
/ 2) - 4;
1528 if (chdir(cpane
->dirn
) < 0)
1531 dir
= opendir(cpane
->dirn
);
1535 /* get content and filter sum */
1536 while ((entry
= readdir(dir
)) != 0) {
1537 if (filter
!= NULL
) {
1538 if (strstr(entry
->d_name
, filter
) != NULL
)
1540 } else { /* no filter */
1545 if (filter
== NULL
) {
1546 clear_pane(cpane
->dirx
);
1550 if (filter
!= NULL
) {
1551 if (filtercount
> 0) {
1553 cpane
->dirc
= filtercount
;
1554 clear_pane(cpane
->dirx
);
1556 } else if (filtercount
== 0) {
1557 if (closedir(dir
) < 0)
1564 /* print current directory title */
1565 cpane
->dircol
.fg
|= TB_BOLD
;
1566 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1569 print_error("can't add watch");
1571 /* empty directory */
1572 if (cpane
->dirc
== 0) {
1574 if (closedir(dir
) < 0)
1579 rewinddir(dir
); /* reset position */
1581 /* create array of entries */
1583 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1584 while ((entry
= readdir(dir
)) != 0) {
1585 if ((strcmp(entry
->d_name
, ".") == 0 ||
1586 strcmp(entry
->d_name
, "..") == 0))
1589 /* list found filter */
1590 if (filter
!= NULL
) {
1591 if (strstr(entry
->d_name
, filter
) != NULL
) {
1592 strcpy(cpane
->direntr
[i
].name
, entry
->d_name
);
1593 if (lstat(entry
->d_name
, &status
) == 0) {
1594 cpane
->direntr
[i
].size
= status
.st_size
;
1595 cpane
->direntr
[i
].mode
= status
.st_mode
;
1596 cpane
->direntr
[i
].group
= status
.st_gid
;
1597 cpane
->direntr
[i
].user
= status
.st_uid
;
1598 cpane
->direntr
[i
].td
= status
.st_mtime
;
1604 strcpy(cpane
->direntr
[i
].name
, entry
->d_name
);
1605 if (lstat(entry
->d_name
, &status
) == 0) {
1606 cpane
->direntr
[i
].size
= status
.st_size
;
1607 cpane
->direntr
[i
].mode
= status
.st_mode
;
1608 cpane
->direntr
[i
].group
= status
.st_gid
;
1609 cpane
->direntr
[i
].user
= status
.st_uid
;
1610 cpane
->direntr
[i
].td
= status
.st_mtime
;
1617 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1621 add_hi(cpane
, cpane
->hdir
- 1);
1623 if (closedir(dir
) < 0)
1631 /* TODO need refactoring */
1634 pane_r
.dirx
= (twidth
/ 2) + 2;
1636 if (cpane
== &pane_l
) {
1643 if (cpane
->dirc
> 0)
1644 add_hi(&pane_l
, pane_l
.hdir
- 1);
1645 } else if (cpane
== &pane_r
) {
1652 if (cpane
->dirc
> 0)
1653 add_hi(&pane_r
, pane_r
.hdir
- 1);
1662 editor
[0] = getenv("EDITOR");
1665 if (editor
[0] == NULL
)
1675 home
= getenv("HOME");
1678 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1679 strncpy(cwd
, home
, MAX_P
);
1683 pane_l
.dircol
= cpanell
;
1684 pane_l
.firstrow
= 0;
1685 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1686 strcpy(pane_l
.dirn
, cwd
);
1688 pane_l
.inotify_wd
= -1;
1689 pane_l
.parent_row
= 1;
1692 pane_r
.dirx
= (twidth
/ 2) + 2;
1693 pane_r
.dircol
= cpanelr
;
1694 pane_r
.firstrow
= 0;
1695 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1696 strcpy(pane_r
.dirn
, home
);
1698 pane_r
.inotify_wd
= -1;
1699 pane_r
.parent_row
= 1;
1706 theight
= tb_height();
1707 twidth
= tb_width();
1708 scrheight
= theight
- 2;
1710 /* 2 horizontal lines */
1711 for (i
= 1; i
< twidth
- 1; ++i
) {
1712 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1713 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1716 /* 3 vertical lines */
1717 for (i
= 1; i
< theight
- 1; ++i
) {
1718 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1719 tb_change_cell((twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
,
1721 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1725 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1726 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1727 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1728 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1730 /* 2 middel top and bottom */
1731 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1732 tb_change_cell((twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1740 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1741 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1742 die("output error");
1747 if (fsev_init() < 0)
1748 print_error(strerror(errno
));
1750 if (listdir(NoHi
, NULL
) < 0)
1751 print_error(strerror(errno
));
1753 if (listdir(AddHi
, NULL
) < 0)
1754 print_error(strerror(errno
));
1760 main(int argc
, char *argv
[])
1763 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1766 #endif /* __OpenBSD__ */
1769 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
1770 die("sfm-" VERSION
);
1772 die("usage: sfm [-v]");