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.
 
 
 
 
 
 

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