My build of nnn with minor changes
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

2328 satır
48 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
  4. || defined(__APPLE__)
  5. # include <sys/types.h>
  6. #else
  7. # include <sys/sysmacros.h>
  8. #endif
  9. #include <sys/wait.h>
  10. #include <sys/statvfs.h>
  11. #include <sys/resource.h>
  12. #include <ctype.h>
  13. #include <curses.h>
  14. #include <dirent.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <grp.h>
  18. #include <libgen.h>
  19. #include <limits.h>
  20. #ifdef __gnu_hurd__
  21. #define PATH_MAX 4096
  22. #endif
  23. #include <locale.h>
  24. #include <pwd.h>
  25. #include <regex.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <wchar.h>
  34. #include <readline/readline.h>
  35. #ifndef __USE_XOPEN_EXTENDED
  36. #define __USE_XOPEN_EXTENDED 1
  37. #endif
  38. #include <ftw.h>
  39. #ifdef DEBUG
  40. static int
  41. xprintf(int fd, const char *fmt, ...)
  42. {
  43. char buf[BUFSIZ];
  44. int r;
  45. va_list ap;
  46. va_start(ap, fmt);
  47. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  48. if (r > 0)
  49. r = write(fd, buf, r);
  50. va_end(ap);
  51. return r;
  52. }
  53. #define DEBUG_FD 8
  54. #define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
  55. #define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x)
  56. #define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x)
  57. #define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=0x%p\n", x)
  58. #else
  59. #define DPRINTF_D(x)
  60. #define DPRINTF_U(x)
  61. #define DPRINTF_S(x)
  62. #define DPRINTF_P(x)
  63. #endif /* DEBUG */
  64. #define VERSION "v1.1"
  65. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  66. #undef MIN
  67. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  68. #define ISODD(x) ((x) & 1)
  69. #define CONTROL(c) ((c) ^ 0x40)
  70. #define TOUPPER(ch) \
  71. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  72. #define MAX_CMD_LEN 5120
  73. #define CURSYM(flag) (flag ? CURSR : EMPTY)
  74. #define FILTER '/'
  75. #define MAX_BM 10
  76. struct assoc {
  77. char *regex; /* Regex to match on filename */
  78. char *mime; /* File type */
  79. };
  80. /* Supported actions */
  81. enum action {
  82. SEL_QUIT = 1,
  83. SEL_CDQUIT,
  84. SEL_BACK,
  85. SEL_GOIN,
  86. SEL_FLTR,
  87. SEL_MFLTR,
  88. SEL_SEARCH,
  89. SEL_NEXT,
  90. SEL_PREV,
  91. SEL_PGDN,
  92. SEL_PGUP,
  93. SEL_HOME,
  94. SEL_END,
  95. SEL_CD,
  96. SEL_CDHOME,
  97. SEL_CDBEGIN,
  98. SEL_CDLAST,
  99. SEL_CDBM,
  100. SEL_TOGGLEDOT,
  101. SEL_DETAIL,
  102. SEL_STATS,
  103. SEL_MEDIA,
  104. SEL_FMEDIA,
  105. SEL_DFB,
  106. SEL_FSIZE,
  107. SEL_BSIZE,
  108. SEL_MTIME,
  109. SEL_REDRAW,
  110. SEL_COPY,
  111. SEL_HELP,
  112. SEL_RUN,
  113. SEL_RUNARG,
  114. };
  115. struct key {
  116. int sym; /* Key pressed */
  117. enum action act; /* Action */
  118. char *run; /* Program to run */
  119. char *env; /* Environment variable to run */
  120. };
  121. #include "config.h"
  122. typedef struct entry {
  123. char name[NAME_MAX];
  124. mode_t mode;
  125. time_t t;
  126. off_t size;
  127. off_t bsize;
  128. } *pEntry;
  129. typedef struct {
  130. char *key;
  131. char *loc;
  132. } bm;
  133. typedef unsigned long ulong;
  134. /* Externs */
  135. #ifdef __APPLE__
  136. extern int add_history(const char *);
  137. #else
  138. extern void add_history(const char *string);
  139. #endif
  140. extern int wget_wch(WINDOW *, wint_t *);
  141. /* Global context */
  142. static struct entry *dents;
  143. static int ndents, cur, total_dents;
  144. static int idle;
  145. static char *player;
  146. static char *copier;
  147. static char *editor;
  148. static char *desktop_manager;
  149. static off_t blk_size;
  150. static size_t fs_free;
  151. static int open_max;
  152. static bm bookmark[MAX_BM];
  153. static const double div_2_pow_10 = 1.0 / 1024.0;
  154. static char *utils[] = {
  155. #ifdef __APPLE__
  156. "/usr/bin/open",
  157. #else
  158. "/usr/bin/xdg-open",
  159. #endif
  160. "nlay"
  161. };
  162. /* For use in functions which are isolated and don't return the buffer */
  163. static char g_buf[MAX_CMD_LEN];
  164. /*
  165. * Layout:
  166. * .---------
  167. * | cwd: /mnt/path
  168. * |
  169. * | file0
  170. * | file1
  171. * | > file2
  172. * | file3
  173. * | file4
  174. * ...
  175. * | filen
  176. * |
  177. * | Permission denied
  178. * '------
  179. */
  180. static void printmsg(char *);
  181. static void printwarn(void);
  182. static void printerr(int, char *);
  183. static void redraw(char *path);
  184. static rlim_t
  185. max_openfds()
  186. {
  187. struct rlimit rl;
  188. rlim_t limit;
  189. limit = getrlimit(RLIMIT_NOFILE, &rl);
  190. if (limit != 0)
  191. return 32;
  192. limit = rl.rlim_cur;
  193. rl.rlim_cur = rl.rlim_max;
  194. if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
  195. return rl.rlim_max - 64;
  196. if (limit > 128)
  197. return limit - 64;
  198. return 32;
  199. }
  200. /* Just a safe strncpy(3) */
  201. static void
  202. xstrlcpy(char *dest, const char *src, size_t n)
  203. {
  204. while (--n && (*dest++ = *src++));
  205. if (!n)
  206. *dest = '\0';
  207. }
  208. /*
  209. * The poor man's implementation of memrchr(3).
  210. * We are only looking for '/' in this program.
  211. */
  212. static void *
  213. xmemrchr(const void *s, unsigned char ch, size_t n)
  214. {
  215. static unsigned char *p;
  216. if (!s || !n)
  217. return NULL;
  218. p = (unsigned char *)s + n - 1;
  219. while (n--)
  220. if ((*p--) == ch)
  221. return ++p;
  222. return NULL;
  223. }
  224. /*
  225. * The following dirname(3) implementation does not
  226. * modify the input. We use a copy of the original.
  227. *
  228. * Modified from the glibc (GNU LGPL) version.
  229. */
  230. static char *
  231. xdirname(const char *path)
  232. {
  233. static char buf[PATH_MAX];
  234. static char *last_slash;
  235. xstrlcpy(buf, path, PATH_MAX);
  236. /* Find last '/'. */
  237. last_slash = strrchr(buf, '/');
  238. if (last_slash != NULL && last_slash != buf && last_slash[1] == '\0') {
  239. /* Determine whether all remaining characters are slashes. */
  240. char *runp;
  241. for (runp = last_slash; runp != buf; --runp)
  242. if (runp[-1] != '/')
  243. break;
  244. /* The '/' is the last character, we have to look further. */
  245. if (runp != buf)
  246. last_slash = xmemrchr(buf, '/', runp - buf);
  247. }
  248. if (last_slash != NULL) {
  249. /* Determine whether all remaining characters are slashes. */
  250. char *runp;
  251. for (runp = last_slash; runp != buf; --runp)
  252. if (runp[-1] != '/')
  253. break;
  254. /* Terminate the buffer. */
  255. if (runp == buf) {
  256. /* The last slash is the first character in the string.
  257. We have to return "/". As a special case we have to
  258. return "//" if there are exactly two slashes at the
  259. beginning of the string. See XBD 4.10 Path Name
  260. Resolution for more information. */
  261. if (last_slash == buf + 1)
  262. ++last_slash;
  263. else
  264. last_slash = buf + 1;
  265. } else
  266. last_slash = runp;
  267. last_slash[0] = '\0';
  268. } else {
  269. /* This assignment is ill-designed but the XPG specs require to
  270. return a string containing "." in any case no directory part
  271. is found and so a static and constant string is required. */
  272. buf[0] = '.';
  273. buf[1] = '\0';
  274. }
  275. return buf;
  276. }
  277. /*
  278. * Return number of dots of all chars in a string are dots, else 0
  279. */
  280. static int
  281. all_dots(const char* ptr)
  282. {
  283. if (!ptr)
  284. return FALSE;
  285. int count = 0;
  286. while (*ptr == '.') {
  287. count++;
  288. ptr++;
  289. }
  290. if (*ptr)
  291. return 0;
  292. return count;
  293. }
  294. /*
  295. * Spawns a child process. Behaviour can be controlled using flag:
  296. * Limited to 2 arguments to a program
  297. * flag works on bit set:
  298. * - 0b1: draw a marker to indicate nnn spawned e.g., a shell
  299. * - 0b10: do not wait in parent for child process e.g. DE file manager
  300. * - 0b100: suppress stdout and stderr
  301. * - 0b1000: restore default SIGINT handler
  302. */
  303. static void
  304. spawn(char *file, char *arg1, char *arg2, char *dir, unsigned char flag)
  305. {
  306. pid_t pid;
  307. int status;
  308. pid = fork();
  309. if (pid == 0) {
  310. if (dir != NULL)
  311. status = chdir(dir);
  312. /* Show a marker (to indicate nnn spawned shell) */
  313. if (flag & 0b1)
  314. fprintf(stdout, "\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
  315. /* Suppress stdout and stderr */
  316. if (flag & 0b100) {
  317. int fd = open("/dev/null", O_WRONLY, S_IWUSR);
  318. dup2(fd, 1);
  319. dup2(fd, 2);
  320. close(fd);
  321. }
  322. if (flag & 0b1000)
  323. signal(SIGINT, SIG_DFL);
  324. execlp(file, file, arg1, arg2, NULL);
  325. _exit(1);
  326. } else {
  327. if (!(flag & 0b10))
  328. /* Ignore interruptions */
  329. while (waitpid(pid, &status, 0) == -1)
  330. DPRINTF_D(status);
  331. DPRINTF_D(pid);
  332. }
  333. }
  334. static char *
  335. xgetenv(char *name, char *fallback)
  336. {
  337. char *value;
  338. if (name == NULL)
  339. return fallback;
  340. value = getenv(name);
  341. return value && value[0] ? value : fallback;
  342. }
  343. /*
  344. * We assume none of the strings are NULL.
  345. *
  346. * Let's have the logic to sort numeric names in numeric order.
  347. * E.g., the order '1, 10, 2' doesn't make sense to human eyes.
  348. *
  349. * If the absolute numeric values are same, we fallback to alphasort.
  350. */
  351. static int
  352. xstricmp(char *s1, char *s2)
  353. {
  354. static char *c1, *c2;
  355. c1 = s1;
  356. while (isspace(*c1))
  357. c1++;
  358. if (*c1 == '-' || *c1 == '+')
  359. c1++;
  360. while (*c1 >= '0' && *c1 <= '9')
  361. c1++;
  362. c2 = s2;
  363. while (isspace(*c2))
  364. c2++;
  365. if (*c2 == '-' || *c2 == '+')
  366. c2++;
  367. while(*c2 >= '0' && *c2 <= '9')
  368. c2++;
  369. if (*c1 == '\0' && *c2 == '\0') {
  370. static long long num1, num2;
  371. num1 = strtoll(s1, &c1, 10);
  372. num2 = strtoll(s2, &c2, 10);
  373. if (num1 != num2) {
  374. if (num1 > num2)
  375. return 1;
  376. else
  377. return -1;
  378. }
  379. } else if (*c1 == '\0' && *c2 != '\0')
  380. return -1;
  381. else if (*c1 != '\0' && *c2 == '\0')
  382. return 1;
  383. while (*s2 && *s1 && TOUPPER(*s1) == TOUPPER(*s2))
  384. s1++, s2++;
  385. /* In case of alphabetically same names, make sure
  386. lower case one comes before upper case one */
  387. if (!*s1 && !*s2)
  388. return 1;
  389. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  390. }
  391. /* Trim all whitespace from both ends, / from end */
  392. static char *
  393. strstrip(char *s)
  394. {
  395. if (!s || !*s)
  396. return s;
  397. size_t len = strlen(s) - 1;
  398. while (len != 0 && (isspace(s[len]) || s[len] == '/'))
  399. len--;
  400. s[len + 1] = '\0';
  401. while (*s && isspace(*s))
  402. s++;
  403. return s;
  404. }
  405. static char *
  406. getmime(char *file)
  407. {
  408. regex_t regex;
  409. unsigned int i;
  410. static unsigned int len = LEN(assocs);
  411. for (i = 0; i < len; i++) {
  412. if (regcomp(&regex, assocs[i].regex,
  413. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  414. continue;
  415. if (regexec(&regex, file, 0, NULL, 0) == 0)
  416. return assocs[i].mime;
  417. }
  418. return NULL;
  419. }
  420. static int
  421. setfilter(regex_t *regex, char *filter)
  422. {
  423. static size_t len;
  424. static int r;
  425. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  426. if (r != 0 && filter && filter[0] != '\0') {
  427. len = COLS;
  428. if (len > LINE_MAX)
  429. len = LINE_MAX;
  430. regerror(r, regex, g_buf, len);
  431. printmsg(g_buf);
  432. }
  433. return r;
  434. }
  435. static void
  436. initfilter(int dot, char **ifilter)
  437. {
  438. *ifilter = dot ? "." : "^[^.]";
  439. }
  440. static int
  441. visible(regex_t *regex, char *file)
  442. {
  443. return regexec(regex, file, 0, NULL, 0) == 0;
  444. }
  445. static int
  446. entrycmp(const void *va, const void *vb)
  447. {
  448. static pEntry pa, pb;
  449. pa = (pEntry)va;
  450. pb = (pEntry)vb;
  451. /* Sort directories first */
  452. if (S_ISDIR(pb->mode) && !S_ISDIR(pa->mode))
  453. return 1;
  454. else if (S_ISDIR(pa->mode) && !S_ISDIR(pb->mode))
  455. return -1;
  456. /* Do the actual sorting */
  457. if (mtimeorder)
  458. return pb->t - pa->t;
  459. if (sizeorder) {
  460. if (pb->size > pa->size)
  461. return 1;
  462. else if (pb->size < pa->size)
  463. return -1;
  464. }
  465. if (bsizeorder) {
  466. if (pb->bsize > pa->bsize)
  467. return 1;
  468. else if (pb->bsize < pa->bsize)
  469. return -1;
  470. }
  471. return xstricmp(pa->name, pb->name);
  472. }
  473. static void
  474. initcurses(void)
  475. {
  476. if (initscr() == NULL) {
  477. char *term = getenv("TERM");
  478. if (term != NULL)
  479. fprintf(stderr, "error opening terminal: %s\n", term);
  480. else
  481. fprintf(stderr, "failed to initialize curses\n");
  482. exit(1);
  483. }
  484. cbreak();
  485. noecho();
  486. nonl();
  487. intrflush(stdscr, FALSE);
  488. keypad(stdscr, TRUE);
  489. curs_set(FALSE); /* Hide cursor */
  490. start_color();
  491. use_default_colors();
  492. timeout(1000); /* One second */
  493. }
  494. static void
  495. exitcurses(void)
  496. {
  497. endwin(); /* Restore terminal */
  498. }
  499. /* Messages show up at the bottom */
  500. static void
  501. printmsg(char *msg)
  502. {
  503. move(LINES - 1, 0);
  504. printw("%s\n", msg);
  505. }
  506. /* Display warning as a message */
  507. static void
  508. printwarn(void)
  509. {
  510. printmsg(strerror(errno));
  511. }
  512. /* Kill curses and display error before exiting */
  513. static void
  514. printerr(int ret, char *prefix)
  515. {
  516. exitcurses();
  517. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  518. exit(ret);
  519. }
  520. /* Clear the last line */
  521. static void
  522. clearprompt(void)
  523. {
  524. printmsg("");
  525. }
  526. /* Print prompt on the last line */
  527. static void
  528. printprompt(char *str)
  529. {
  530. clearprompt();
  531. printw(str);
  532. }
  533. /* Returns SEL_* if key is bound and 0 otherwise.
  534. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}).
  535. * The next keyboard input can be simulated by presel.
  536. */
  537. static int
  538. nextsel(char **run, char **env, int *presel)
  539. {
  540. int c = *presel;
  541. unsigned int i;
  542. static unsigned int len = LEN(bindings);
  543. if (c == 0)
  544. c = getch();
  545. else
  546. *presel = 0;
  547. if (c == -1)
  548. idle++;
  549. else
  550. idle = 0;
  551. for (i = 0; i < len; i++)
  552. if (c == bindings[i].sym) {
  553. *run = bindings[i].run;
  554. *env = bindings[i].env;
  555. return bindings[i].act;
  556. }
  557. return 0;
  558. }
  559. /*
  560. * Move non-matching entries to the end
  561. */
  562. static void
  563. fill(struct entry **dents,
  564. int (*filter)(regex_t *, char *), regex_t *re)
  565. {
  566. static int count;
  567. for (count = 0; count < ndents; count++) {
  568. if (filter(re, (*dents)[count].name) == 0) {
  569. if (count != --ndents) {
  570. static struct entry _dent;
  571. /* Copy count to tmp */
  572. xstrlcpy(_dent.name, (*dents)[count].name, NAME_MAX);
  573. _dent.mode = (*dents)[count].mode;
  574. _dent.t = (*dents)[count].t;
  575. _dent.size = (*dents)[count].size;
  576. _dent.bsize = (*dents)[count].bsize;
  577. /* Copy ndents - 1 to count */
  578. xstrlcpy((*dents)[count].name, (*dents)[ndents].name, NAME_MAX);
  579. (*dents)[count].mode = (*dents)[ndents].mode;
  580. (*dents)[count].t = (*dents)[ndents].t;
  581. (*dents)[count].size = (*dents)[ndents].size;
  582. (*dents)[count].bsize = (*dents)[ndents].bsize;
  583. /* Copy tmp to ndents - 1 */
  584. xstrlcpy((*dents)[ndents].name, _dent.name, NAME_MAX);
  585. (*dents)[ndents].mode = _dent.mode;
  586. (*dents)[ndents].t = _dent.t;
  587. (*dents)[ndents].size = _dent.size;
  588. (*dents)[ndents].bsize = _dent.bsize;
  589. count--;
  590. }
  591. continue;
  592. }
  593. }
  594. }
  595. static int
  596. matches(char *fltr)
  597. {
  598. static regex_t re;
  599. /* Search filter */
  600. if (setfilter(&re, fltr) != 0)
  601. return -1;
  602. fill(&dents, visible, &re);
  603. qsort(dents, ndents, sizeof(*dents), entrycmp);
  604. return 0;
  605. }
  606. static int
  607. readln(char *path)
  608. {
  609. static char ln[LINE_MAX << 2];
  610. static wchar_t wln[LINE_MAX];
  611. static wint_t ch[2] = {0};
  612. int r, total = ndents;
  613. int oldcur = cur;
  614. int len = 1;
  615. char *pln = ln + 1;
  616. memset(wln, 0, LINE_MAX << 2);
  617. wln[0] = FILTER;
  618. ln[0] = FILTER;
  619. ln[1] = '\0';
  620. cur = 0;
  621. timeout(-1);
  622. echo();
  623. curs_set(TRUE);
  624. printprompt(ln);
  625. while ((r = wget_wch(stdscr, ch)) != ERR) {
  626. if (r == OK) {
  627. switch(*ch) {
  628. case '\r': // with nonl(), this is ENTER key value
  629. if (len == 1) {
  630. cur = oldcur;
  631. goto end;
  632. }
  633. if (matches(pln) == -1)
  634. goto end;
  635. redraw(path);
  636. goto end;
  637. case 127: // handle DEL
  638. if (len == 1) {
  639. cur = oldcur;
  640. *ch = CONTROL('L');
  641. goto end;
  642. }
  643. if (len == 2)
  644. cur = oldcur;
  645. wln[--len] = '\0';
  646. wcstombs(ln, wln, LINE_MAX << 2);
  647. ndents = total;
  648. if (matches(pln) == -1) {
  649. printprompt(ln);
  650. continue;
  651. }
  652. redraw(path);
  653. printprompt(ln);
  654. break;
  655. case CONTROL('L'):
  656. if (len == 1)
  657. cur = oldcur; // fallthrough
  658. case CONTROL('Q'):
  659. goto end;
  660. default:
  661. wln[len++] = (wchar_t)*ch;
  662. wln[len] = '\0';
  663. wcstombs(ln, wln, LINE_MAX << 2);
  664. ndents = total;
  665. if (matches(pln) == -1)
  666. continue;
  667. redraw(path);
  668. printprompt(ln);
  669. }
  670. } else {
  671. switch(*ch) {
  672. case KEY_DC: // fallthrough
  673. case KEY_BACKSPACE:
  674. if (len == 1) {
  675. cur = oldcur;
  676. *ch = CONTROL('L');
  677. goto end;
  678. }
  679. if (len == 2)
  680. cur = oldcur;
  681. wln[--len] = '\0';
  682. wcstombs(ln, wln, LINE_MAX << 2);
  683. ndents = total;
  684. if (matches(pln) == -1)
  685. continue;
  686. redraw(path);
  687. printprompt(ln);
  688. break;
  689. case KEY_DOWN: // fallthrough
  690. case KEY_UP: // fallthrough
  691. case KEY_LEFT: // fallthrough
  692. case KEY_RIGHT:
  693. if (len == 1)
  694. cur = oldcur; // fallthrough
  695. default:
  696. goto end;
  697. }
  698. }
  699. }
  700. end:
  701. noecho();
  702. curs_set(FALSE);
  703. timeout(1000);
  704. /* Return keys for navigation etc. */
  705. return *ch;
  706. }
  707. static int
  708. canopendir(char *path)
  709. {
  710. static DIR *dirp;
  711. dirp = opendir(path);
  712. if (dirp == NULL)
  713. return 0;
  714. closedir(dirp);
  715. return 1;
  716. }
  717. /*
  718. * Returns "dir/name or "/name"
  719. */
  720. static char *
  721. mkpath(char *dir, char *name, char *out, size_t n)
  722. {
  723. /* Handle absolute path */
  724. if (name[0] == '/')
  725. xstrlcpy(out, name, n);
  726. else {
  727. /* Handle root case */
  728. if (strcmp(dir, "/") == 0)
  729. snprintf(out, n, "/%s", name);
  730. else
  731. snprintf(out, n, "%s/%s", dir, name);
  732. }
  733. return out;
  734. }
  735. static void
  736. parsebmstr(char *bms)
  737. {
  738. int i = 0;
  739. while (*bms && i < MAX_BM) {
  740. bookmark[i].key = bms;
  741. bms++;
  742. while (*bms && *bms != ':')
  743. bms++;
  744. if (!*bms) {
  745. bookmark[i].key = NULL;
  746. break;
  747. }
  748. *bms = '\0';
  749. bookmark[i].loc = ++bms;
  750. if (bookmark[i].loc[0] == '\0' || bookmark[i].loc[0] == ';') {
  751. bookmark[i].key = NULL;
  752. break;
  753. }
  754. while (*bms && *bms != ';')
  755. bms++;
  756. if (*bms)
  757. *bms = '\0';
  758. else
  759. break;
  760. bms++;
  761. i++;
  762. }
  763. }
  764. static char *
  765. readinput(void)
  766. {
  767. timeout(-1);
  768. echo();
  769. curs_set(TRUE);
  770. memset(g_buf, 0, LINE_MAX);
  771. wgetnstr(stdscr, g_buf, LINE_MAX - 1);
  772. noecho();
  773. curs_set(FALSE);
  774. timeout(1000);
  775. return g_buf[0] ? g_buf : NULL;
  776. }
  777. static char *
  778. replace_escape(const char *str)
  779. {
  780. static char buffer[PATH_MAX];
  781. static wchar_t wbuf[PATH_MAX];
  782. static wchar_t *buf;
  783. buffer[0] = '\0';
  784. buf = wbuf;
  785. /* Convert multi-byte to wide char */
  786. mbstowcs(wbuf, str, PATH_MAX);
  787. while (*buf) {
  788. if (*buf <= '\x1f' || *buf == '\x7f')
  789. *buf = '\?';
  790. buf++;
  791. }
  792. /* Convert wide char to multi-byte */
  793. wcstombs(buffer, wbuf, PATH_MAX);
  794. return buffer;
  795. }
  796. static void
  797. printent(struct entry *ent, int active)
  798. {
  799. static int ncols;
  800. if (COLS > PATH_MAX + 16)
  801. ncols = PATH_MAX + 16;
  802. else
  803. ncols = COLS;
  804. if (S_ISDIR(ent->mode))
  805. snprintf(g_buf, ncols, "%s%s/", CURSYM(active),
  806. replace_escape(ent->name));
  807. else if (S_ISLNK(ent->mode))
  808. snprintf(g_buf, ncols, "%s%s@", CURSYM(active),
  809. replace_escape(ent->name));
  810. else if (S_ISSOCK(ent->mode))
  811. snprintf(g_buf, ncols, "%s%s=", CURSYM(active),
  812. replace_escape(ent->name));
  813. else if (S_ISFIFO(ent->mode))
  814. snprintf(g_buf, ncols, "%s%s|", CURSYM(active),
  815. replace_escape(ent->name));
  816. else if (ent->mode & S_IXUSR)
  817. snprintf(g_buf, ncols, "%s%s*", CURSYM(active),
  818. replace_escape(ent->name));
  819. else
  820. snprintf(g_buf, ncols, "%s%s", CURSYM(active),
  821. replace_escape(ent->name));
  822. printw("%s\n", g_buf);
  823. }
  824. static char*
  825. coolsize(off_t size)
  826. {
  827. static const char *size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  828. static char size_buf[12]; /* Buffer to hold human readable size */
  829. static int i;
  830. static off_t tmp;
  831. static long double rem;
  832. i = 0;
  833. rem = 0;
  834. while (size > 1024) {
  835. tmp = size;
  836. size >>= 10;
  837. rem = tmp - (size << 10);
  838. i++;
  839. }
  840. snprintf(size_buf, 12, "%.*Lf%s", i, size + rem * div_2_pow_10, size_units[i]);
  841. return size_buf;
  842. }
  843. static void
  844. printent_long(struct entry *ent, int active)
  845. {
  846. static int ncols;
  847. static char buf[18];
  848. if (COLS > PATH_MAX + 32)
  849. ncols = PATH_MAX + 32;
  850. else
  851. ncols = COLS;
  852. strftime(buf, 18, "%d %m %Y %H:%M", localtime(&ent->t));
  853. if (active)
  854. attron(A_REVERSE);
  855. if (!bsizeorder) {
  856. if (S_ISDIR(ent->mode))
  857. snprintf(g_buf, ncols, "%s%-16.16s / %s/",
  858. CURSYM(active), buf, replace_escape(ent->name));
  859. else if (S_ISLNK(ent->mode))
  860. snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
  861. CURSYM(active), buf, replace_escape(ent->name));
  862. else if (S_ISSOCK(ent->mode))
  863. snprintf(g_buf, ncols, "%s%-16.16s = %s=",
  864. CURSYM(active), buf, replace_escape(ent->name));
  865. else if (S_ISFIFO(ent->mode))
  866. snprintf(g_buf, ncols, "%s%-16.16s | %s|",
  867. CURSYM(active), buf, replace_escape(ent->name));
  868. else if (S_ISBLK(ent->mode))
  869. snprintf(g_buf, ncols, "%s%-16.16s b %s",
  870. CURSYM(active), buf, replace_escape(ent->name));
  871. else if (S_ISCHR(ent->mode))
  872. snprintf(g_buf, ncols, "%s%-16.16s c %s",
  873. CURSYM(active), buf, replace_escape(ent->name));
  874. else if (ent->mode & S_IXUSR)
  875. snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
  876. CURSYM(active), buf, coolsize(ent->size),
  877. replace_escape(ent->name));
  878. else
  879. snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
  880. CURSYM(active), buf, coolsize(ent->size),
  881. replace_escape(ent->name));
  882. } else {
  883. if (S_ISDIR(ent->mode))
  884. snprintf(g_buf, ncols, "%s%-16.16s %8.8s/ %s/",
  885. CURSYM(active), buf, coolsize(ent->bsize << 9),
  886. replace_escape(ent->name));
  887. else if (S_ISLNK(ent->mode))
  888. snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
  889. CURSYM(active), buf,
  890. replace_escape(ent->name));
  891. else if (S_ISSOCK(ent->mode))
  892. snprintf(g_buf, ncols, "%s%-16.16s = %s=",
  893. CURSYM(active), buf,
  894. replace_escape(ent->name));
  895. else if (S_ISFIFO(ent->mode))
  896. snprintf(g_buf, ncols, "%s%-16.16s | %s|",
  897. CURSYM(active), buf,
  898. replace_escape(ent->name));
  899. else if (S_ISBLK(ent->mode))
  900. snprintf(g_buf, ncols, "%s%-16.16s b %s",
  901. CURSYM(active), buf,
  902. replace_escape(ent->name));
  903. else if (S_ISCHR(ent->mode))
  904. snprintf(g_buf, ncols, "%s%-16.16s c %s",
  905. CURSYM(active), buf,
  906. replace_escape(ent->name));
  907. else if (ent->mode & S_IXUSR)
  908. snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
  909. CURSYM(active), buf, coolsize(ent->bsize << 9),
  910. replace_escape(ent->name));
  911. else
  912. snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
  913. CURSYM(active), buf, coolsize(ent->bsize << 9),
  914. replace_escape(ent->name));
  915. }
  916. printw("%s\n", g_buf);
  917. if (active)
  918. attroff(A_REVERSE);
  919. }
  920. static void (*printptr)(struct entry *ent, int active) = &printent_long;
  921. static char
  922. get_fileind(mode_t mode, char *desc)
  923. {
  924. static char c;
  925. if (S_ISREG(mode)) {
  926. c = '-';
  927. sprintf(desc, "%s", "regular file");
  928. if (mode & S_IXUSR)
  929. strcat(desc, ", executable");
  930. } else if (S_ISDIR(mode)) {
  931. c = 'd';
  932. sprintf(desc, "%s", "directory");
  933. } else if (S_ISBLK(mode)) {
  934. c = 'b';
  935. sprintf(desc, "%s", "block special device");
  936. } else if (S_ISCHR(mode)) {
  937. c = 'c';
  938. sprintf(desc, "%s", "character special device");
  939. #ifdef S_ISFIFO
  940. } else if (S_ISFIFO(mode)) {
  941. c = 'p';
  942. sprintf(desc, "%s", "FIFO");
  943. #endif /* S_ISFIFO */
  944. #ifdef S_ISLNK
  945. } else if (S_ISLNK(mode)) {
  946. c = 'l';
  947. sprintf(desc, "%s", "symbolic link");
  948. #endif /* S_ISLNK */
  949. #ifdef S_ISSOCK
  950. } else if (S_ISSOCK(mode)) {
  951. c = 's';
  952. sprintf(desc, "%s", "socket");
  953. #endif /* S_ISSOCK */
  954. #ifdef S_ISDOOR
  955. /* Solaris 2.6, etc. */
  956. } else if (S_ISDOOR(mode)) {
  957. c = 'D';
  958. desc[0] = '\0';
  959. #endif /* S_ISDOOR */
  960. } else {
  961. /* Unknown type -- possibly a regular file? */
  962. c = '?';
  963. desc[0] = '\0';
  964. }
  965. return(c);
  966. }
  967. /* Convert a mode field into "ls -l" type perms field. */
  968. static char *
  969. get_lsperms(mode_t mode, char *desc)
  970. {
  971. static const char *rwx[] = {"---", "--x", "-w-", "-wx",
  972. "r--", "r-x", "rw-", "rwx"};
  973. static char bits[11];
  974. bits[0] = get_fileind(mode, desc);
  975. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  976. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  977. strcpy(&bits[7], rwx[(mode & 7)]);
  978. if (mode & S_ISUID)
  979. bits[3] = (mode & S_IXUSR) ? 's' : 'S';
  980. if (mode & S_ISGID)
  981. bits[6] = (mode & S_IXGRP) ? 's' : 'l';
  982. if (mode & S_ISVTX)
  983. bits[9] = (mode & S_IXOTH) ? 't' : 'T';
  984. bits[10] = '\0';
  985. return(bits);
  986. }
  987. /*
  988. * Gets only a single line (that's what we need
  989. * for now) or shows full command output in pager.
  990. *
  991. * If pager is valid, returns NULL
  992. */
  993. static char *
  994. get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2, int pager)
  995. {
  996. pid_t pid;
  997. int pipefd[2];
  998. FILE* pf;
  999. int status;
  1000. char *ret = NULL;
  1001. if (pipe(pipefd) == -1)
  1002. printerr(1, "pipe(2)");
  1003. pid = fork();
  1004. if (pid == 0) {
  1005. /* In child */
  1006. close(pipefd[0]);
  1007. dup2(pipefd[1], STDOUT_FILENO);
  1008. dup2(pipefd[1], STDERR_FILENO);
  1009. execlp(file, file, arg1, arg2, NULL);
  1010. _exit(1);
  1011. }
  1012. /* In parent */
  1013. waitpid(pid, &status, 0);
  1014. close(pipefd[1]);
  1015. if (!pager) {
  1016. if ((pf = fdopen(pipefd[0], "r"))) {
  1017. ret = fgets(buf, bytes, pf);
  1018. close(pipefd[0]);
  1019. }
  1020. return ret;
  1021. }
  1022. pid = fork();
  1023. if (pid == 0) {
  1024. /* Show in pager in child */
  1025. dup2(pipefd[0], STDIN_FILENO);
  1026. execlp("less", "less", NULL);
  1027. close(pipefd[0]);
  1028. _exit(1);
  1029. }
  1030. /* In parent */
  1031. waitpid(pid, &status, 0);
  1032. return NULL;
  1033. }
  1034. /*
  1035. * Follows the stat(1) output closely
  1036. */
  1037. static int
  1038. show_stats(char* fpath, char* fname, struct stat *sb)
  1039. {
  1040. char *perms = get_lsperms(sb->st_mode, g_buf);
  1041. char *p, *begin = g_buf;
  1042. char tmp[] = "/tmp/nnnXXXXXX";
  1043. int fd = mkstemp(tmp);
  1044. if (fd == -1)
  1045. return -1;
  1046. /* Show file name or 'symlink' -> 'target' */
  1047. if (perms[0] == 'l') {
  1048. /* Note that MAX_CMD_LEN > PATH_MAX */
  1049. ssize_t len = readlink(fpath, g_buf, MAX_CMD_LEN);
  1050. if (len != -1) {
  1051. g_buf[len] = '\0';
  1052. dprintf(fd, " File: '%s' -> ",
  1053. replace_escape(fname));
  1054. dprintf(fd, "'%s'",
  1055. replace_escape(g_buf));
  1056. }
  1057. } else
  1058. dprintf(fd, " File: '%s'", replace_escape(fname));
  1059. /* Show size, blocks, file type */
  1060. #ifdef __APPLE__
  1061. dprintf(fd, "\n Size: %-15lld Blocks: %-10lld IO Block: %-6d %s",
  1062. #else
  1063. dprintf(fd, "\n Size: %-15ld Blocks: %-10ld IO Block: %-6ld %s",
  1064. #endif
  1065. sb->st_size, sb->st_blocks, sb->st_blksize, g_buf);
  1066. /* Show containing device, inode, hardlink count */
  1067. #ifdef __APPLE__
  1068. sprintf(g_buf, "%xh/%ud", sb->st_dev, sb->st_dev);
  1069. dprintf(fd, "\n Device: %-15s Inode: %-11llu Links: %-9hu",
  1070. #else
  1071. sprintf(g_buf, "%lxh/%lud", sb->st_dev, sb->st_dev);
  1072. dprintf(fd, "\n Device: %-15s Inode: %-11lu Links: %-9lu",
  1073. #endif
  1074. g_buf, sb->st_ino, sb->st_nlink);
  1075. /* Show major, minor number for block or char device */
  1076. if (perms[0] == 'b' || perms[0] == 'c')
  1077. dprintf(fd, " Device type: %x,%x",
  1078. major(sb->st_rdev), minor(sb->st_rdev));
  1079. /* Show permissions, owner, group */
  1080. dprintf(fd, "\n Access: 0%d%d%d/%s Uid: (%u/%s) Gid: (%u/%s)",
  1081. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  1082. perms,
  1083. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  1084. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  1085. /* Show last access time */
  1086. strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  1087. dprintf(fd, "\n\n Access: %s", g_buf);
  1088. /* Show last modification time */
  1089. strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  1090. dprintf(fd, "\n Modify: %s", g_buf);
  1091. /* Show last status change time */
  1092. strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  1093. dprintf(fd, "\n Change: %s", g_buf);
  1094. if (S_ISREG(sb->st_mode)) {
  1095. /* Show file(1) output */
  1096. p = get_output(g_buf, MAX_CMD_LEN, "file", "-b", fpath, 0);
  1097. if (p) {
  1098. dprintf(fd, "\n\n ");
  1099. while (*p) {
  1100. if (*p == ',') {
  1101. *p = '\0';
  1102. dprintf(fd, " %s\n", begin);
  1103. begin = p + 1;
  1104. }
  1105. p++;
  1106. }
  1107. dprintf(fd, " %s", begin);
  1108. }
  1109. dprintf(fd, "\n\n");
  1110. } else
  1111. dprintf(fd, "\n\n\n");
  1112. close(fd);
  1113. get_output(NULL, 0, "cat", tmp, NULL, 1);
  1114. unlink(tmp);
  1115. return 0;
  1116. }
  1117. static int
  1118. show_mediainfo(char* fpath, char *arg)
  1119. {
  1120. if (get_output(g_buf, MAX_CMD_LEN, "which", "mediainfo", NULL, 0) == NULL)
  1121. return -1;
  1122. exitcurses();
  1123. get_output(NULL, 0, "mediainfo", fpath, arg, 1);
  1124. initcurses();
  1125. return 0;
  1126. }
  1127. static int
  1128. show_help(void)
  1129. {
  1130. static char helpstr[] = ("echo \"\
  1131. Key | Function\n\
  1132. -+-\n\
  1133. Up, k, ^P | Previous entry\n\
  1134. Down, j, ^N | Next entry\n\
  1135. PgUp, ^U | Scroll half page up\n\
  1136. PgDn, ^D | Scroll half page down\n\
  1137. Home, g, ^, ^A | Jump to first entry\n\
  1138. End, G, $, ^E | Jump to last entry\n\
  1139. Right, Enter, l, ^M | Open file or enter dir\n\
  1140. Left, Bksp, h, ^H | Go to parent dir\n\
  1141. Insert | Toggle navigate-as-you-type mode\n\
  1142. ~ | Jump to HOME dir\n\
  1143. & | Jump to initial dir\n\
  1144. - | Jump to last visited dir\n\
  1145. / | Filter dir contents\n\
  1146. ^/ | Search dir in desktop search tool\n\
  1147. . | Toggle hide .dot files\n\
  1148. b | Show bookmark key prompt\n\
  1149. c | Show change dir prompt\n\
  1150. d | Toggle detail view\n\
  1151. D | Toggle current file details screen\n\
  1152. m | Show concise mediainfo\n\
  1153. M | Show full mediainfo\n\
  1154. s | Toggle sort by file size\n\
  1155. S | Toggle disk usage analyzer mode\n\
  1156. t | Toggle sort by modified time\n\
  1157. ! | Spawn SHELL in PWD (fallback sh)\n\
  1158. e | Edit entry in EDITOR (fallback vi)\n\
  1159. o | Open dir in NNN_DE_FILE_MANAGER\n\
  1160. p | Open entry in PAGER (fallback less)\n\
  1161. ^K | Invoke file path copier\n\
  1162. ^L | Force a redraw, exit filter prompt\n\
  1163. ? | Toggle help screen\n\
  1164. Q | Quit and change directory\n\
  1165. q, ^Q | Quit\n\n\" | less");
  1166. return system(helpstr);
  1167. }
  1168. static int
  1169. sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  1170. {
  1171. if (typeflag == FTW_F || typeflag == FTW_D)
  1172. blk_size += sb->st_blocks;
  1173. return 0;
  1174. }
  1175. static int
  1176. getorder(size_t size)
  1177. {
  1178. switch (size) {
  1179. case 4096:
  1180. return 12;
  1181. case 512:
  1182. return 9;
  1183. case 8192:
  1184. return 13;
  1185. case 16384:
  1186. return 14;
  1187. case 32768:
  1188. return 15;
  1189. case 65536:
  1190. return 16;
  1191. case 131072:
  1192. return 17;
  1193. case 262144:
  1194. return 18;
  1195. case 524288:
  1196. return 19;
  1197. case 1048576:
  1198. return 20;
  1199. case 2048:
  1200. return 11;
  1201. case 1024:
  1202. return 10;
  1203. default:
  1204. return 0;
  1205. }
  1206. }
  1207. static int
  1208. dentfill(char *path, struct entry **dents,
  1209. int (*filter)(regex_t *, char *), regex_t *re)
  1210. {
  1211. static char newpath[PATH_MAX];
  1212. static DIR *dirp;
  1213. static struct dirent *dp;
  1214. static struct stat sb;
  1215. static int n;
  1216. n = 0;
  1217. dirp = opendir(path);
  1218. if (dirp == NULL)
  1219. return 0;
  1220. while ((dp = readdir(dirp)) != NULL) {
  1221. /* Skip self and parent */
  1222. if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
  1223. (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
  1224. continue;
  1225. if (filter(re, dp->d_name) == 0)
  1226. continue;
  1227. if (n == total_dents) {
  1228. total_dents += 64;
  1229. *dents = realloc(*dents, total_dents * sizeof(**dents));
  1230. if (*dents == NULL)
  1231. printerr(1, "realloc");
  1232. }
  1233. xstrlcpy((*dents)[n].name, dp->d_name, NAME_MAX);
  1234. /* Get mode flags */
  1235. mkpath(path, dp->d_name, newpath, PATH_MAX);
  1236. if (lstat(newpath, &sb) == -1) {
  1237. if (*dents)
  1238. free(*dents);
  1239. printerr(1, "lstat");
  1240. }
  1241. (*dents)[n].mode = sb.st_mode;
  1242. (*dents)[n].t = sb.st_mtime;
  1243. (*dents)[n].size = sb.st_size;
  1244. if (bsizeorder) {
  1245. if (S_ISDIR(sb.st_mode)) {
  1246. blk_size = 0;
  1247. if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
  1248. printmsg("nftw(3) failed");
  1249. (*dents)[n].bsize = sb.st_blocks;
  1250. } else
  1251. (*dents)[n].bsize = blk_size;
  1252. } else
  1253. (*dents)[n].bsize = sb.st_blocks;
  1254. }
  1255. n++;
  1256. }
  1257. if (bsizeorder) {
  1258. static struct statvfs svb;
  1259. if (statvfs(path, &svb) == -1)
  1260. fs_free = 0;
  1261. else
  1262. fs_free = svb.f_bavail << getorder(svb.f_bsize);
  1263. }
  1264. /* Should never be null */
  1265. if (closedir(dirp) == -1) {
  1266. if (*dents)
  1267. free(*dents);
  1268. printerr(1, "closedir");
  1269. }
  1270. return n;
  1271. }
  1272. static void
  1273. dentfree(struct entry *dents)
  1274. {
  1275. free(dents);
  1276. }
  1277. /* Return the position of the matching entry or 0 otherwise */
  1278. static int
  1279. dentfind(struct entry *dents, int n, char *path)
  1280. {
  1281. if (!path)
  1282. return 0;
  1283. static int i;
  1284. static char *p;
  1285. p = basename(path);
  1286. DPRINTF_S(p);
  1287. for (i = 0; i < n; i++)
  1288. if (strcmp(p, dents[i].name) == 0)
  1289. return i;
  1290. return 0;
  1291. }
  1292. static int
  1293. populate(char *path, char *oldpath, char *fltr)
  1294. {
  1295. static regex_t re;
  1296. /* Can fail when permissions change while browsing */
  1297. if (canopendir(path) == 0)
  1298. return -1;
  1299. /* Search filter */
  1300. if (setfilter(&re, fltr) != 0)
  1301. return -1;
  1302. ndents = dentfill(path, &dents, visible, &re);
  1303. qsort(dents, ndents, sizeof(*dents), entrycmp);
  1304. /* Find cur from history */
  1305. cur = dentfind(dents, ndents, oldpath);
  1306. return 0;
  1307. }
  1308. static void
  1309. redraw(char *path)
  1310. {
  1311. static char cwd[PATH_MAX];
  1312. static int nlines, i;
  1313. static size_t ncols;
  1314. nlines = MIN(LINES - 4, ndents);
  1315. /* Clean screen */
  1316. erase();
  1317. /* Strip trailing slashes */
  1318. for (i = strlen(path) - 1; i > 0; i--)
  1319. if (path[i] == '/')
  1320. path[i] = '\0';
  1321. else
  1322. break;
  1323. DPRINTF_D(cur);
  1324. DPRINTF_S(path);
  1325. /* No text wrapping in cwd line */
  1326. if (!realpath(path, cwd)) {
  1327. printmsg("Cannot resolve path");
  1328. return;
  1329. }
  1330. ncols = COLS;
  1331. if (ncols > PATH_MAX)
  1332. ncols = PATH_MAX;
  1333. cwd[ncols - strlen(CWD) - 1] = '\0';
  1334. printw(CWD "%s\n\n", cwd);
  1335. /* Print listing */
  1336. if (cur < (nlines >> 1)) {
  1337. for (i = 0; i < nlines; i++)
  1338. printptr(&dents[i], i == cur);
  1339. } else if (cur >= ndents - (nlines >> 1)) {
  1340. for (i = ndents - nlines; i < ndents; i++)
  1341. printptr(&dents[i], i == cur);
  1342. } else {
  1343. static int odd;
  1344. odd = ISODD(nlines);
  1345. nlines >>= 1;
  1346. for (i = cur - nlines; i < cur + nlines + odd; i++)
  1347. printptr(&dents[i], i == cur);
  1348. }
  1349. if (showdetail) {
  1350. if (ndents) {
  1351. static char ind[2] = "\0\0";
  1352. static char sort[17];
  1353. if (mtimeorder)
  1354. sprintf(sort, "by time ");
  1355. else if (sizeorder)
  1356. sprintf(sort, "by size ");
  1357. else
  1358. sort[0] = '\0';
  1359. if (S_ISDIR(dents[cur].mode))
  1360. ind[0] = '/';
  1361. else if (S_ISLNK(dents[cur].mode))
  1362. ind[0] = '@';
  1363. else if (S_ISSOCK(dents[cur].mode))
  1364. ind[0] = '=';
  1365. else if (S_ISFIFO(dents[cur].mode))
  1366. ind[0] = '|';
  1367. else if (dents[cur].mode & S_IXUSR)
  1368. ind[0] = '*';
  1369. else
  1370. ind[0] = '\0';
  1371. if (!bsizeorder)
  1372. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  1373. replace_escape(dents[cur].name), ind);
  1374. else
  1375. sprintf(cwd, "total %d by disk usage, %s free [%s%s]",
  1376. ndents, coolsize(fs_free),
  1377. replace_escape(dents[cur].name), ind);
  1378. printmsg(cwd);
  1379. } else
  1380. printmsg("0 items");
  1381. }
  1382. }
  1383. static void
  1384. browse(char *ipath, char *ifilter)
  1385. {
  1386. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  1387. static char lastdir[PATH_MAX];
  1388. static char fltr[LINE_MAX];
  1389. char *mime, *dir, *tmp, *run, *env;
  1390. struct stat sb;
  1391. int r, fd, presel;
  1392. enum action sel = SEL_RUNARG + 1;
  1393. xstrlcpy(path, ipath, PATH_MAX);
  1394. xstrlcpy(fltr, ifilter, LINE_MAX);
  1395. oldpath[0] = '\0';
  1396. newpath[0] = '\0';
  1397. lastdir[0] = '\0'; /* Can't move back from initial directory */
  1398. if (filtermode)
  1399. presel = FILTER;
  1400. else
  1401. presel = 0;
  1402. begin:
  1403. if (populate(path, oldpath, fltr) == -1) {
  1404. printwarn();
  1405. goto nochange;
  1406. }
  1407. for (;;) {
  1408. redraw(path);
  1409. nochange:
  1410. /* Exit if parent has exited */
  1411. if (getppid() == 1)
  1412. _exit(0);
  1413. sel = nextsel(&run, &env, &presel);
  1414. switch (sel) {
  1415. case SEL_CDQUIT:
  1416. {
  1417. char *tmpfile = "/tmp/nnn";
  1418. if ((tmp = getenv("NNN_TMPFILE")) != NULL)
  1419. tmpfile = tmp;
  1420. FILE *fp = fopen(tmpfile, "w");
  1421. if (fp) {
  1422. fprintf(fp, "cd \"%s\"", path);
  1423. fclose(fp);
  1424. }
  1425. /* Fall through to exit */
  1426. } // fallthrough
  1427. case SEL_QUIT:
  1428. dentfree(dents);
  1429. return;
  1430. case SEL_BACK:
  1431. /* There is no going back */
  1432. if (path[0] == '/' && path[1] == '\0') {
  1433. printmsg("You are at /");
  1434. goto nochange;
  1435. }
  1436. dir = xdirname(path);
  1437. if (canopendir(dir) == 0) {
  1438. printwarn();
  1439. goto nochange;
  1440. }
  1441. /* Save history */
  1442. xstrlcpy(oldpath, path, PATH_MAX);
  1443. /* Save last working directory */
  1444. xstrlcpy(lastdir, path, PATH_MAX);
  1445. xstrlcpy(path, dir, PATH_MAX);
  1446. /* Reset filter */
  1447. xstrlcpy(fltr, ifilter, LINE_MAX);
  1448. if (filtermode)
  1449. presel = FILTER;
  1450. goto begin;
  1451. case SEL_GOIN:
  1452. /* Cannot descend in empty directories */
  1453. if (ndents == 0)
  1454. goto begin;
  1455. mkpath(path, dents[cur].name, newpath, PATH_MAX);
  1456. DPRINTF_S(newpath);
  1457. /* Get path info */
  1458. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1459. if (fd == -1) {
  1460. printwarn();
  1461. goto nochange;
  1462. }
  1463. r = fstat(fd, &sb);
  1464. if (r == -1) {
  1465. printwarn();
  1466. close(fd);
  1467. goto nochange;
  1468. }
  1469. close(fd);
  1470. DPRINTF_U(sb.st_mode);
  1471. switch (sb.st_mode & S_IFMT) {
  1472. case S_IFDIR:
  1473. if (canopendir(newpath) == 0) {
  1474. printwarn();
  1475. goto nochange;
  1476. }
  1477. /* Save last working directory */
  1478. xstrlcpy(lastdir, path, PATH_MAX);
  1479. xstrlcpy(path, newpath, PATH_MAX);
  1480. oldpath[0] = '\0';
  1481. /* Reset filter */
  1482. xstrlcpy(fltr, ifilter, LINE_MAX);
  1483. if (filtermode)
  1484. presel = FILTER;
  1485. goto begin;
  1486. case S_IFREG:
  1487. {
  1488. /* If NNN_USE_EDITOR is set, open text in EDITOR */
  1489. if (editor) {
  1490. mime = getmime(dents[cur].name);
  1491. if (mime) {
  1492. exitcurses();
  1493. spawn(editor, newpath, NULL, NULL, 0);
  1494. initcurses();
  1495. continue;
  1496. }
  1497. /* Recognize and open plain text files with vi */
  1498. if (get_output(g_buf, MAX_CMD_LEN, "file", "-bi",
  1499. newpath, 0) == NULL)
  1500. continue;
  1501. if (strstr(g_buf, "text/") == g_buf) {
  1502. exitcurses();
  1503. spawn(editor, newpath, NULL, NULL, 0);
  1504. initcurses();
  1505. continue;
  1506. }
  1507. }
  1508. /* Invoke desktop opener as last resort */
  1509. spawn(utils[0], newpath, NULL, NULL, 4);
  1510. continue;
  1511. }
  1512. default:
  1513. printmsg("Unsupported file");
  1514. goto nochange;
  1515. }
  1516. case SEL_FLTR:
  1517. presel = readln(path);
  1518. xstrlcpy(fltr, ifilter, LINE_MAX);
  1519. DPRINTF_S(fltr);
  1520. /* Save current */
  1521. if (ndents > 0)
  1522. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1523. goto nochange;
  1524. case SEL_MFLTR:
  1525. filtermode = !filtermode;
  1526. if (filtermode)
  1527. presel = FILTER;
  1528. else
  1529. printmsg("navigate-as-you-type off");
  1530. goto nochange;
  1531. case SEL_SEARCH:
  1532. exitcurses();
  1533. spawn(player, path, "search", NULL, 0);
  1534. initcurses();
  1535. break;
  1536. case SEL_NEXT:
  1537. if (cur < ndents - 1)
  1538. cur++;
  1539. else if (ndents)
  1540. /* Roll over, set cursor to first entry */
  1541. cur = 0;
  1542. break;
  1543. case SEL_PREV:
  1544. if (cur > 0)
  1545. cur--;
  1546. else if (ndents)
  1547. /* Roll over, set cursor to last entry */
  1548. cur = ndents - 1;
  1549. break;
  1550. case SEL_PGDN:
  1551. if (cur < ndents - 1)
  1552. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1553. break;
  1554. case SEL_PGUP:
  1555. if (cur > 0)
  1556. cur -= MIN((LINES - 4) / 2, cur);
  1557. break;
  1558. case SEL_HOME:
  1559. cur = 0;
  1560. break;
  1561. case SEL_END:
  1562. cur = ndents - 1;
  1563. break;
  1564. case SEL_CD:
  1565. {
  1566. static char *input;
  1567. static int truecd;
  1568. /* Save the program start dir */
  1569. tmp = getcwd(newpath, PATH_MAX);
  1570. if (tmp == NULL) {
  1571. printwarn();
  1572. goto nochange;
  1573. }
  1574. /* Switch to current path for readline(3) */
  1575. if (chdir(path) == -1) {
  1576. printwarn();
  1577. goto nochange;
  1578. }
  1579. exitcurses();
  1580. tmp = readline("chdir: ");
  1581. initcurses();
  1582. /* Change back to program start dir */
  1583. if (chdir(newpath) == -1)
  1584. printwarn();
  1585. if (tmp[0] == '\0')
  1586. break;
  1587. else
  1588. /* Add to readline(3) history */
  1589. add_history(tmp);
  1590. input = tmp;
  1591. tmp = strstrip(tmp);
  1592. if (tmp[0] == '\0') {
  1593. free(input);
  1594. break;
  1595. }
  1596. truecd = 0;
  1597. if (tmp[0] == '~') {
  1598. /* Expand ~ to HOME absolute path */
  1599. char *home = getenv("HOME");
  1600. if (home)
  1601. snprintf(newpath, PATH_MAX, "%s%s", home, tmp + 1);
  1602. else {
  1603. free(input);
  1604. printmsg("HOME not set");
  1605. goto nochange;
  1606. }
  1607. } else if (tmp[0] == '-' && tmp[1] == '\0') {
  1608. if (lastdir[0] == '\0') {
  1609. free(input);
  1610. break;
  1611. }
  1612. /* Switch to last visited dir */
  1613. xstrlcpy(newpath, lastdir, PATH_MAX);
  1614. truecd = 1;
  1615. } else if ((r = all_dots(tmp))) {
  1616. if (r == 1) {
  1617. /* Always in the current dir */
  1618. free(input);
  1619. break;
  1620. }
  1621. r--;
  1622. dir = path;
  1623. for (fd = 0; fd < r; fd++) {
  1624. /* Reached / ? */
  1625. if (strcmp(path, "/") == 0 ||
  1626. strchr(path, '/') == NULL) {
  1627. /* If it's a cd .. at / */
  1628. if (fd == 0) {
  1629. printmsg("You are at /");
  1630. free(input);
  1631. goto nochange;
  1632. }
  1633. /* Can't cd beyond / anyway */
  1634. break;
  1635. } else {
  1636. dir = xdirname(dir);
  1637. if (canopendir(dir) == 0) {
  1638. printwarn();
  1639. free(input);
  1640. goto nochange;
  1641. }
  1642. }
  1643. }
  1644. truecd = 1;
  1645. /* Save the path in case of cd ..
  1646. We mark the current dir in parent dir */
  1647. if (r == 1) {
  1648. xstrlcpy(oldpath, path, PATH_MAX);
  1649. truecd = 2;
  1650. }
  1651. xstrlcpy(newpath, dir, PATH_MAX);
  1652. } else
  1653. mkpath(path, tmp, newpath, PATH_MAX);
  1654. if (canopendir(newpath) == 0) {
  1655. printwarn();
  1656. free(input);
  1657. break;
  1658. }
  1659. if (truecd == 0) {
  1660. /* Probable change in dir */
  1661. /* No-op if it's the same directory */
  1662. if (strcmp(path, newpath) == 0) {
  1663. free(input);
  1664. break;
  1665. }
  1666. oldpath[0] = '\0';
  1667. } else if (truecd == 1)
  1668. /* Sure change in dir */
  1669. oldpath[0] = '\0';
  1670. /* Save last working directory */
  1671. xstrlcpy(lastdir, path, PATH_MAX);
  1672. /* Save the newly opted dir in path */
  1673. xstrlcpy(path, newpath, PATH_MAX);
  1674. /* Reset filter */
  1675. xstrlcpy(fltr, ifilter, LINE_MAX);
  1676. DPRINTF_S(path);
  1677. free(input);
  1678. if (filtermode)
  1679. presel = FILTER;
  1680. goto begin;
  1681. }
  1682. case SEL_CDHOME:
  1683. tmp = getenv("HOME");
  1684. if (tmp == NULL) {
  1685. clearprompt();
  1686. goto nochange;
  1687. }
  1688. if (canopendir(tmp) == 0) {
  1689. printwarn();
  1690. goto nochange;
  1691. }
  1692. if (strcmp(path, tmp) == 0)
  1693. break;
  1694. /* Save last working directory */
  1695. xstrlcpy(lastdir, path, PATH_MAX);
  1696. xstrlcpy(path, tmp, PATH_MAX);
  1697. oldpath[0] = '\0';
  1698. /* Reset filter */
  1699. xstrlcpy(fltr, ifilter, LINE_MAX);
  1700. DPRINTF_S(path);
  1701. if (filtermode)
  1702. presel = FILTER;
  1703. goto begin;
  1704. case SEL_CDBEGIN:
  1705. if (canopendir(ipath) == 0) {
  1706. printwarn();
  1707. goto nochange;
  1708. }
  1709. if (strcmp(path, ipath) == 0)
  1710. break;
  1711. /* Save last working directory */
  1712. xstrlcpy(lastdir, path, PATH_MAX);
  1713. xstrlcpy(path, ipath, PATH_MAX);
  1714. oldpath[0] = '\0';
  1715. /* Reset filter */
  1716. xstrlcpy(fltr, ifilter, LINE_MAX);
  1717. DPRINTF_S(path);
  1718. if (filtermode)
  1719. presel = FILTER;
  1720. goto begin;
  1721. case SEL_CDLAST:
  1722. if (lastdir[0] == '\0')
  1723. break;
  1724. if (canopendir(lastdir) == 0) {
  1725. printwarn();
  1726. goto nochange;
  1727. }
  1728. xstrlcpy(newpath, lastdir, PATH_MAX);
  1729. xstrlcpy(lastdir, path, PATH_MAX);
  1730. xstrlcpy(path, newpath, PATH_MAX);
  1731. oldpath[0] = '\0';
  1732. /* Reset filter */
  1733. xstrlcpy(fltr, ifilter, LINE_MAX);
  1734. DPRINTF_S(path);
  1735. if (filtermode)
  1736. presel = FILTER;
  1737. goto begin;
  1738. case SEL_CDBM:
  1739. printprompt("key: ");
  1740. tmp = readinput();
  1741. if (tmp == NULL) {
  1742. clearprompt();
  1743. goto nochange;
  1744. }
  1745. clearprompt();
  1746. for (r = 0; bookmark[r].key && r < MAX_BM; r++) {
  1747. if (strcmp(bookmark[r].key, tmp) == 0) {
  1748. if (bookmark[r].loc[0] == '~') {
  1749. /* Expand ~ to HOME absolute path */
  1750. char *home = getenv("HOME");
  1751. if (home)
  1752. snprintf(newpath, PATH_MAX, "%s%s", home, bookmark[r].loc + 1);
  1753. else {
  1754. printmsg("HOME not set");
  1755. goto nochange;
  1756. }
  1757. } else
  1758. mkpath(path, bookmark[r].loc, newpath, PATH_MAX);
  1759. if (canopendir(newpath) == 0) {
  1760. printwarn();
  1761. goto nochange;
  1762. }
  1763. if (strcmp(path, newpath) == 0)
  1764. break;
  1765. oldpath[0] = '\0';
  1766. break;
  1767. }
  1768. }
  1769. if (!bookmark[r].key) {
  1770. printmsg("No matching bookmark");
  1771. goto nochange;
  1772. }
  1773. /* Save last working directory */
  1774. xstrlcpy(lastdir, path, PATH_MAX);
  1775. /* Save the newly opted dir in path */
  1776. xstrlcpy(path, newpath, PATH_MAX);
  1777. /* Reset filter */
  1778. xstrlcpy(fltr, ifilter, LINE_MAX);
  1779. DPRINTF_S(path);
  1780. if (filtermode)
  1781. presel = FILTER;
  1782. goto begin;
  1783. case SEL_TOGGLEDOT:
  1784. showhidden ^= 1;
  1785. initfilter(showhidden, &ifilter);
  1786. xstrlcpy(fltr, ifilter, LINE_MAX);
  1787. goto begin;
  1788. case SEL_DETAIL:
  1789. showdetail = !showdetail;
  1790. showdetail ? (printptr = &printent_long)
  1791. : (printptr = &printent);
  1792. /* Save current */
  1793. if (ndents > 0)
  1794. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1795. goto begin;
  1796. case SEL_STATS:
  1797. {
  1798. struct stat sb;
  1799. if (ndents > 0) {
  1800. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1801. r = lstat(oldpath, &sb);
  1802. if (r == -1) {
  1803. if (dents)
  1804. dentfree(dents);
  1805. printerr(1, "lstat");
  1806. } else {
  1807. exitcurses();
  1808. r = show_stats(oldpath, dents[cur].name, &sb);
  1809. initcurses();
  1810. if (r < 0) {
  1811. printmsg(strerror(errno));
  1812. goto nochange;
  1813. }
  1814. }
  1815. }
  1816. break;
  1817. }
  1818. case SEL_MEDIA:
  1819. if (ndents > 0) {
  1820. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1821. if(show_mediainfo(oldpath, NULL) == -1) {
  1822. printmsg("mediainfo missing");
  1823. goto nochange;
  1824. }
  1825. }
  1826. break;
  1827. case SEL_FMEDIA:
  1828. if (ndents > 0) {
  1829. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1830. if(show_mediainfo(oldpath, "-f") == -1) {
  1831. printmsg("mediainfo missing");
  1832. goto nochange;
  1833. }
  1834. }
  1835. break;
  1836. case SEL_DFB:
  1837. if (!desktop_manager) {
  1838. printmsg("NNN_DE_FILE_MANAGER not set");
  1839. goto nochange;
  1840. }
  1841. spawn(desktop_manager, path, NULL, path, 0b110);
  1842. break;
  1843. case SEL_FSIZE:
  1844. sizeorder = !sizeorder;
  1845. mtimeorder = 0;
  1846. bsizeorder = 0;
  1847. /* Save current */
  1848. if (ndents > 0)
  1849. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1850. goto begin;
  1851. case SEL_BSIZE:
  1852. bsizeorder = !bsizeorder;
  1853. if (bsizeorder) {
  1854. showdetail = 1;
  1855. printptr = &printent_long;
  1856. }
  1857. mtimeorder = 0;
  1858. sizeorder = 0;
  1859. /* Save current */
  1860. if (ndents > 0)
  1861. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1862. goto begin;
  1863. case SEL_MTIME:
  1864. mtimeorder = !mtimeorder;
  1865. sizeorder = 0;
  1866. bsizeorder = 0;
  1867. /* Save current */
  1868. if (ndents > 0)
  1869. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1870. goto begin;
  1871. case SEL_REDRAW:
  1872. /* Save current */
  1873. if (ndents > 0)
  1874. mkpath(path, dents[cur].name, oldpath, PATH_MAX);
  1875. goto begin;
  1876. case SEL_COPY:
  1877. if (copier && ndents) {
  1878. if (strcmp(path, "/") == 0)
  1879. snprintf(newpath, PATH_MAX, "/%s",
  1880. dents[cur].name);
  1881. else
  1882. snprintf(newpath, PATH_MAX, "%s/%s",
  1883. path, dents[cur].name);
  1884. spawn(copier, newpath, NULL, NULL, 0);
  1885. printmsg(newpath);
  1886. } else if (!copier)
  1887. printmsg("NNN_COPIER is not set");
  1888. goto nochange;
  1889. case SEL_HELP:
  1890. exitcurses();
  1891. show_help();
  1892. initcurses();
  1893. break;
  1894. case SEL_RUN:
  1895. run = xgetenv(env, run);
  1896. exitcurses();
  1897. spawn(run, NULL, NULL, path, 1);
  1898. initcurses();
  1899. /* Repopulate as directory content may have changed */
  1900. goto begin;
  1901. case SEL_RUNARG:
  1902. run = xgetenv(env, run);
  1903. exitcurses();
  1904. spawn(run, dents[cur].name, NULL, path, 0);
  1905. initcurses();
  1906. break;
  1907. }
  1908. /* Screensaver */
  1909. if (idletimeout != 0 && idle == idletimeout) {
  1910. idle = 0;
  1911. exitcurses();
  1912. spawn(player, "", "screensaver", NULL, 8);
  1913. initcurses();
  1914. }
  1915. }
  1916. }
  1917. static void
  1918. usage(void)
  1919. {
  1920. fprintf(stdout, "usage: nnn [-l] [-i] [-p custom_nlay] [-S] [-v] [-h] [PATH]\n\n\
  1921. The missing terminal file browser for X.\n\n\
  1922. positional arguments:\n\
  1923. PATH directory to open [default: current dir]\n\n\
  1924. optional arguments:\n\
  1925. -l start in light mode (fewer details)\n\
  1926. -i start in navigate-as-you-type mode\n\
  1927. -p path to custom nlay\n\
  1928. -S start in disk usage analyzer mode\n\
  1929. -v show program version and exit\n\
  1930. -h show this help and exit\n\n\
  1931. Version: %s\n\
  1932. License: BSD 2-Clause\n\
  1933. Webpage: https://github.com/jarun/nnn\n", VERSION);
  1934. exit(0);
  1935. }
  1936. int
  1937. main(int argc, char *argv[])
  1938. {
  1939. char cwd[PATH_MAX], *ipath;
  1940. char *ifilter;
  1941. int opt = 0;
  1942. /* Confirm we are in a terminal */
  1943. if (!isatty(0) || !isatty(1)) {
  1944. fprintf(stderr, "stdin or stdout is not a tty\n");
  1945. exit(1);
  1946. }
  1947. while ((opt = getopt(argc, argv, "dlSip:vh")) != -1) {
  1948. switch (opt) {
  1949. case 'S':
  1950. bsizeorder = 1;
  1951. break;
  1952. case 'l':
  1953. showdetail = 0;
  1954. printptr = &printent;
  1955. break;
  1956. case 'i':
  1957. filtermode = 1;
  1958. break;
  1959. case 'p':
  1960. player = optarg;
  1961. break;
  1962. case 'v':
  1963. fprintf(stdout, "%s\n", VERSION);
  1964. return 0;
  1965. case 'd':
  1966. fprintf(stderr, "Option -d is deprecated and will be removed, detail view mode is default now.\n");
  1967. break;
  1968. case 'h': // fallthrough
  1969. default:
  1970. usage();
  1971. }
  1972. }
  1973. if (argc == optind) {
  1974. /* Start in the current directory */
  1975. ipath = getcwd(cwd, PATH_MAX);
  1976. if (ipath == NULL)
  1977. ipath = "/";
  1978. } else {
  1979. ipath = realpath(argv[optind], cwd);
  1980. if (!ipath) {
  1981. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1982. exit(1);
  1983. }
  1984. }
  1985. open_max = max_openfds();
  1986. if (getuid() == 0)
  1987. showhidden = 1;
  1988. initfilter(showhidden, &ifilter);
  1989. /* Edit text in EDITOR, if opted */
  1990. if (getenv("NNN_USE_EDITOR"))
  1991. editor = xgetenv("EDITOR", "vi");
  1992. /* Set player if not set already */
  1993. if (!player)
  1994. player = utils[1];
  1995. /* Get the desktop file browser, if set */
  1996. desktop_manager = getenv("NNN_DE_FILE_MANAGER");
  1997. /* Get screensaver wait time, if set; copier used as tmp var */
  1998. copier = getenv("NNN_IDLE_TIMEOUT");
  1999. if (copier)
  2000. idletimeout = abs(atoi(copier));
  2001. /* Get the default copier, if set */
  2002. copier = getenv("NNN_COPIER");
  2003. /* Parse bookmarks string, if available */
  2004. char *bms = getenv("NNN_BMS");
  2005. if (bms)
  2006. parsebmstr(bms);
  2007. signal(SIGINT, SIG_IGN);
  2008. /* Test initial path */
  2009. if (canopendir(ipath) == 0) {
  2010. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  2011. exit(1);
  2012. }
  2013. /* Set locale */
  2014. setlocale(LC_ALL, "");
  2015. initcurses();
  2016. browse(ipath, ifilter);
  2017. exitcurses();
  2018. exit(0);
  2019. }