My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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