1 /* See LICENSE file for copyright and license details. */
13 #include <sys/resource.h>
16 #include <sys/types.h>
22 #include <sys/inotify.h>
24 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
27 #include <sys/event.h>
28 #endif /* filesystem events */
38 #define EV_BUF_LEN (EVENTS * (sizeof(struct inotify_event) + MAX_N + 1))
41 enum { AskDel
, DAskDel
}; /* delete directory */
59 char dirn
[MAX_P
]; // dir name cwd
60 Entry
*direntr
; // dir entries
61 int dirx
; // pane cwd x pos
62 size_t dirc
; // dir entries sum
63 size_t hdir
; // highlighted dir
80 uint16_t key
; /* one of the TB_KEY_* constants */
81 uint32_t ch
; /* unicode character */
89 /* function declarations */
90 static void print_tb(const char*, int, int, uint16_t, uint16_t);
91 static void printf_tb(int, int, Cpair
, const char*, ...);
92 static void print_status(Cpair
, const char*, ...);
93 static void print_xstatus(char, int);
94 static void print_error(char*);
95 static void print_prompt(char*);
96 static void print_info(void);
97 static void print_row(Pane
*, size_t, Cpair
);
98 static void clear(int, int, int, uint16_t);
99 static void clear_status(void);
100 static void clear_pane(int);
101 static void add_hi(Pane
*, size_t);
102 static void rm_hi(Pane
*, size_t);
103 static void float_to_string(float, char*);
104 static int check_dir(char*);
105 static mode_t
chech_execf(mode_t
);
106 static int sort_name(const void *const, const void *const);
107 static char *get_ext(char*);
108 static int get_fdt(char*, size_t, time_t);
109 static char *get_fgrp(gid_t
, size_t);
110 static char *get_finfo(Entry
*);
111 static char *get_fperm(mode_t
);
112 static char *get_fsize(off_t
);
113 static char *get_fullpath(char*, char*);
114 static char *get_fusr(uid_t
, size_t);
115 static void get_dirsize(char*, off_t
*);
116 static void get_hicol(Cpair
*, mode_t
);
117 static int deldir(char*, int);
118 static int delent(char *);
119 static int delf(char*);
120 static void calcdir(void);
121 static void crnd(void);
122 static void crnf(void);
123 static void delfd(void);
124 static void mvbk(void);
125 static void mvbtm(void);
126 static void mvdwn(void);
127 static void mvdwns(void);
128 static void mvfor(void);
129 static void mvmid(void);
130 static void mvtop(void);
131 static void mvup(void);
132 static void mvups(void);
133 static void scrdwn(void);
134 static void scrdwns(void);
135 static void scrup(void);
136 static void scrups(void);
137 static int get_usrinput(char*, size_t, char*);
138 static int open_files(char*);
139 static ssize_t
findbm(char);
140 static void filter(void);
141 static void switch_pane(void);
142 static void quit(void);
143 static void grabkeys(struct tb_event
*);
144 static void start_ev(void);
145 static void refresh_pane(void);
146 static int listdir(char*);
147 static void t_resize(void);
148 static int set_panes(int);
149 static void draw_frame(void);
150 static int start(void);
152 /* global variables */
153 static Pane pane_r
, pane_l
, *cpane
;
154 static int parent_row
= 1; // FIX
155 static size_t scrheight
;
157 static const uint32_t INOTIFY_MASK
= IN_CREATE
| IN_DELETE
| IN_DELETE_SELF \
158 | IN_MODIFY
| IN_MOVE_SELF
| IN_MOVED_FROM
| IN_MOVED_TO
;
160 /* configuration, allows nested code to access above variables */
163 /* function implementations */
165 print_tb(const char *str
, int x
, int y
, uint16_t fg
, uint16_t bg
)
167 while (*str
!= '\0') {
169 str
+= tb_utf8_char_to_unicode(&uni
, str
);
170 tb_change_cell(x
, y
, uni
, fg
, bg
);
176 printf_tb(int x
, int y
, Cpair col
, const char *fmt
, ...)
181 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
183 print_tb(buf
, x
, y
, col
.fg
, col
.bg
);
187 print_status(Cpair col
, const char *fmt
, ...)
190 height
= tb_height();
195 (void)vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
198 print_tb(buf
, 1, height
-1, col
.fg
, col
.bg
);
203 print_xstatus(char c
, int x
)
207 height
= tb_height();
208 (void)tb_utf8_char_to_unicode(&uni
, &c
);
209 tb_change_cell(x
, height
-1, uni
, cstatus
.fg
, cstatus
.bg
);
213 print_error(char *errmsg
)
215 print_status(cerr
, errmsg
);
219 print_prompt(char *prompt
)
221 print_status(cprompt
, prompt
);
228 fileinfo
= get_finfo(&cpane
->direntr
[cpane
->hdir
-1]);
229 print_status(cstatus
, "%lu/%lu %s",
230 cpane
->hdir
, cpane
->dirc
, fileinfo
);
235 print_row(Pane
*pane
, size_t entpos
, Cpair col
)
240 char lnk_full
[MAX_P
];
243 width
= (tb_width() / 2) - 4;
244 result
= pane
->direntr
[entpos
].name
;
246 y
= entpos
- cpane
->firstrow
+ 1;
248 if (S_ISLNK(pane
->direntr
[entpos
].mode
) &&
249 realpath(pane
->direntr
[entpos
].name
, buf
) != NULL
) {
250 strncpy(lnk_full
, pane
->direntr
[entpos
].name
, MAX_N
);
251 strcat(lnk_full
, " -> ");
252 strncat(lnk_full
, buf
, MAX_N
);
256 printf_tb(x
, y
, col
, "%*.*s", ~width
, width
, result
);
260 clear(int sx
, int ex
, int y
, uint16_t bg
)
262 /* clear line from to */
263 /* x = line number vertical */
264 /* y = column number horizontal */
266 for (i
= sx
; i
< ex
; i
++) {
267 tb_change_cell(i
, y
, 0x0000, TB_DEFAULT
, bg
);
277 height
= tb_height();
278 clear(1, width
-1, height
-1, cstatus
.bg
);
284 int i
, x
, ex
, y
, width
, height
;
286 height
= tb_height();
287 x
= 0, y
= 0, i
= 0, ex
= 0;
292 } else if (pane
== (width
/2) + 2) {
297 while (i
< height
-2) {
298 clear(x
, ex
, y
, TB_DEFAULT
);
303 for (y
= x
; y
< ex
; ++y
) {
304 tb_change_cell(y
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
310 add_hi(Pane
*pane
, size_t entpos
)
313 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
314 col
.fg
|= TB_REVERSE
|TB_BOLD
;
315 col
.bg
|= TB_REVERSE
;
316 print_row(pane
, entpos
, col
);
320 rm_hi(Pane
*pane
, size_t entpos
)
323 get_hicol(&col
, pane
->direntr
[entpos
].mode
);
324 print_row(pane
, entpos
, col
);
328 float_to_string(float f
, char *r
)
330 int length
, length2
, i
, number
, position
, tenth
; /* length is size of decimal part, length2 is size of tenth part */
333 f
= (float)(int)(f
* 10) / 10;
340 /* Calculate length2 tenth part */
341 while ((number2
- (float)number
) != 0.0 && !((number2
- (float)number
) < 0.0))
344 number2
= f
* (float)tenth
;
345 number
= (int)number2
;
350 /* Calculate length decimal part */
351 for (length
= (f
> 1.0) ? 0 : 1; f
> 1.0; length
++)
355 length
= length
+ 1 + length2
;
356 number
= (int)number2
;
360 for (i
= length
; i
>= 0; i
--)
364 else if (i
== (position
))
368 r
[i
] = (char)(number
% 10) + '0';
376 for (i
= length
; i
>= 0; i
--)
382 r
[i
] = (char)(number
% 10) + '0';
390 check_dir(char *path
)
396 if (errno
== ENOTDIR
) {
403 if (closedir(dir
) < 0)
410 chech_execf(mode_t mode
)
413 return (((S_IXUSR
| S_IXGRP
| S_IXOTH
) & mode
));
418 sort_name(const void *const A
, const void *const B
)
421 mode_t data1
= (*(Entry
*) A
).mode
;
422 mode_t data2
= (*(Entry
*) B
).mode
;
426 } else if (data1
== data2
) {
427 result
= strcmp((*(Entry
*) A
).name
,(*(Entry
*) B
).name
);
439 size_t counter
, len
, i
;
445 for (i
= len
-1; i
> 0; i
--) {
453 ext
= ecalloc(counter
+1, sizeof(char));
454 strncpy(ext
, &str
[len
-counter
], counter
);
459 get_fdt(char *result
, size_t reslen
, time_t status
)
462 localtime_r(&status
, <
);
463 return strftime(result
, reslen
, dtfmt
, <
);
467 get_fgrp(gid_t status
, size_t len
)
472 result
= ecalloc(len
, sizeof(char));
473 gr
= getgrgid(status
);
475 (void)snprintf(result
, len
-1, "%d", (int)status
);
477 strncpy(result
, gr
->gr_name
, len
-1);
483 get_finfo(Entry
*cursor
)
485 char *sz
, *rst
, *ur
, *gr
, *td
, *prm
;
486 size_t szlen
, prmlen
, urlen
, grlen
, tdlen
, rstlen
;
490 urlen
= grlen
= tdlen
= 32;
491 rstlen
= szlen
+ prmlen
+ urlen
+ grlen
+ tdlen
;
492 rst
= ecalloc(rstlen
, sizeof(char));
494 if (show_perm
== 1) {
495 prm
= get_fperm(cursor
->mode
);
496 strncpy(rst
, prm
, prmlen
);
502 ur
= get_fusr(cursor
->user
, urlen
);
503 gr
= get_fgrp(cursor
->group
, grlen
);
504 strncat(rst
, ur
, urlen
);
506 strncat(rst
, gr
, grlen
);
513 td
= ecalloc(tdlen
, sizeof(char));
514 if (get_fdt(td
, tdlen
, cursor
->td
) > 0) {
515 strncat(rst
, td
, tdlen
);
521 if (show_size
== 1 && S_ISREG(cursor
->mode
)) {
522 sz
= get_fsize(cursor
->size
);
523 strncat(rst
, sz
, szlen
);
531 get_fperm(mode_t mode
)
536 const char chars
[] = "rwxrwxrwx";
537 buf
= ecalloc((size_t)11, sizeof(char));
541 else if (S_ISREG(mode
))
543 else if (S_ISLNK(mode
))
545 else if (S_ISBLK(mode
))
547 else if (S_ISCHR(mode
))
549 else if (S_ISFIFO(mode
))
551 else if (S_ISSOCK(mode
))
556 for (i
= 1; i
< 10; i
++) {
557 buf
[i
] = (mode
& (1 << (9-i
))) ? chars
[i
-1] : '-';
565 get_fsize(off_t size
)
568 /* need to be freed */
574 Rsize
= ecalloc((size_t)10, sizeof(char));
577 while (lsize
>= 1000.0)
583 float_to_string(lsize
, Rsize
);
608 get_fullpath(char *first
, char *second
)
611 size_t full_path_len
;
613 full_path_len
= strlen(first
) + strlen(second
) + 2;
614 full_path
= ecalloc(full_path_len
, sizeof(char));
616 if (strcmp(first
, "/") == 0) {
617 (void)snprintf(full_path
, full_path_len
, "/%s", second
);
621 full_path
, full_path_len
, "%s/%s", first
, second
);
628 get_fusr(uid_t status
, size_t len
)
633 result
= ecalloc(len
, sizeof(char));
634 pw
= getpwuid(status
);
636 (void)snprintf(result
, len
-1, "%d", (int)status
);
638 strncpy(result
, pw
->pw_name
, len
-1);
644 get_dirsize(char *fullpath
, off_t
*fullsize
)
649 struct dirent
*entry
;
652 dir
= opendir(fullpath
);
658 while ((entry
= readdir(dir
)) != 0)
660 if ((strcmp(entry
->d_name
, ".") == 0 ||
661 strcmp(entry
->d_name
, "..") == 0))
664 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
665 if (lstat(ent_full
, &status
) == 0) {
666 mode
= status
.st_mode
;
668 get_dirsize(ent_full
, fullsize
);
672 *fullsize
+= status
.st_size
;
683 get_hicol(Cpair
*col
, mode_t mode
)
688 else if (S_ISLNK(mode
))
690 else if (chech_execf(mode
) > 0)
695 deldir(char *fullpath
, int delchoice
)
697 if (delchoice
== (int)AskDel
) {
699 confirmation
= ecalloc((size_t)2, sizeof(char));
701 confirmation
, (size_t)2,"delete directory (Y) ?") < 0) ||
702 (strcmp(confirmation
, "Y") != 0)) {
704 return 1; /* canceled by user or wrong confirmation */
709 if (rmdir(fullpath
) == 0)
710 return 0; /* empty directory */
715 struct dirent
*entry
;
718 dir
= opendir(fullpath
);
723 while ((entry
= readdir(dir
)) != 0) {
724 if ((strcmp(entry
->d_name
, ".") == 0 ||
725 strcmp(entry
->d_name
, "..") == 0))
728 ent_full
= get_fullpath(fullpath
, entry
->d_name
);
729 if (lstat(ent_full
, &status
) == 0) {
730 mode
= status
.st_mode
;
732 if (deldir(ent_full
, (int)DAskDel
) < 0) {
736 } else if (S_ISREG(mode
)) {
737 if (unlink(ent_full
) < 0) {
746 print_status(cstatus
, "gotit");
747 if (closedir(dir
) < 0)
750 return rmdir(fullpath
); /* directory after delete all entries */
754 delent(char *fullpath
)
759 if (lstat(fullpath
, &status
) < 0)
762 mode
= status
.st_mode
;
764 return deldir(fullpath
, (int)AskDel
);
766 return delf(fullpath
);
775 confirmation
= ecalloc((size_t)2, sizeof(char));
777 if ((get_usrinput(confirmation
, (size_t)2, "delete file (Y) ?") < 0) ||
778 (strcmp(confirmation
, "Y") != 0)) {
780 return 1; /* canceled by user or wrong confirmation */
784 return unlink(fullpath
);
794 if (S_ISDIR(cpane
->direntr
[cpane
->hdir
-1].mode
)) {
795 fullsize
= ecalloc(50, sizeof(off_t
));
796 get_dirsize(cpane
->direntr
[cpane
->hdir
-1].name
, fullsize
);
797 csize
= get_fsize(*fullsize
);
798 result
= get_finfo(&cpane
->direntr
[cpane
->hdir
-1]);
801 print_status(cstatus
, "%lu/%lu %s%s",
802 cpane
->hdir
, cpane
->dirc
, result
, csize
);
812 char *user_input
, *path
;
815 user_input
= ecalloc(MAX_USRI
, sizeof(char));
816 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
821 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
822 path
= ecalloc(pathlen
, sizeof(char));
823 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
829 if (mkdir(path
, new_dir_perm
) < 0)
830 print_error(strerror(errno
));
841 char *user_input
, *path
;
845 user_input
= ecalloc(MAX_USRI
, sizeof(char));
846 if (get_usrinput(user_input
, MAX_USRI
, "new file") < 0) {
851 pathlen
= strlen(cpane
->dirn
) + 1 + MAX_USRI
+ 1;
852 path
= ecalloc(pathlen
, sizeof(char));
853 if (snprintf(path
, pathlen
, "%s/%s", cpane
->dirn
, user_input
) < 0) {
859 rf
= open(path
, O_CREAT
|O_EXCL
, S_IRUSR
|S_IWUSR
);
862 print_error(strerror(errno
));
865 print_error(strerror(errno
));
877 switch (delent(cpane
->direntr
[cpane
->hdir
-1].name
)) {
879 print_error(strerror(errno
));
882 if (cpane
->hdir
== cpane
->dirc
) /* last entry */
893 getcwd(cpane
->dirn
, MAX_P
);
895 cpane
->hdir
= parent_row
;
898 add_hi(cpane
, cpane
->hdir
-1);
904 if (cpane
->dirc
> scrheight
) {
905 clear_pane(cpane
->dirx
);
906 rm_hi(cpane
, cpane
->hdir
-1);
907 cpane
->hdir
= cpane
->dirc
;
908 cpane
->firstrow
= cpane
->dirc
- scrheight
+ 1;
910 add_hi(cpane
, cpane
->hdir
-1);
912 rm_hi(cpane
, cpane
->hdir
-1);
913 cpane
->hdir
= cpane
->dirc
;
914 add_hi(cpane
, cpane
->hdir
-1);
922 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
923 rm_hi(cpane
, cpane
->hdir
-1);
925 add_hi(cpane
, cpane
->hdir
-1);
927 mvdwns(); /* scroll */
936 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
938 if (real
> scrheight
- 3 - scrsp
&& cpane
->hdir
+ scrsp
< cpane
->dirc
) {
940 clear_pane(cpane
->dirx
);
941 rm_hi(cpane
, cpane
->hdir
-1);
944 add_hi(cpane
, cpane
->hdir
-1);
945 } else if (cpane
->hdir
< cpane
->dirc
) {
946 rm_hi(cpane
, cpane
->hdir
-1);
948 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 parent_row
= (int)cpane
->hdir
;
963 add_hi(cpane
, cpane
->hdir
-1);
966 /* is not a directory open file */
967 if (open_files(cpane
->direntr
[cpane
->hdir
-1].name
) < 0) {
968 print_error("procces failed");
976 /* failed to open directory */
977 print_error(strerror(errno
));
984 rm_hi(cpane
, cpane
->hdir
- 1);
985 cpane
->hdir
= (scrheight
/ 2) + cpane
->firstrow
;
986 add_hi(cpane
, cpane
->hdir
- 1);
993 if (cpane
->dirc
> scrheight
) {
994 clear_pane(cpane
->dirx
);
995 rm_hi(cpane
, cpane
->hdir
-1);
999 add_hi(cpane
, cpane
->hdir
-1);
1001 rm_hi(cpane
, cpane
->hdir
-1);
1003 add_hi(cpane
, cpane
->hdir
-1);
1011 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1012 rm_hi(cpane
, cpane
->hdir
-1);
1014 add_hi(cpane
, cpane
->hdir
-1);
1016 mvups(); /* scroll */
1025 real
= cpane
->hdir
- 1 - cpane
->firstrow
;
1027 if (cpane
->firstrow
> 0 && real
< 1 + scrsp
) {
1029 clear_pane(cpane
->dirx
);
1030 rm_hi(cpane
, cpane
->hdir
-1);
1033 add_hi(cpane
, cpane
->hdir
-1);
1034 } else if (cpane
->hdir
> 1) {
1035 rm_hi(cpane
, cpane
->hdir
-1);
1037 add_hi(cpane
, cpane
->hdir
-1);
1044 if (cpane
->dirc
< scrheight
&& cpane
->hdir
< cpane
->dirc
) {
1045 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
1046 rm_hi(cpane
, cpane
->hdir
-1);
1047 cpane
->hdir
+= scrmv
;
1048 add_hi(cpane
, cpane
->hdir
-1);
1063 real
= cpane
->hdir
- cpane
->firstrow
;
1064 dynmv
= MIN(cpane
->dirc
- cpane
->hdir
- cpane
->firstrow
, scrmv
);
1066 if (real
+ scrmv
+ 1 > scrheight
&&
1067 cpane
->hdir
+ scrsp
+ scrmv
< cpane
->dirc
) { /* scroll */
1068 cpane
->firstrow
+= dynmv
;
1069 clear_pane(cpane
->dirx
);
1070 rm_hi(cpane
, cpane
->hdir
-1);
1071 cpane
->hdir
+= scrmv
;
1073 add_hi(cpane
, cpane
->hdir
-1);
1075 if (cpane
->hdir
< cpane
->dirc
- scrmv
) {
1076 rm_hi(cpane
, cpane
->hdir
-1);
1077 cpane
->hdir
+= scrmv
;
1078 add_hi(cpane
, cpane
->hdir
-1);
1088 if (cpane
->dirc
< scrheight
&& cpane
->hdir
> 1) {
1089 if (cpane
->hdir
> scrmv
) {
1090 rm_hi(cpane
, cpane
->hdir
-1);
1091 cpane
->hdir
= cpane
->hdir
- scrmv
;
1092 add_hi(cpane
, cpane
->hdir
-1);
1107 real
= cpane
->hdir
- cpane
->firstrow
;
1108 dynmv
= MIN(cpane
->firstrow
, scrmv
);
1110 if (cpane
->firstrow
> 0 && real
< scrmv
+ scrsp
) {
1111 cpane
->firstrow
-= dynmv
;
1112 clear_pane(cpane
->dirx
);
1113 rm_hi(cpane
, cpane
->hdir
-1);
1114 cpane
->hdir
-= scrmv
;
1116 add_hi(cpane
, cpane
->hdir
-1);
1118 if (cpane
->hdir
> scrmv
+ 1) {
1119 rm_hi(cpane
, cpane
->hdir
-1);
1120 cpane
->hdir
-= scrmv
;
1121 add_hi(cpane
, cpane
->hdir
-1);
1129 get_usrinput(char *out
, size_t sout
, char *prompt
)
1131 int height
= tb_height();
1133 struct tb_event fev
;
1134 size_t counter
= (size_t)1;
1139 startat
= strlen(prompt
) + 3;
1140 print_prompt(prompt
);
1141 tb_set_cursor((int)(startat
+ 1), height
-1);
1144 while (tb_poll_event(&fev
) != 0) {
1147 if (fev
.key
== (uint16_t)TB_KEY_ESC
) {
1148 tb_set_cursor(-1, -1);
1153 if (fev
.key
== (uint16_t)TB_KEY_BACKSPACE
||
1154 fev
.key
== (uint16_t)TB_KEY_BACKSPACE2
) {
1155 if (BETWEEN(counter
, (size_t)2, sout
)) {
1159 print_xstatus(empty
, startat
+ counter
);
1161 (int)startat
+ counter
, height
- 1);
1164 } else if (fev
.key
== (uint16_t)TB_KEY_ENTER
) {
1165 tb_set_cursor(-1, -1);
1166 out
[counter
-1] = '\0';
1170 if (counter
< sout
) {
1171 print_xstatus((char)fev
.ch
, (int)(startat
+counter
));
1172 out
[x
] = (char)fev
.ch
;
1173 tb_set_cursor((int)(startat
+ counter
+ 1),height
-1);
1192 open_files(char *filename
)
1195 /* open editor in other window */
1197 char *editor
, *file_ex
, *software
, *term
;
1202 editor
= getenv("EDITOR");
1203 term
= getenv("TERM");
1204 file_ex
= get_ext(filename
);
1207 /* find software in rules */
1208 for (c
= 0; c
< LEN(rules
); c
++) {
1209 for (d
= 0; d
< rules
[c
].len
; d
++) {
1210 if (strcmp(rules
[c
].ext
[d
], file_ex
) == 0) {
1211 software
= rules
[c
].soft
;
1216 /* default softwares */
1218 term
= "xterm-256color";
1221 if (software
== NULL
)
1225 char *filex
[] = {software
, filename
, NULL
};
1233 (void)execvp(filex
[0], filex
);
1236 while ((r
= waitpid(pid
, &status
, 0)) == (pid_t
)-1 && errno
== EINTR
)
1240 if ((WIFEXITED(status
) != 0) && (WEXITSTATUS(status
) != 0))
1252 for (i
= 0; i
< (ssize_t
)LEN(bmarks
); i
++) {
1253 if (event
== bmarks
[i
].key
) {
1254 if (check_dir(bmarks
[i
].path
) != 0) {
1255 print_error(strerror(errno
));
1268 user_input
= ecalloc(MAX_USRI
, sizeof(char));
1269 if (get_usrinput(user_input
, MAX_USRI
, "filter") < 0) {
1273 if (listdir(user_input
) < 0) {
1274 print_error("no match");
1282 if (cpane
== &pane_l
) {
1283 rm_hi(&pane_l
, pane_l
.hdir
-1);
1284 add_hi(&pane_r
, pane_r
.hdir
-1);
1287 } else if (cpane
== &pane_r
) {
1288 rm_hi(&pane_r
, pane_r
.hdir
-1);
1289 add_hi(&pane_l
, pane_l
.hdir
-1);
1299 free(pane_l
.direntr
);
1300 free(pane_r
.direntr
);
1306 grabkeys(struct tb_event
*event
)
1310 for (i
= 0; i
< LEN(keys
); i
++) {
1311 if (event
->ch
!= 0) {
1312 if (event
->ch
== keys
[i
].evkey
.ch
) {
1316 } else if (event
->key
!= 0) {
1317 if (event
->key
== keys
[i
].evkey
.key
) {
1330 while (tb_poll_event(&ev
) != 0) {
1336 case TB_EVENT_RESIZE
:
1349 size_t y
, dyn_max
, start_from
;
1351 width
= (tb_width() / 2) - 4;
1355 start_from
= cpane
->firstrow
;
1356 dyn_max
= MIN(cpane
->dirc
, (scrheight
- 1) + cpane
->firstrow
);
1358 /* print each entry in directory */
1359 while (start_from
< dyn_max
) {
1360 get_hicol(&col
, cpane
->direntr
[start_from
].mode
);
1361 print_row(cpane
, start_from
, col
);
1368 /* print current directory title */
1369 cpane
->dircol
.fg
|= TB_BOLD
;
1370 printf_tb(cpane
->dirx
, 0, cpane
->dircol
," %.*s ", width
, cpane
->dirn
);
1374 listdir(char *filter
)
1377 struct dirent
*entry
;
1381 int filtercount
= 0;
1382 size_t oldc
= cpane
->dirc
;
1384 width
= (tb_width() / 2) - 4;
1388 if (chdir(cpane
->dirn
) < 0)
1391 dir
= opendir(cpane
->dirn
);
1395 /* get content and filter sum */
1396 while ((entry
= readdir(dir
)) != 0) {
1397 if (filter
!= NULL
) {
1398 if (strstr(entry
->d_name
, filter
) != NULL
)
1400 } else { /* no filter */
1405 if (filter
== NULL
) {
1406 clear_pane(cpane
->dirx
);
1410 if (filter
!= NULL
) {
1411 if (filtercount
> 0) {
1413 cpane
->dirc
= filtercount
;
1414 clear_pane(cpane
->dirx
);
1416 } else if (filtercount
== 0) {
1417 if (closedir(dir
) < 0)
1424 /* print current directory title */
1425 cpane
->dircol
.fg
|= TB_BOLD
;
1426 printf_tb(cpane
->dirx
, 0, cpane
->dircol
," %.*s ", width
, cpane
->dirn
);
1428 /* empty directory */
1429 if (cpane
->dirc
== 0) {
1431 if (closedir(dir
) < 0)
1436 rewinddir(dir
); /* reset position */
1438 /* create array of entries */
1440 cpane
->direntr
= erealloc(cpane
->direntr
, cpane
->dirc
* sizeof(Entry
));
1441 while ((entry
= readdir(dir
)) != 0) {
1442 if ((strcmp(entry
->d_name
, ".") == 0 ||
1443 strcmp(entry
->d_name
, "..") == 0))
1446 /* list found filter */
1447 if (filter
!= NULL
) {
1448 if (strstr(entry
->d_name
, filter
) != NULL
) {
1449 strcpy(cpane
->direntr
[i
].name
, entry
->d_name
);
1450 if (lstat(entry
->d_name
, &status
) == 0) {
1451 cpane
->direntr
[i
].size
= status
.st_size
;
1452 cpane
->direntr
[i
].mode
= status
.st_mode
;
1453 cpane
->direntr
[i
].group
= status
.st_gid
;
1454 cpane
->direntr
[i
].user
= status
.st_uid
;
1455 cpane
->direntr
[i
].td
= status
.st_mtime
;
1461 strcpy(cpane
->direntr
[i
].name
, entry
->d_name
);
1462 if (lstat(entry
->d_name
, &status
) == 0) {
1463 cpane
->direntr
[i
].size
= status
.st_size
;
1464 cpane
->direntr
[i
].mode
= status
.st_mode
;
1465 cpane
->direntr
[i
].group
= status
.st_gid
;
1466 cpane
->direntr
[i
].user
= status
.st_uid
;
1467 cpane
->direntr
[i
].td
= status
.st_mtime
;
1474 qsort(cpane
->direntr
, cpane
->dirc
, sizeof(Entry
), sort_name
);
1477 if (closedir(dir
) < 0)
1485 /* TODO need refactoring */
1490 if (cpane
== &pane_l
) {
1497 add_hi(&pane_l
, pane_l
.hdir
-1);
1498 } else if (cpane
== &pane_r
) {
1505 add_hi(&pane_r
, pane_r
.hdir
-1);
1512 set_panes(int resize
)
1517 scrheight
= tb_height() - 2;
1519 home
= getenv("HOME");
1521 if ((getcwd(cwd
, sizeof(cwd
)) == NULL
))
1527 pane_l
.dircol
= cpanell
;
1528 pane_l
.firstrow
= 0;
1530 pane_l
.direntr
= ecalloc(0, sizeof(Entry
));
1531 strcpy(pane_l
.dirn
, cwd
);
1535 pane_r
.dirx
= (width
/ 2) + 2;
1536 pane_r
.dircol
= cpanelr
;
1537 pane_r
.firstrow
= 0;
1539 pane_r
.direntr
= ecalloc(0, sizeof(Entry
));
1540 strcpy(pane_r
.dirn
, home
);
1549 int height
, width
, i
;
1552 height
= tb_height();
1554 /* 2 horizontal lines */
1555 for (i
= 1; i
< width
-1 ; ++i
) {
1556 tb_change_cell(i
, 0, u_hl
, cframe
.fg
, cframe
.bg
);
1557 tb_change_cell(i
, height
-2, u_hl
, cframe
.fg
, cframe
.bg
);
1560 /* 3 vertical lines */
1561 for (i
= 1; i
< height
-1 ; ++i
) {
1562 tb_change_cell(0, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1563 tb_change_cell((width
-1)/2, i
-1, u_vl
, cframe
.fg
, cframe
.bg
);
1564 tb_change_cell(width
-1, i
, u_vl
, cframe
.fg
, cframe
.bg
);
1568 tb_change_cell(0, 0, u_cnw
, cframe
.fg
, cframe
.bg
);
1569 tb_change_cell(width
-1, 0, u_cne
, cframe
.fg
, cframe
.bg
);
1570 tb_change_cell(0, height
-2, u_csw
, cframe
.fg
, cframe
.bg
);
1571 tb_change_cell(width
-1, height
-2, u_cse
, cframe
.fg
, cframe
.bg
);
1573 /* 2 middel top and bottom */
1574 tb_change_cell((width
-1)/2, 0, u_mn
, cframe
.fg
, cframe
.bg
);
1575 tb_change_cell((width
-1)/2, height
-2, u_ms
, cframe
.fg
, cframe
.bg
);
1583 if (tb_select_output_mode(TB_OUTPUT_256
) != TB_OUTPUT_256
)
1584 if (tb_select_output_mode(TB_OUTPUT_NORMAL
) != TB_OUTPUT_NORMAL
)
1585 die("output error");
1593 add_hi(&pane_l
, pane_l
.hdir
-1);
1600 main(int argc
, char *argv
[])
1603 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath", NULL
) == -1)
1605 #endif /* __OpenBSD__ */
1608 die("start failed");
1610 argc
== 2 && strlen(argv
[1]) == (size_t)2 &&
1611 strcmp("-v", argv
[1]) == 0) {
1614 die("usage: sfm [-v]");