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
66 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 start_vmode(void);
152 static void exit_vmode(void);
153 static void selup(void);
154 static void seldwn(void);
155 static void selall(void);
156 static void selref(void);
157 static void selynk(void);
158 static void selcalc(void);
159 static void paste(void);
160 static void selmv(void);
161 static void seldel(void);
162 static void init_files(void);
163 static void free_files(void);
164 static void yank(void);
165 static void rname(void);
166 static void switch_pane(void);
167 static void quit(void);
168 static void grabkeys(struct tb_event
*, Key
*, size_t);
169 static void start_ev(void);
170 static void refresh_pane(void);
171 static void set_direntr(struct dirent
*, DIR *, char *);
172 static int listdir(int, char *);
173 static void t_resize(void);
174 static void set_panes(void);
175 static void draw_frame(void);
176 static void start(void);
178 /* global variables */
179 static Pane pane_r
, pane_l
, *cpane
;
180 static char *editor
[2];
181 static char fed
[] = "vi";
182 static int theight
, twidth
, scrheight
;
183 static size_t selection_size
= 0;
184 static char yank_file
[MAX_P
];
185 static int *selection
;
186 static int cont_vmode
= 0;
187 static char **selected_files
;
188 #if defined _SYS_INOTIFY_H
189 static int inotify_fd
;
190 #elif defined _SYS_EVENT_H_
192 struct kevent evlist
[2]; /* events we want to monitor */
193 struct kevent chlist
[2]; /* events that were triggered */
194 static struct timespec gtimeout
;
197 /* configuration, allows nested code to access above variables */
200 /* function implementations */
202 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
204 while (*str
!= '\0') {
206 str
+= tb_utf8_char_to_unicode(&uni
, str
);
207 tb_change_cell(x
, y
, uni
, fg
, bg
);
213 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
218 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
220 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
224 print_status(Cpair col
, const char *fmt
, ...)
229 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
232 print_tb(buf
, 1, theight
- 1, col
.fg
, col
.bg
);
236 print_xstatus(char c
, int x
)
239 (void)tb_utf8_char_to_unicode(&uni
, &c
);
240 tb_change_cell(x
, theight
- 1, uni
, cstatus
.fg
, cstatus
.bg
);
244 print_error(char *errmsg
)
246 print_status(cerr
, errmsg
);
250 print_prompt(char *prompt
)
252 print_status(cprompt
, prompt
);
259 fileinfo
= get_finfo(&cpane
->direntr
[cpane
->hdir
- 1]);
260 print_status(cstatus
, "%d/%d %s", cpane
->hdir
, cpane
->dirc
, fileinfo
);
265 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
270 char lnk_full
[MAX_P
];
273 width
= (twidth
/ 2) - 4;
274 result
= basename(pane
->direntr
[entpos
].name
);
276 y
= entpos
- cpane
->firstrow
+ 1;
278 if (S_ISLNK(pane
->direntr
[entpos
].mode
) &&
279 realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
280 strncpy(lnk_full
, pane
->direntr
[entpos
].name
, MAX_N
);
281 strcat(lnk_full
, " -> ");
282 strncat(lnk_full
, buf
, MAX_N
);
286 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
290 clear(int sx
, int ex
, int y
, uint16_t bg
)
292 /* clear line from to */
293 /* x = line number vertical */
294 /* y = column number horizontal */
296 for (i
= sx
; i
< ex
; i
++) {
297 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
304 clear(1, twidth
- 1, theight
- 1, cstatus
.bg
);
311 y
= 0, i
= 0, ex
= 0;
313 if (cpane
->pane_id
== pane_l
.pane_id
)
314 ex
= (twidth
/ 2) - 1;
315 else if (cpane
->pane_id
== pane_r
.pane_id
)
318 while (i
< scrheight
) {
319 clear(cpane
->dirx
, ex
, y
, TB_DEFAULT
);
325 for (y
= cpane
->dirx
; y
< ex
; ++y
) {
326 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
331 add_hi(Pane
*pane
, size_t entpos
)
334 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
335 col
.fg
|= TB_REVERSE
| TB_BOLD
;
336 col
.bg
|= TB_REVERSE
;
337 print_row(pane
, entpos
, col
);
341 rm_hi(Pane
*pane
, size_t entpos
)
344 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
345 print_row(pane
, entpos
, col
);
349 float_to_string(float f
, char *r
)
351 int length
, length2
, i
, number
, position
,
352 tenth
; /* length is size of decimal part, length2 is size of tenth part */
355 f
= (float)(int)(f
* 10) / 10;
362 /* Calculate length2 tenth part */
363 while ((number2
- (float)number
) != 0.0 &&
364 !((number2
- (float)number
) < 0.0)) {
366 number2
= f
* (float)tenth
;
367 number
= (int)number2
;
372 /* Calculate length decimal part */
373 for (length
= (f
> 1.0) ? 0 : 1; f
> 1.0; length
++)
377 length
= length
+ 1 + length2
;
378 number
= (int)number2
;
381 for (i
= length
; i
>= 0; i
--) {
384 else if (i
== (position
))
387 r
[i
] = (char)(number
% 10) + '0';
393 for (i
= length
; i
>= 0; i
--) {
397 r
[i
] = (char)(number
% 10) + '0';
405 check_dir(char *path
)
411 if (errno
== ENOTDIR
) {
418 if (closedir(dir
) < 0)
425 chech_execf(mode_t mode
)
428 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
433 sort_name(const void *const A
, const void *const B
)
436 mode_t data1
= (*(Entry
*)A
).mode
;
437 mode_t data2
= (*(Entry
*)B
).mode
;
441 } else if (data1
== data2
) {
442 result
= strcmp((*(Entry
*)A
).name
, (*(Entry
*)B
).name
);
460 for (i
= len
- 1; i
> 1; i
--) {
467 cdir
[len
-counter
-1] = '\0';
475 size_t counter
, len
, i
;
481 for (i
= len
- 1; i
> 0; i
--) {
489 ext
= ecalloc(counter
+ 1, sizeof(char));
490 strncpy(ext
, &str
[len
- counter
], counter
);
495 get_fdt(char *result
, size_t reslen
, time_t status
)
498 localtime_r(&status
, <
);
499 return strftime(result
, reslen
, dtfmt
, <
);
503 get_fgrp(gid_t status
, size_t len
)
508 result
= ecalloc(len
, sizeof(char));
509 gr
= getgrgid(status
);
511 (void)snprintf(result
, len
- 1, "%u", status
);
513 strncpy(result
, gr
->gr_name
, len
- 1);
519 get_finfo(Entry
*cursor
)
521 char *sz
, *rst
, *ur
, *gr
, *td
, *prm
;
522 size_t szlen
, prmlen
, urlen
, grlen
, tdlen
, rstlen
;
526 urlen
= grlen
= tdlen
= 32;
527 rstlen
= szlen
+ prmlen
+ urlen
+ grlen
+ tdlen
;
528 rst
= ecalloc(rstlen
, sizeof(char));
530 if (show_perm
== 1) {
531 prm
= get_fperm(cursor
->mode
);
532 strncpy(rst
, prm
, prmlen
);
538 ur
= get_fusr(cursor
->user
, urlen
);
539 gr
= get_fgrp(cursor
->group
, grlen
);
540 strncat(rst
, ur
, urlen
);
542 strncat(rst
, gr
, grlen
);
549 td
= ecalloc(tdlen
, sizeof(char));
550 if (get_fdt(td
, tdlen
, cursor
->td
) > 0) {
551 strncat(rst
, td
, tdlen
);
557 if (show_size
== 1 && S_ISREG(cursor
->mode
)) {
558 sz
= get_fsize(cursor
->size
);
559 strncat(rst
, sz
, szlen
);
567 get_fperm(mode_t mode
)
572 const char chars
[] = "rwxrwxrwx";
573 buf
= ecalloc(11, sizeof(char));
577 else if (S_ISREG(mode
))
579 else if (S_ISLNK(mode
))
581 else if (S_ISBLK(mode
))
583 else if (S_ISCHR(mode
))
585 else if (S_ISFIFO(mode
))
587 else if (S_ISSOCK(mode
))
592 for (i
= 1; i
< 10; i
++) {
593 buf
[i
] = (mode
& (1 << (9 - i
))) ? chars
[i
- 1] : '-';
601 get_fsize(off_t size
)
603 /* need to be freed */
609 Rsize
= ecalloc(10, sizeof(char));
612 while (lsize
>= 1000.0) {
617 float_to_string(lsize
, Rsize
);
641 get_fullpath(char *first
, char *second
)
644 size_t full_path_len
;
646 full_path_len
= strlen(first
) + strlen(second
) + 2;
647 full_path
= ecalloc(full_path_len
, sizeof(char));
649 if (strcmp(first
, "/") == 0) {
650 (void)snprintf(full_path
, full_path_len
, "/%s", second
);
653 (void)snprintf(full_path
, full_path_len
, "%s/%s", first
,
661 get_fusr(uid_t status
, size_t len
)
666 result
= ecalloc(len
, sizeof(char));
667 pw
= getpwuid(status
);
669 (void)snprintf(result
, len
- 1, "%u", status
);
671 strncpy(result
, pw
->pw_name
, len
- 1);
677 get_dirsize(char *fullpath
, off_t
*fullsize
)
682 struct dirent
*entry
;
685 dir
= opendir(fullpath
);
690 while ((entry
= readdir(dir
)) != 0) {
691 if ((strcmp(entry
->d_name
, ".") == 0 ||
692 strcmp(entry
->d_name
, "..") == 0))
695 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
696 if (lstat(ent_full
, &status
) == 0) {
697 mode
= status
.st_mode
;
699 get_dirsize(ent_full
, fullsize
);
702 *fullsize
+= status
.st_size
;
713 get_hicol(Cpair
*col
, mode_t mode
)
718 else if (S_ISLNK(mode
))
720 else if (chech_execf(mode
) > 0)
725 delent(char *fullpath
)
731 inp_conf
= ecalloc(conf_len
, sizeof(char));
732 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
733 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
735 return 1; /* canceled by user or wrong inp_conf */
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
));
794 char *user_input
, *path
;
798 user_input
= ecalloc(MAX_USRI
, sizeof(char));
799 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
804 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
805 path
= ecalloc(pathlen
, sizeof(char));
806 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
812 rf
= open(path
, O_CREAT
| O_EXCL
, nf_perm
);
815 print_error(strerror(errno
));
818 print_error(strerror(errno
));
827 switch (delent(CURSOR_NAME
)) {
829 print_error(strerror(errno
));
832 if (BETWEEN(cpane
->hdir
- 1, 1, cpane
->dirc
)) /* last entry */
841 get_dirp(cpane
->dirn
);
842 if (check_dir(cpane
->dirn
) < 0) {
843 print_error(strerror(errno
));
848 cpane
->firstrow
= cpane
->parent_firstrow
;
849 cpane
->hdir
= cpane
->parent_row
;
850 if (listdir(AddHi
, NULL
) < 0)
851 print_error(strerror(errno
));
852 cpane
->parent_firstrow
= 0;
853 cpane
->parent_row
= 1;
861 if (cpane
->dirc
> scrheight
) {
862 rm_hi(cpane
, cpane
->hdir
- 1);
863 cpane
->hdir
= cpane
->dirc
;
864 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
866 add_hi(cpane
, cpane
->hdir
- 1);
868 rm_hi(cpane
, cpane
->hdir
- 1);
869 cpane
->hdir
= cpane
->dirc
;
870 add_hi(cpane
, cpane
->hdir
- 1);
880 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
881 rm_hi(cpane
, cpane
->hdir
- 1);
883 add_hi(cpane
, cpane
->hdir
- 1);
885 mvdwns(); /* scroll */
894 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
896 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
898 rm_hi(cpane
, cpane
->hdir
- 1);
901 add_hi(cpane
, cpane
->hdir
- 1);
902 } else if (cpane
->hdir
< cpane
->dirc
) {
903 rm_hi(cpane
, cpane
->hdir
- 1);
905 add_hi(cpane
, cpane
->hdir
- 1);
917 switch (check_dir(CURSOR_NAME
)) {
919 strcpy(cpane
->dirn
, CURSOR_NAME
);
920 cpane
->parent_row
= cpane
->hdir
;
921 cpane
->parent_firstrow
= cpane
->firstrow
;
924 if (listdir(AddHi
, NULL
) < 0)
925 print_error(strerror(errno
));
927 case 1: /* not a directory open file */
929 s
= opnf(CURSOR_NAME
);
934 print_error("process failed non-zero exit");
936 case -1: /* failed to open directory */
937 print_error(strerror(errno
));
946 rm_hi(cpane
, cpane
->hdir
- 1);
947 if (cpane
->dirc
< scrheight
/ 2)
948 cpane
->hdir
= (cpane
->dirc
+ 1) / 2;
950 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
951 add_hi(cpane
, cpane
->hdir
- 1);
960 if (cpane
->dirc
> scrheight
) {
961 rm_hi(cpane
, cpane
->hdir
- 1);
965 add_hi(cpane
, cpane
->hdir
- 1);
967 rm_hi(cpane
, cpane
->hdir
- 1);
969 add_hi(cpane
, cpane
->hdir
- 1);
979 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
980 rm_hi(cpane
, cpane
->hdir
- 1);
982 add_hi(cpane
, cpane
->hdir
- 1);
984 mvups(); /* scroll */
993 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
995 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
997 rm_hi(cpane
, cpane
->hdir
- 1);
1000 add_hi(cpane
, cpane
->hdir
- 1);
1001 } else if (cpane
->hdir
> 1) {
1002 rm_hi(cpane
, cpane
->hdir
- 1);
1004 add_hi(cpane
, cpane
->hdir
- 1);
1011 if (cpane
->dirc
< 1)
1013 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
1014 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
1015 rm_hi(cpane
, cpane
->hdir
- 1);
1016 cpane
->hdir
+= scrmv
;
1017 add_hi(cpane
, cpane
->hdir
- 1);
1032 real
= cpane
->hdir
- cpane
->firstrow
;
1033 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
1035 if (real
+ scrmv
+ 1 > scrheight
&&
1036 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
1037 cpane
->firstrow
+= dynmv
;
1038 rm_hi(cpane
, cpane
->hdir
- 1);
1039 cpane
->hdir
+= scrmv
;
1041 add_hi(cpane
, cpane
->hdir
- 1);
1043 if (cpane
->hdir
< cpane
->dirc
- scrmv
- 1) {
1044 rm_hi(cpane
, cpane
->hdir
- 1);
1045 cpane
->hdir
+= scrmv
;
1046 add_hi(cpane
, cpane
->hdir
- 1);
1056 if (cpane
->dirc
< 1)
1058 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1059 if (cpane
->hdir
> scrmv
) {
1060 rm_hi(cpane
, cpane
->hdir
- 1);
1061 cpane
->hdir
= cpane
->hdir
- scrmv
;
1062 add_hi(cpane
, cpane
->hdir
- 1);
1076 real
= cpane
->hdir
- cpane
->firstrow
;
1077 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1079 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1080 cpane
->firstrow
-= dynmv
;
1081 rm_hi(cpane
, cpane
->hdir
- 1);
1082 cpane
->hdir
-= scrmv
;
1084 add_hi(cpane
, cpane
->hdir
- 1);
1086 if (cpane
->hdir
> scrmv
+ 1) {
1087 rm_hi(cpane
, cpane
->hdir
- 1);
1088 cpane
->hdir
-= scrmv
;
1089 add_hi(cpane
, cpane
->hdir
- 1);
1097 get_usrinput(char *out
, size_t sout
, const char *fmt
, ...)
1099 int height
= tb_height();
1101 struct tb_event fev
;
1102 size_t counter
= (size_t)1;
1114 name_size
= vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
1116 print_tb(buf
, 1, height
-1, col
.fg
, col
.bg
);
1117 startat
= name_size
+ 1;
1118 tb_set_cursor((int)(startat
+ 1), height
-1);
1121 while (tb_poll_event(&fev
) != 0) {
1124 if (fev
.key
== TB_KEY_ESC
) {
1125 tb_set_cursor(-1, -1);
1130 if (fev
.key
== TB_KEY_BACKSPACE
||
1131 fev
.key
== TB_KEY_BACKSPACE2
) {
1132 if (BETWEEN(counter
, 2, sout
)) {
1136 print_xstatus(empty
, startat
+ counter
);
1137 tb_set_cursor(startat
+ counter
,
1141 } else if (fev
.key
== TB_KEY_ENTER
) {
1142 tb_set_cursor(-1, -1);
1143 out
[counter
- 1] = '\0';
1147 if (counter
< sout
) {
1148 print_xstatus((char)fev
.ch
,
1149 (startat
+ counter
));
1150 out
[x
] = (char)fev
.ch
;
1151 tb_set_cursor((startat
+ counter
+ 1),
1174 for (c
= 0; c
< LEN(rules
); c
++)
1175 for (d
= 0; d
< rules
[c
].exlen
; d
++)
1176 if (strncmp(rules
[c
].ext
[d
], ex
, MAX_EXT
) == 0)
1182 spawn(const void *v
, char *fn
)
1184 int ws
, x
, argc
, fd
;
1191 while (((char **)v
)[x
++] != NULL
)
1194 char *argv
[argc
+ 2];
1195 for ( x
= 0; x
< argc
; x
++)
1196 argv
[x
] = ((char **)v
)[x
];
1199 argv
[argc
+ 1] = NULL
;
1206 fd
= open("/dev/null",O_WRONLY
);
1207 dup2(fd
, STDERR_FILENO
);
1208 execvp(argv
[0], argv
);
1212 while ((r
= waitpid(pid
, &ws
, 0)) == -1 && errno
== EINTR
)
1216 if ((WIFEXITED(ws
) != 0) && (WEXITSTATUS(ws
) != 0))
1232 if (c
< 0) /* extension not found open in editor */
1233 return spawn(editor
, fn
);
1235 return spawn((char **)rules
[c
].v
, fn
);
1241 #if defined _SYS_INOTIFY_H
1242 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1245 #elif defined _SYS_EVENT_H_
1256 #if defined _SYS_INOTIFY_H
1257 return cpane
->inotify_wd
= inotify_add_watch(inotify_fd
, cpane
->dirn
,
1258 IN_MODIFY
| IN_MOVED_FROM
| IN_MOVED_TO
| IN_CREATE
|
1259 IN_DELETE
| IN_DELETE_SELF
| IN_MOVE_SELF
);
1260 #elif defined _SYS_EVENT_H_
1261 cpane
->event_fd
= open(cpane
->dirn
, O_RDONLY
);
1262 if (cpane
->event_fd
< 0)
1263 return cpane
->event_fd
;
1264 EV_SET(&evlist
[cpane
->pane_id
], cpane
->event_fd
,
1265 EVFILT_VNODE
, EV_ADD
| EV_CLEAR
,
1266 NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
1267 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
, 0, NULL
);
1275 #if defined _SYS_INOTIFY_H
1278 struct inotify_event
*event
;
1279 const size_t events
= 32;
1280 const size_t evbuflen
=
1281 events
* (sizeof(struct inotify_event
) + MAX_N
+ 1);
1284 if (cpane
->inotify_wd
< 0)
1286 r
= read(inotify_fd
, buf
, evbuflen
);
1290 for (p
= buf
; p
< buf
+ r
;) {
1291 event
= (struct inotify_event
*)p
;
1298 p
+= sizeof(struct inotify_event
) + event
->len
;
1300 #elif defined _SYS_EVENT_H_
1301 return kevent(kq
, evlist
, 2, chlist
, 2, >imeout
);
1309 #if defined _SYS_INOTIFY_H
1310 if (pane
->inotify_wd
>= 0)
1311 inotify_rm_watch(inotify_fd
, pane
->inotify_wd
);
1312 #elif defined _SYS_EVENT_H_
1313 close(pane
->event_fd
);
1323 #if defined _SYS_INOTIFY_H
1325 #elif defined _SYS_EVENT_H_
1331 findbm(uint32_t event
)
1335 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1336 if (event
== bmarks
[i
].ch
) {
1337 if (check_dir(bmarks
[i
].path
) != 0) {
1338 print_error(strerror(errno
));
1350 if (cpane
->dirc
< 1)
1353 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1354 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1358 if (listdir(AddHi
, user_input
) < 0)
1359 print_error("no match");
1366 struct tb_event fev
;
1367 if (selection
!= NULL
) {
1372 selection
= ecalloc(cpane
->dirc
, sizeof(size_t));
1373 selection
[0] = cpane
->hdir
;
1375 print_prompt("-- VISUAL --");
1377 while (tb_poll_event(&fev
) != 0) {
1380 grabkeys(&fev
, skeys
, skeyslen
);
1381 if (cont_vmode
== -1)
1393 add_hi(cpane
, cpane
->hdir
- 1);
1401 print_prompt("-- VISUAL --");
1402 int index
= abs(cpane
->hdir
- selection
[0]);
1404 if (cpane
->hdir
< selection
[0]) {
1405 selection
[index
] = cpane
->hdir
;
1406 add_hi(cpane
, selection
[index
]);
1407 } else if (index
< cpane
->dirc
) {
1408 selection
[index
+ 1] = 0;
1410 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
<= 1) { /* rehighlight all if scrolling */
1419 print_prompt("-- VISUAL --");
1420 int index
= abs(cpane
->hdir
- selection
[0]);
1422 if (cpane
->hdir
> selection
[0]) {
1423 selection
[index
] = cpane
->hdir
;
1424 add_hi(cpane
, selection
[index
] - 2);
1426 selection
[index
+ 1] = 0;
1428 if (cpane
->dirc
>= scrheight
|| cpane
->hdir
>= cpane
->dirc
) { /* rehighlight all if scrolling */
1437 for (i
= 0; i
< cpane
->dirc
; i
++) {
1438 selection
[i
] = i
+ 1;
1447 for (i
= 0; i
< cpane
->dirc
; i
++) {
1448 if (selection
[i
] < (scrheight
+ cpane
->firstrow
) && selection
[i
] > cpane
->firstrow
) { /* checks if in the frame of the directories */
1449 add_hi(cpane
, selection
[i
] - 1);
1460 for (j
= 0; j
< cpane
->dirc
; j
++) { /* calculate used selection size */
1461 if (selection
[j
] != 0)
1473 if (selected_files
!= NULL
) {
1474 for (i
= 0; i
< selection_size
; i
++) {
1475 free(selected_files
[i
]);
1476 selected_files
[i
] = NULL
;
1478 free(selected_files
);
1479 selected_files
= NULL
;
1490 selected_files
= ecalloc(selection_size
, sizeof(char*));
1492 for (i
= 0; i
< selection_size
; i
++) {
1493 selected_files
[i
] = ecalloc(MAX_P
, sizeof(char));
1494 // strcpy(selected_files[i], "\"");
1495 strcpy(selected_files
[i
], cpane
->direntr
[selection
[i
] -1].name
);
1496 // strcat(selected_files[i], "\"");
1505 add_hi(cpane
, cpane
->hdir
-1);
1506 print_status(cprompt
, "%zu files are yanked", selection_size
);
1515 char conf
[] = "yes";
1518 inp_conf
= ecalloc(conf_len
, sizeof(char));
1519 if ((get_usrinput(inp_conf
, conf_len
, "delete file (yes) ?") < 0) ||
1520 (strncmp(inp_conf
, conf
, conf_len
) != 0)) {
1522 return; /* canceled by user or wrong inp_conf */
1527 for (i
= 0; i
< selection_size
; i
++) {
1528 spawn(rm_cmd
, selected_files
[i
]);
1531 cpane
->hdir
= cpane
->dirc
- selection_size
;
1532 print_status(cprompt
, "%zu files are deleted", selection_size
);
1541 if (strlen(yank_file
) != 0) {
1542 print_status(cprompt
, "coping");
1543 if (spawn(cp_cmd
, cpane
->dirn
) != 0)
1544 print_error("coping failed");
1546 print_status(cprompt
, "file copied");
1547 yank_file
[0] = '\0'; /* set yank_file len 0 */
1551 print_error("nothing to paste");
1553 if (selected_files
== NULL
)
1556 for (i
= 0; i
< selection_size
; i
++) {
1557 char *selcp_cmd
[] = { "cp", "-r", selected_files
[i
], cpane
->dirn
, NULL
};
1558 spawn(selcp_cmd
,NULL
);
1560 print_status(cprompt
, "%zu files are copied", selection_size
);
1569 if (strlen(yank_file
) != 0) {
1570 print_status(cprompt
, "moving");
1571 if (spawn(mv_cmd
, cpane
->dirn
) != 0)
1572 print_error("moving failed");
1574 print_status(cprompt
, "file moved");
1575 yank_file
[0] = '\0'; /* set yank_file len 0 */
1579 print_error("nothing to move");
1581 if (selected_files
== NULL
)
1584 for (i
= 0; i
< selection_size
; i
++) {
1585 char *selmv_cmd
[] = { "mv", selected_files
[i
], cpane
->dirn
, NULL
};
1586 spawn(selmv_cmd
,NULL
);
1588 print_status(cprompt
, "%zu files are moved", selection_size
);
1595 char new_name
[MAX_P
];
1598 input_name
= ecalloc(MAX_N
, sizeof(char));
1600 if (get_usrinput(input_name
, MAX_N
, "rename: %s", basename(CURSOR_NAME
)) < 0) {
1605 strcpy(new_name
, cpane
->dirn
);
1606 strcat(new_name
, "/");
1607 strcat(new_name
, input_name
);
1609 char *rename_cmd
[] = { "mv", CURSOR_NAME
, new_name
, NULL
};
1610 if (spawn(rename_cmd
, NULL
) < 0)
1611 print_error(strerror(errno
));
1619 strncpy(yank_file
, CURSOR_NAME
, MAX_P
);
1620 print_status(cprompt
, "1 file is yanked", selection_size
);
1627 if (cpane
->dirc
> 0)
1628 rm_hi(cpane
, cpane
->hdir
- 1);
1629 if (cpane
== &pane_l
)
1631 else if (cpane
== &pane_r
)
1633 if (cpane
->dirc
> 0) {
1634 add_hi(cpane
, cpane
->hdir
- 1);
1644 if (cont_vmode
== -1) { /* check if selection was allocated */
1646 if (selected_files
!= NULL
)
1649 free(pane_l
.direntr
);
1650 free(pane_r
.direntr
);
1657 grabkeys(struct tb_event
*event
, Key
*key
, size_t max_keys
)
1662 for (i
= 0; i
< max_keys
; i
++) {
1663 if (event
->ch
!= 0) {
1664 if (event
->ch
== key
[i
].evkey
.ch
) {
1668 } else if (event
->key
!= 0) {
1669 if (event
->key
== key
[i
].evkey
.key
) {
1677 b
= findbm(event
->ch
);
1681 strcpy(cpane
->dirn
, bmarks
[b
].path
);
1682 cpane
->firstrow
= 0;
1683 cpane
->parent_row
= 1;
1685 if (listdir(AddHi
, NULL
) < 0)
1686 print_error(strerror(errno
));
1695 int t
= tb_peek_event(&ev
, 2000);
1701 if (t
== 1) /* keyboard event */
1702 grabkeys(&ev
, nkeys
, nkeyslen
);
1703 else if (t
== 2) /* resize event */
1705 else if (t
== 0) /* filesystem event */
1706 if (read_events() > 0) { /* TODO need refactoring */
1707 if (cpane
== &pane_l
) {
1709 if (listdir(NoHi
, NULL
) < 0)
1710 print_error(strerror(errno
));
1712 if (listdir(AddHi
, NULL
) < 0)
1713 print_error(strerror(errno
));
1714 } else if (cpane
== &pane_r
) {
1716 if (listdir(NoHi
, NULL
) < 0)
1717 print_error(strerror(errno
));
1719 if (listdir(AddHi
, NULL
) < 0)
1720 print_error(strerror(errno
));
1733 size_t y
, dyn_max
, start_from
;
1735 width
= (twidth
/ 2) - 4;
1739 start_from
= cpane
->firstrow
;
1740 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1742 /* print each entry in directory */
1743 while (start_from
< dyn_max
) {
1744 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1745 print_row(cpane
, start_from
, col
);
1750 if (cpane
->dirc
> 0)
1755 /* print current directory title */
1756 cpane
->dircol
.fg
|= TB_BOLD
;
1757 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1761 set_direntr(struct dirent
*entry
, DIR *dir
, char *filter
)
1769 tmpfull = get_fullpath(cpane->dirn, entry->d_name); \
1770 strcpy(cpane->direntr[i].name, tmpfull); \
1771 if (lstat(tmpfull, &status) == 0) { \
1772 cpane->direntr[i].size = status.st_size; \
1773 cpane->direntr[i].mode = status.st_mode; \
1774 cpane->direntr[i].group = status.st_gid; \
1775 cpane->direntr[i].user = status.st_uid; \
1776 cpane->direntr[i].td = status.st_mtime; \
1780 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1781 while ((entry
= readdir(dir
)) != 0) {
1782 if ((strcmp(entry
->d_name
, ".") == 0 ||
1783 strcmp(entry
->d_name
, "..") == 0))
1786 if (filter
!= NULL
) {
1787 if (strstr(entry
->d_name
, filter
) != NULL
) {
1792 if (filter
== NULL
) {
1801 listdir(int hi
, char *filter
)
1804 struct dirent
*entry
;
1806 int filtercount
= 0;
1807 size_t oldc
= cpane
->dirc
;
1809 width
= (twidth
/ 2) - 4;
1812 dir
= opendir(cpane
->dirn
);
1816 /* get content and filter sum */
1817 while ((entry
= readdir(dir
)) != 0) {
1818 if (filter
!= NULL
) {
1819 if (strstr(entry
->d_name
, filter
) != NULL
)
1821 } else { /* no filter */
1826 if (filter
== NULL
) {
1831 if (filter
!= NULL
) {
1832 if (filtercount
> 0) {
1834 cpane
->dirc
= filtercount
;
1837 } else if (filtercount
== 0) {
1838 if (closedir(dir
) < 0)
1845 /* print current directory title */
1846 cpane
->dircol
.fg
|= TB_BOLD
;
1847 printf_tb(cpane
->dirx
, 0, cpane
->dircol
, " %.*s ", width
, cpane
->dirn
);
1849 if (filter
== NULL
) /* dont't watch when filtering */
1851 print_error("can't add watch");
1853 /* empty directory */
1854 if (cpane
->dirc
== 0) {
1856 if (closedir(dir
) < 0)
1861 rewinddir(dir
); /* reset position */
1862 set_direntr(entry
, dir
, filter
); /* create array of entries */
1863 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1867 add_hi(cpane
, cpane
->hdir
- 1);
1869 if (closedir(dir
) < 0)
1877 /* TODO need refactoring */
1880 pane_r
.dirx
= (twidth
/ 2) + 2;
1882 if (cpane
== &pane_l
) {
1887 if (cpane
->dirc
> 0)
1888 add_hi(&pane_l
, pane_l
.hdir
- 1);
1889 } else if (cpane
== &pane_r
) {
1894 if (cpane
->dirc
> 0)
1895 add_hi(&pane_r
, pane_r
.hdir
- 1);
1904 editor
[0] = getenv("EDITOR");
1907 if (editor
[0] == NULL
)
1917 home
= getenv("HOME");
1920 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1921 strncpy(cwd
, home
, MAX_P
);
1925 pane_l
.dircol
= cpanell
;
1926 pane_l
.firstrow
= 0;
1927 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1928 strcpy(pane_l
.dirn
, cwd
);
1930 pane_l
.inotify_wd
= -1;
1931 pane_l
.parent_row
= 1;
1934 pane_r
.dirx
= (twidth
/ 2) + 2;
1935 pane_r
.dircol
= cpanelr
;
1936 pane_r
.firstrow
= 0;
1937 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1938 strcpy(pane_r
.dirn
, home
);
1940 pane_r
.inotify_wd
= -1;
1941 pane_r
.parent_row
= 1;
1948 theight
= tb_height();
1949 twidth
= tb_width();
1950 scrheight
= theight
- 2;
1952 /* 2 horizontal lines */
1953 for (i
= 1; i
< twidth
- 1; ++i
) {
1954 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1955 tb_change_cell(i
, theight
- 2, u_hl
, cframe
.fg
, cframe
.bg
);
1958 /* 4 vertical lines */
1959 for (i
= 1; i
< theight
- 1; ++i
) {
1960 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1961 tb_change_cell((twidth
- 1) / 2, i
- 1, u_vl
, cframe
.fg
,
1963 tb_change_cell(((twidth
- 1) / 2) + 1, i
- 1, u_vl
, cframe
.fg
,
1965 tb_change_cell(twidth
- 1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1969 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1970 tb_change_cell(twidth
- 1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1971 tb_change_cell(0, theight
- 2, u_csw
, cframe
.fg
, cframe
.bg
);
1972 tb_change_cell(twidth
- 1, theight
- 2, u_cse
, cframe
.fg
, cframe
.bg
);
1974 /* 2 middel top and bottom */
1975 tb_change_cell((twidth
- 1) / 2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1976 tb_change_cell((twidth
- 1) / 2, theight
- 2, u_ms
, cframe
.fg
, cframe
.bg
);
1984 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1985 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1986 die("output error");
1991 if (fsev_init() < 0)
1992 print_error(strerror(errno
));
1994 if (listdir(NoHi
, NULL
) < 0)
1995 print_error(strerror(errno
));
1997 if (listdir(AddHi
, NULL
) < 0)
1998 print_error(strerror(errno
));
2004 main(int argc
, char *argv
[])
2007 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
2010 #endif /* __OpenBSD__ */
2013 else if (argc
== 2 && strncmp("-v", argv
[1], 2) == 0)
2014 die("sfm-" VERSION
);
2016 die("usage: sfm [-v]");