My build of nnn with minor changes
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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