My build of nnn with minor changes
 
 
 
 
 
 

1577 rindas
33 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <curses.h>
  6. #include <dirent.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <regex.h>
  12. #include <signal.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <time.h>
  19. #include <pwd.h>
  20. #include <grp.h>
  21. #define __USE_XOPEN_EXTENDED
  22. #include <ftw.h>
  23. #ifdef DEBUG
  24. static int
  25. xprintf(int fd, const char *fmt, ...)
  26. {
  27. char buf[BUFSIZ];
  28. int r;
  29. va_list ap;
  30. va_start(ap, fmt);
  31. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  32. if (r > 0)
  33. r = write(fd, buf, r);
  34. va_end(ap);
  35. return r;
  36. }
  37. #define DEBUG_FD 8
  38. #define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
  39. #define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x)
  40. #define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x)
  41. #define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=0x%p\n", x)
  42. #else
  43. #define DPRINTF_D(x)
  44. #define DPRINTF_U(x)
  45. #define DPRINTF_S(x)
  46. #define DPRINTF_P(x)
  47. #endif /* DEBUG */
  48. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  49. #undef MIN
  50. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  51. #define ISODD(x) ((x) & 1)
  52. #define CONTROL(c) ((c) ^ 0x40)
  53. #define TOUPPER(ch) \
  54. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  55. #define MAX_CMD_LEN (PATH_MAX << 1)
  56. #define CURSYM(flag) (flag ? CURSR : EMPTY)
  57. struct assoc {
  58. char *regex; /* Regex to match on filename */
  59. char *bin; /* Program */
  60. };
  61. /* Supported actions */
  62. enum action {
  63. SEL_QUIT = 1,
  64. SEL_BACK,
  65. SEL_GOIN,
  66. SEL_FLTR,
  67. SEL_NEXT,
  68. SEL_PREV,
  69. SEL_PGDN,
  70. SEL_PGUP,
  71. SEL_HOME,
  72. SEL_END,
  73. SEL_CD,
  74. SEL_CDHOME,
  75. SEL_LAST,
  76. SEL_TOGGLEDOT,
  77. SEL_DETAIL,
  78. SEL_STATS,
  79. SEL_FSIZE,
  80. SEL_BSIZE,
  81. SEL_MTIME,
  82. SEL_REDRAW,
  83. SEL_COPY,
  84. SEL_HELP,
  85. SEL_RUN,
  86. SEL_RUNARG,
  87. };
  88. struct key {
  89. int sym; /* Key pressed */
  90. enum action act; /* Action */
  91. char *run; /* Program to run */
  92. char *env; /* Environment variable to run */
  93. };
  94. #include "config.h"
  95. typedef struct entry {
  96. char name[PATH_MAX];
  97. mode_t mode;
  98. time_t t;
  99. off_t size;
  100. off_t bsize;
  101. } *pEntry;
  102. typedef unsigned long ulong;
  103. /* Global context */
  104. static struct entry *dents;
  105. static int ndents, cur;
  106. static int idle;
  107. static char *opener;
  108. static char *fallback_opener;
  109. static char *copier;
  110. static const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  111. /*
  112. * Layout:
  113. * .---------
  114. * | cwd: /mnt/path
  115. * |
  116. * | file0
  117. * | file1
  118. * | > file2
  119. * | file3
  120. * | file4
  121. * ...
  122. * | filen
  123. * |
  124. * | Permission denied
  125. * '------
  126. */
  127. static void printmsg(char *);
  128. static void printwarn(void);
  129. static void printerr(int, char *);
  130. static void *
  131. xrealloc(void *p, size_t size)
  132. {
  133. p = realloc(p, size);
  134. if (p == NULL)
  135. printerr(1, "realloc");
  136. return p;
  137. }
  138. static size_t
  139. xstrlcpy(char *dest, const char *src, size_t n)
  140. {
  141. size_t i;
  142. for (i = 0; i < n && *src; i++)
  143. *dest++ = *src++;
  144. if (n) {
  145. *dest = '\0';
  146. #ifdef CHECK_XSTRLCPY_RET
  147. /* Compiling this out as we are not checking
  148. the return value anywhere (controlled case).
  149. Just returning the number of bytes copied. */
  150. while(*src++)
  151. i++;
  152. #endif
  153. }
  154. return i;
  155. }
  156. /*
  157. * The poor man's implementation of memrchr(3).
  158. * We are only looking for '/' in this program.
  159. */
  160. static void *
  161. xmemrchr(const void *s, int c, size_t n)
  162. {
  163. unsigned char *p;
  164. unsigned char ch = (unsigned char)c;
  165. if (!s || !n)
  166. return NULL;
  167. p = (unsigned char *)s + n - 1;
  168. while(n--)
  169. if ((*p--) == ch)
  170. return ++p;
  171. return NULL;
  172. }
  173. #if 0
  174. /* Some implementations of dirname(3) may modify `path' and some
  175. * return a pointer inside `path'. */
  176. static char *
  177. xdirname(const char *path)
  178. {
  179. static char out[PATH_MAX];
  180. char tmp[PATH_MAX], *p;
  181. xstrlcpy(tmp, path, sizeof(tmp));
  182. p = dirname(tmp);
  183. if (p == NULL)
  184. printerr(1, "dirname");
  185. xstrlcpy(out, p, sizeof(out));
  186. return out;
  187. }
  188. #endif
  189. /*
  190. * The following dirname(3) implementation does not
  191. * change the input. We use a copy of the original.
  192. *
  193. * Modified from the glibc (GNU LGPL) version.
  194. */
  195. static char *
  196. xdirname(const char *path)
  197. {
  198. static char name[PATH_MAX];
  199. char *last_slash;
  200. xstrlcpy(name, path, PATH_MAX);
  201. /* Find last '/'. */
  202. last_slash = strrchr(name, '/');
  203. if (last_slash != NULL && last_slash != name && last_slash[1] == '\0') {
  204. /* Determine whether all remaining characters are slashes. */
  205. char *runp;
  206. for (runp = last_slash; runp != name; --runp)
  207. if (runp[-1] != '/')
  208. break;
  209. /* The '/' is the last character, we have to look further. */
  210. if (runp != name)
  211. last_slash = xmemrchr(name, '/', runp - name);
  212. }
  213. if (last_slash != NULL) {
  214. /* Determine whether all remaining characters are slashes. */
  215. char *runp;
  216. for (runp = last_slash; runp != name; --runp)
  217. if (runp[-1] != '/')
  218. break;
  219. /* Terminate the name. */
  220. if (runp == name) {
  221. /* The last slash is the first character in the string.
  222. We have to return "/". As a special case we have to
  223. return "//" if there are exactly two slashes at the
  224. beginning of the string. See XBD 4.10 Path Name
  225. Resolution for more information. */
  226. if (last_slash == name + 1)
  227. ++last_slash;
  228. else
  229. last_slash = name + 1;
  230. } else
  231. last_slash = runp;
  232. last_slash[0] = '\0';
  233. } else {
  234. /* This assignment is ill-designed but the XPG specs require to
  235. return a string containing "." in any case no directory part
  236. is found and so a static and constant string is required. */
  237. name[0] = '.';
  238. name[1] = '\0';
  239. }
  240. return name;
  241. }
  242. static void
  243. spawn(char *file, char *arg, char *dir, int notify)
  244. {
  245. pid_t pid;
  246. int status;
  247. pid = fork();
  248. if (pid == 0) {
  249. if (dir != NULL)
  250. status = chdir(dir);
  251. if (notify)
  252. fprintf(stdout, "\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
  253. execlp(file, file, arg, NULL);
  254. _exit(1);
  255. } else {
  256. /* Ignore interruptions */
  257. while (waitpid(pid, &status, 0) == -1)
  258. DPRINTF_D(status);
  259. DPRINTF_D(pid);
  260. }
  261. }
  262. static char *
  263. xgetenv(char *name, char *fallback)
  264. {
  265. char *value;
  266. if (name == NULL)
  267. return fallback;
  268. value = getenv(name);
  269. return value && value[0] ? value : fallback;
  270. }
  271. int xisdigit(const char c) {
  272. if (c >= '0' && c <= '9') \
  273. return 1; \
  274. return 0;
  275. }
  276. /*
  277. * We assume none of the strings are NULL.
  278. *
  279. * Let's have the logic to sort numeric names in numeric order.
  280. * E.g., the order '1, 10, 2' doesn't make sense to human eyes.
  281. *
  282. * If the absolute numeric values are same, we fallback to alphasort.
  283. */
  284. static int
  285. xstricmp(const char *s1, const char *s2)
  286. {
  287. static char *c1, *c2;
  288. static long long num1, num2;
  289. num1 = strtoll(s1, &c1, 10);
  290. num2 = strtoll(s2, &c2, 10);
  291. if (*c1 == '\0' && *c2 == '\0') {
  292. if (num1 != num2) {
  293. if (num1 > num2)
  294. return 1;
  295. else
  296. return -1;
  297. }
  298. } else if (*c1 == '\0' && *c2 != '\0')
  299. return -1;
  300. else if (*c1 != '\0' && *c2 == '\0')
  301. return 1;
  302. while (*s2 && *s1 && TOUPPER(*s1) == TOUPPER(*s2))
  303. s1++, s2++;
  304. /* In case of alphabetically same names, make sure
  305. lower case one comes before upper case one */
  306. if (!*s1 && !*s2)
  307. return 1;
  308. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  309. }
  310. static char *
  311. openwith(char *file)
  312. {
  313. regex_t regex;
  314. char *bin = NULL;
  315. unsigned int i;
  316. for (i = 0; i < LEN(assocs); i++) {
  317. if (regcomp(&regex, assocs[i].regex,
  318. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  319. continue;
  320. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  321. bin = assocs[i].bin;
  322. break;
  323. }
  324. }
  325. DPRINTF_S(bin);
  326. return bin;
  327. }
  328. static int
  329. setfilter(regex_t *regex, char *filter)
  330. {
  331. char errbuf[LINE_MAX];
  332. size_t len;
  333. int r;
  334. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  335. if (r != 0) {
  336. len = COLS;
  337. if (len > sizeof(errbuf))
  338. len = sizeof(errbuf);
  339. regerror(r, regex, errbuf, len);
  340. printmsg(errbuf);
  341. }
  342. return r;
  343. }
  344. static void
  345. initfilter(int dot, char **ifilter)
  346. {
  347. *ifilter = dot ? "." : "^[^.]";
  348. }
  349. static int
  350. visible(regex_t *regex, char *file)
  351. {
  352. return regexec(regex, file, 0, NULL, 0) == 0;
  353. }
  354. static int
  355. entrycmp(const void *va, const void *vb)
  356. {
  357. static pEntry pa, pb;
  358. pa = (pEntry)va;
  359. pb = (pEntry)vb;
  360. /* Sort directories first */
  361. if (S_ISDIR(pb->mode) && !S_ISDIR(pa->mode))
  362. return 1;
  363. else if (S_ISDIR(pa->mode) && !S_ISDIR(pb->mode))
  364. return -1;
  365. /* Do the actual sorting */
  366. if (mtimeorder)
  367. return pb->t - pa->t;
  368. if (sizeorder) {
  369. if (pb->size > pa->size)
  370. return 1;
  371. else if (pb->size < pa->size)
  372. return -1;
  373. }
  374. if (bsizeorder) {
  375. if (pb->bsize > pa->bsize)
  376. return 1;
  377. else if (pb->bsize < pa->bsize)
  378. return -1;
  379. }
  380. return xstricmp(pa->name, pb->name);
  381. }
  382. static void
  383. initcurses(void)
  384. {
  385. if (initscr() == NULL) {
  386. char *term = getenv("TERM");
  387. if (term != NULL)
  388. fprintf(stderr, "error opening terminal: %s\n", term);
  389. else
  390. fprintf(stderr, "failed to initialize curses\n");
  391. exit(1);
  392. }
  393. cbreak();
  394. noecho();
  395. nonl();
  396. intrflush(stdscr, FALSE);
  397. keypad(stdscr, TRUE);
  398. curs_set(FALSE); /* Hide cursor */
  399. timeout(1000); /* One second */
  400. }
  401. static void
  402. exitcurses(void)
  403. {
  404. endwin(); /* Restore terminal */
  405. }
  406. /* Messages show up at the bottom */
  407. static void
  408. printmsg(char *msg)
  409. {
  410. move(LINES - 1, 0);
  411. printw("%s\n", msg);
  412. }
  413. /* Display warning as a message */
  414. static void
  415. printwarn(void)
  416. {
  417. printmsg(strerror(errno));
  418. }
  419. /* Kill curses and display error before exiting */
  420. static void
  421. printerr(int ret, char *prefix)
  422. {
  423. exitcurses();
  424. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  425. exit(ret);
  426. }
  427. /* Clear the last line */
  428. static void
  429. clearprompt(void)
  430. {
  431. printmsg("");
  432. }
  433. /* Print prompt on the last line */
  434. static void
  435. printprompt(char *str)
  436. {
  437. clearprompt();
  438. printw(str);
  439. }
  440. /* Returns SEL_* if key is bound and 0 otherwise.
  441. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  442. static int
  443. nextsel(char **run, char **env)
  444. {
  445. int c;
  446. unsigned int i;
  447. c = getch();
  448. if (c == -1)
  449. idle++;
  450. else
  451. idle = 0;
  452. for (i = 0; i < LEN(bindings); i++)
  453. if (c == bindings[i].sym) {
  454. *run = bindings[i].run;
  455. *env = bindings[i].env;
  456. return bindings[i].act;
  457. }
  458. return 0;
  459. }
  460. static char *
  461. readln(void)
  462. {
  463. static char ln[LINE_MAX];
  464. timeout(-1);
  465. echo();
  466. curs_set(TRUE);
  467. memset(ln, 0, sizeof(ln));
  468. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  469. noecho();
  470. curs_set(FALSE);
  471. timeout(1000);
  472. return ln[0] ? ln : NULL;
  473. }
  474. static int
  475. canopendir(char *path)
  476. {
  477. DIR *dirp;
  478. dirp = opendir(path);
  479. if (dirp == NULL)
  480. return 0;
  481. closedir(dirp);
  482. return 1;
  483. }
  484. /*
  485. * Returns "dir/name or "/name"
  486. */
  487. static char *
  488. mkpath(char *dir, char *name, char *out, size_t n)
  489. {
  490. /* Handle absolute path */
  491. if (name[0] == '/')
  492. xstrlcpy(out, name, n);
  493. else {
  494. /* Handle root case */
  495. if (strcmp(dir, "/") == 0)
  496. snprintf(out, n, "/%s", name);
  497. else
  498. snprintf(out, n, "%s/%s", dir, name);
  499. }
  500. return out;
  501. }
  502. static void
  503. printent(struct entry *ent, int active)
  504. {
  505. if (S_ISDIR(ent->mode))
  506. printw("%s%s/\n", CURSYM(active), ent->name);
  507. else if (S_ISLNK(ent->mode))
  508. printw("%s%s@\n", CURSYM(active), ent->name);
  509. else if (S_ISSOCK(ent->mode))
  510. printw("%s%s=\n", CURSYM(active), ent->name);
  511. else if (S_ISFIFO(ent->mode))
  512. printw("%s%s|\n", CURSYM(active), ent->name);
  513. else if (ent->mode & S_IXUSR)
  514. printw("%s%s*\n", CURSYM(active), ent->name);
  515. else
  516. printw("%s%s\n", CURSYM(active), ent->name);
  517. }
  518. static void (*printptr)(struct entry *ent, int active) = &printent;
  519. static const double div_2_pow_10 = 1.0 / 1024.0;
  520. static char*
  521. coolsize(off_t size)
  522. {
  523. static char size_buf[12]; /* Buffer to hold human readable size */
  524. int i = 0;
  525. off_t fsize = size, tmp;
  526. long double rem = 0;
  527. while (fsize > 1024) {
  528. tmp = fsize;
  529. //fsize *= div_2_pow_10;
  530. fsize >>= 10;
  531. rem = tmp - (fsize << 10);
  532. i++;
  533. }
  534. snprintf(size_buf, 12, "%.*Lf%s", i, fsize + rem * div_2_pow_10, size_units[i]);
  535. return size_buf;
  536. }
  537. static void
  538. printent_long(struct entry *ent, int active)
  539. {
  540. static char buf[18];
  541. strftime(buf, 18, "%b %d %H:%M %Y", localtime(&ent->t));
  542. if (active)
  543. attron(A_REVERSE);
  544. if (!bsizeorder) {
  545. if (S_ISDIR(ent->mode))
  546. printw("%s%-17.17s / %s/\n",
  547. CURSYM(active), buf, ent->name);
  548. else if (S_ISLNK(ent->mode))
  549. printw("%s%-17.17s @ %s@\n",
  550. CURSYM(active), buf, ent->name);
  551. else if (S_ISSOCK(ent->mode))
  552. printw("%s%-17.17s = %s=\n",
  553. CURSYM(active), buf, ent->name);
  554. else if (S_ISFIFO(ent->mode))
  555. printw("%s%-17.17s | %s|\n",
  556. CURSYM(active), buf, ent->name);
  557. else if (S_ISBLK(ent->mode))
  558. printw("%s%-17.17s b %s\n",
  559. CURSYM(active), buf, ent->name);
  560. else if (S_ISCHR(ent->mode))
  561. printw("%s%-17.17s c %s\n",
  562. CURSYM(active), buf, ent->name);
  563. else if (ent->mode & S_IXUSR)
  564. printw("%s%-17.17s %8.8s* %s*\n", CURSYM(active),
  565. buf, coolsize(ent->size), ent->name);
  566. else
  567. printw("%s%-17.17s %8.8s %s\n", CURSYM(active),
  568. buf, coolsize(ent->size), ent->name);
  569. } else {
  570. if (S_ISDIR(ent->mode))
  571. printw("%s%-17.17s %8.8s/ %s/\n", CURSYM(active),
  572. buf, coolsize(ent->bsize << 9), ent->name);
  573. else if (S_ISLNK(ent->mode))
  574. printw("%s%-17.17s @ %s@\n",
  575. CURSYM(active), buf, ent->name);
  576. else if (S_ISSOCK(ent->mode))
  577. printw("%s%-17.17s = %s=\n",
  578. CURSYM(active), buf, ent->name);
  579. else if (S_ISFIFO(ent->mode))
  580. printw("%s%-17.17s | %s|\n",
  581. CURSYM(active), buf, ent->name);
  582. else if (S_ISBLK(ent->mode))
  583. printw("%s%-17.17s b %s\n",
  584. CURSYM(active), buf, ent->name);
  585. else if (S_ISCHR(ent->mode))
  586. printw("%s%-17.17s c %s\n",
  587. CURSYM(active), buf, ent->name);
  588. else if (ent->mode & S_IXUSR)
  589. printw("%s%-17.17s %8.8s* %s*\n", CURSYM(active),
  590. buf, coolsize(ent->bsize << 9), ent->name);
  591. else
  592. printw("%s%-17.17s %8.8s %s\n", CURSYM(active),
  593. buf, coolsize(ent->bsize << 9), ent->name);
  594. }
  595. if (active)
  596. attroff(A_REVERSE);
  597. }
  598. static char
  599. get_fileind(mode_t mode, char *desc)
  600. {
  601. char c;
  602. if (S_ISREG(mode)) {
  603. c = '-';
  604. sprintf(desc, "%s", "regular file");
  605. if (mode & S_IXUSR)
  606. strcat(desc, ", executable");
  607. } else if (S_ISDIR(mode)) {
  608. c = 'd';
  609. sprintf(desc, "%s", "directory");
  610. } else if (S_ISBLK(mode)) {
  611. c = 'b';
  612. sprintf(desc, "%s", "block special device");
  613. } else if (S_ISCHR(mode)) {
  614. c = 'c';
  615. sprintf(desc, "%s", "character special device");
  616. #ifdef S_ISFIFO
  617. } else if (S_ISFIFO(mode)) {
  618. c = 'p';
  619. sprintf(desc, "%s", "FIFO");
  620. #endif /* S_ISFIFO */
  621. #ifdef S_ISLNK
  622. } else if (S_ISLNK(mode)) {
  623. c = 'l';
  624. sprintf(desc, "%s", "symbolic link");
  625. #endif /* S_ISLNK */
  626. #ifdef S_ISSOCK
  627. } else if (S_ISSOCK(mode)) {
  628. c = 's';
  629. sprintf(desc, "%s", "socket");
  630. #endif /* S_ISSOCK */
  631. #ifdef S_ISDOOR
  632. /* Solaris 2.6, etc. */
  633. } else if (S_ISDOOR(mode)) {
  634. c = 'D';
  635. desc[0] = '\0';
  636. #endif /* S_ISDOOR */
  637. } else {
  638. /* Unknown type -- possibly a regular file? */
  639. c = '?';
  640. desc[0] = '\0';
  641. }
  642. return(c);
  643. }
  644. /* Convert a mode field into "ls -l" type perms field. */
  645. static char *
  646. get_lsperms(mode_t mode, char *desc)
  647. {
  648. static const char *rwx[] = {"---", "--x", "-w-", "-wx",
  649. "r--", "r-x", "rw-", "rwx"};
  650. static char bits[11];
  651. bits[0] = get_fileind(mode, desc);
  652. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  653. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  654. strcpy(&bits[7], rwx[(mode & 7)]);
  655. if (mode & S_ISUID)
  656. bits[3] = (mode & S_IXUSR) ? 's' : 'S';
  657. if (mode & S_ISGID)
  658. bits[6] = (mode & S_IXGRP) ? 's' : 'l';
  659. if (mode & S_ISVTX)
  660. bits[9] = (mode & S_IXOTH) ? 't' : 'T';
  661. bits[10] = '\0';
  662. return(bits);
  663. }
  664. char *
  665. get_output(char *buf, size_t bytes)
  666. {
  667. char *ret;
  668. FILE *pf = popen(buf, "r");
  669. if (pf) {
  670. ret = fgets(buf, bytes, pf);
  671. pclose(pf);
  672. return ret;
  673. }
  674. return NULL;
  675. }
  676. /*
  677. * Follows the stat(1) output closely
  678. */
  679. void
  680. show_stats(char* fpath, char* fname, struct stat *sb)
  681. {
  682. char buf[PATH_MAX + 48];
  683. char *perms = get_lsperms(sb->st_mode, buf);
  684. char *p, *begin = buf;
  685. clear();
  686. /* Show file name or 'symlink' -> 'target' */
  687. if (perms[0] == 'l') {
  688. char symtgt[PATH_MAX];
  689. ssize_t len = readlink(fpath, symtgt, PATH_MAX);
  690. if (len != -1) {
  691. symtgt[len] = '\0';
  692. printw("\n\n File: '%s' -> '%s'", fname, symtgt);
  693. }
  694. } else
  695. printw("\n File: '%s'", fname);
  696. /* Show size, blocks, file type */
  697. printw("\n Size: %-15llu Blocks: %-10llu IO Block: %-6llu %s",
  698. sb->st_size, sb->st_blocks, sb->st_blksize, buf);
  699. /* Show containing device, inode, hardlink count */
  700. sprintf(buf, "%lxh/%lud", (ulong)sb->st_dev, (ulong)sb->st_dev);
  701. printw("\n Device: %-15s Inode: %-11lu Links: %-9lu",
  702. buf, sb->st_ino, sb->st_nlink);
  703. /* Show major, minor number for block or char device */
  704. if (perms[0] == 'b' || perms[0] == 'c')
  705. printw(" Device type: %lx,%lx",
  706. major(sb->st_rdev), minor(sb->st_rdev));
  707. /* Show permissions, owner, group */
  708. printw("\n Access: 0%d%d%d/%s Uid: (%lu/%s) Gid: (%lu/%s)",
  709. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  710. perms,
  711. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  712. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  713. /* Show last access time */
  714. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  715. printw("\n\n Access: %s", buf);
  716. /* Show last modification time */
  717. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  718. printw("\n Modify: %s", buf);
  719. /* Show last status change time */
  720. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  721. printw("\n Change: %s", buf);
  722. if (S_ISREG(sb->st_mode)) {
  723. /* Show file(1) output */
  724. sprintf(buf, "file -b \"%s\" 2>&1", fpath);
  725. p = get_output(buf, PATH_MAX + 48);
  726. if (p) {
  727. printw("\n\n ");
  728. while (*p) {
  729. if (*p == ',') {
  730. *p = '\0';
  731. printw(" %s\n", begin);
  732. begin = p + 1;
  733. }
  734. p++;
  735. }
  736. printw(" %s", begin);
  737. }
  738. #ifdef SUPPORT_CHKSUM
  739. /* Calculating checksums can take VERY long */
  740. /* Show md5 */
  741. sprintf(buf, "openssl md5 \"%s\" 2>&1", fpath);
  742. p = get_output(buf, PATH_MAX + 48);
  743. if (p) {
  744. p = xmemrchr(buf, ' ', strlen(buf));
  745. if (!p)
  746. p = buf;
  747. else
  748. p++;
  749. printw("\n md5: %s", p);
  750. }
  751. /* Show sha256 */
  752. sprintf(buf, "openssl sha256 \"%s\" 2>&1", fpath);
  753. p = get_output(buf, PATH_MAX + 48);
  754. if (p) {
  755. p = xmemrchr(buf, ' ', strlen(buf));
  756. if (!p)
  757. p = buf;
  758. else
  759. p++;
  760. printw(" sha256: %s", p);
  761. }
  762. #endif
  763. }
  764. /* Show exit keys */
  765. printw("\n\n << (q/Esc)");
  766. for (*buf = getch(); *buf != 'q' && *buf != 27; *buf = getch())
  767. if (*buf == 'q' || *buf == 27)
  768. return;
  769. }
  770. void
  771. show_help(void)
  772. {
  773. char c;
  774. clear();
  775. printw("\n\
  776. << Key >> << Function >>\n\n\
  777. [Up], k, ^P Previous entry\n\
  778. [Down], j, ^N Next entry\n\
  779. [PgUp], ^U Scroll half page up\n\
  780. [PgDn], ^D Scroll half page down\n\
  781. [Home], g, ^, ^A Jump to first entry\n\
  782. [End], G, $, ^E Jump to last entry\n\
  783. [Right], [Enter], l, ^M Open file or enter dir\n\
  784. [Left], [Backspace], h, ^H Go to parent dir\n\
  785. ~ Jump to HOME dir\n\
  786. - Jump to last visited dir\n\
  787. /, & Filter dir contents\n\
  788. c Show change dir prompt\n\
  789. d Toggle detail view\n\
  790. D Show details of selected file\n\
  791. . Toggle hide .dot files\n\
  792. s Toggle sort by file size\n\
  793. S Toggle disk usage analyzer mode\n\
  794. t Toggle sort by modified time\n\
  795. ! Spawn SHELL in PWD (fallback sh)\n\
  796. z Run top\n\
  797. e Edit entry in EDITOR (fallback vi)\n\
  798. p Open entry in PAGER (fallback less)\n\
  799. ^K Invoke file name copier\n\
  800. ^L Force a redraw\n\
  801. ? Show help\n\
  802. q Quit\n");
  803. /* Show exit keys */
  804. printw("\n\n << (q/Esc)");
  805. for (c = getch(); c != 'q' && c != 27; c = getch())
  806. if (c == 'q' || c == 27)
  807. return;
  808. }
  809. off_t blk_size;
  810. static int
  811. sum_sizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  812. {
  813. if (!fpath || !ftwbuf)
  814. printmsg("fpath or ftwbuf NULL"); /* TODO: on %s", fpath); */
  815. /* Handle permission problems */
  816. if(typeflag == FTW_NS) {
  817. printmsg("No stats (permissions ?)"); /* TODO: on %s", fpath); */
  818. return 0;
  819. }
  820. blk_size += sb->st_blocks;
  821. return 0;
  822. }
  823. static int
  824. dentfill(char *path, struct entry **dents,
  825. int (*filter)(regex_t *, char *), regex_t *re)
  826. {
  827. static char newpath[PATH_MAX];
  828. DIR *dirp;
  829. struct dirent *dp;
  830. struct stat sb;
  831. int r, n = 0;
  832. dirp = opendir(path);
  833. if (dirp == NULL)
  834. return 0;
  835. while ((dp = readdir(dirp)) != NULL) {
  836. /* Skip self and parent */
  837. if (strcmp(dp->d_name, ".") == 0 ||
  838. strcmp(dp->d_name, "..") == 0)
  839. continue;
  840. if (filter(re, dp->d_name) == 0)
  841. continue;
  842. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  843. xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  844. /* Get mode flags */
  845. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  846. r = lstat(newpath, &sb);
  847. if (r == -1)
  848. printerr(1, "lstat");
  849. (*dents)[n].mode = sb.st_mode;
  850. (*dents)[n].t = sb.st_mtime;
  851. (*dents)[n].size = sb.st_size;
  852. if (bsizeorder) {
  853. if (S_ISDIR(sb.st_mode)) {
  854. blk_size = 0;
  855. if (nftw(newpath, sum_sizes, 128, FTW_MOUNT | FTW_PHYS) == -1) {
  856. printmsg("nftw(3) failed"); /* TODO: , newpath); */
  857. (*dents)[n].bsize = sb.st_blocks;
  858. } else
  859. (*dents)[n].bsize = blk_size;
  860. } else
  861. (*dents)[n].bsize = sb.st_blocks;
  862. }
  863. n++;
  864. }
  865. /* Should never be null */
  866. r = closedir(dirp);
  867. if (r == -1)
  868. printerr(1, "closedir");
  869. return n;
  870. }
  871. static void
  872. dentfree(struct entry *dents)
  873. {
  874. free(dents);
  875. }
  876. /* Return the position of the matching entry or 0 otherwise */
  877. static int
  878. dentfind(struct entry *dents, int n, char *path)
  879. {
  880. if (!path)
  881. return 0;
  882. int i;
  883. char *p = xmemrchr(path, '/', strlen(path));
  884. if (!p)
  885. p = path;
  886. else
  887. /* We are assuming an entry with actual
  888. name ending in '/' will not appear */
  889. p++;
  890. DPRINTF_S(p);
  891. for (i = 0; i < n; i++)
  892. if (strcmp(p, dents[i].name) == 0)
  893. return i;
  894. return 0;
  895. }
  896. static int
  897. populate(char *path, char *oldpath, char *fltr)
  898. {
  899. regex_t re;
  900. int r;
  901. /* Can fail when permissions change while browsing */
  902. if (canopendir(path) == 0)
  903. return -1;
  904. /* Search filter */
  905. r = setfilter(&re, fltr);
  906. if (r != 0)
  907. return -1;
  908. dentfree(dents);
  909. ndents = 0;
  910. dents = NULL;
  911. ndents = dentfill(path, &dents, visible, &re);
  912. qsort(dents, ndents, sizeof(*dents), entrycmp);
  913. /* Find cur from history */
  914. cur = dentfind(dents, ndents, oldpath);
  915. return 0;
  916. }
  917. static void
  918. redraw(char *path)
  919. {
  920. static char cwd[PATH_MAX];
  921. static int nlines, odd;
  922. static int i;
  923. nlines = MIN(LINES - 4, ndents);
  924. /* Clean screen */
  925. erase();
  926. /* Strip trailing slashes */
  927. for (i = strlen(path) - 1; i > 0; i--)
  928. if (path[i] == '/')
  929. path[i] = '\0';
  930. else
  931. break;
  932. DPRINTF_D(cur);
  933. DPRINTF_S(path);
  934. /* No text wrapping in cwd line */
  935. if (!realpath(path, cwd)) {
  936. printmsg("Cannot resolve path");
  937. return;
  938. }
  939. printw(CWD "%s\n\n", cwd);
  940. /* Print listing */
  941. odd = ISODD(nlines);
  942. if (cur < (nlines >> 1)) {
  943. for (i = 0; i < nlines; i++)
  944. printptr(&dents[i], i == cur);
  945. } else if (cur >= ndents - (nlines >> 1)) {
  946. for (i = ndents - nlines; i < ndents; i++)
  947. printptr(&dents[i], i == cur);
  948. } else {
  949. nlines >>= 1;
  950. for (i = cur - nlines; i < cur + nlines + odd; i++)
  951. printptr(&dents[i], i == cur);
  952. }
  953. if (showdetail) {
  954. if (ndents) {
  955. static char ind[2] = "\0\0";
  956. static char sort[17];
  957. if (mtimeorder)
  958. sprintf(sort, "by time ");
  959. else if (sizeorder)
  960. sprintf(sort, "by size ");
  961. else if (bsizeorder)
  962. sprintf(sort, "by content size ");
  963. else
  964. sort[0] = '\0';
  965. if (S_ISDIR(dents[cur].mode))
  966. ind[0] = '/';
  967. else if (S_ISLNK(dents[cur].mode))
  968. ind[0] = '@';
  969. else if (S_ISSOCK(dents[cur].mode))
  970. ind[0] = '=';
  971. else if (S_ISFIFO(dents[cur].mode))
  972. ind[0] = '|';
  973. else if (dents[cur].mode & S_IXUSR)
  974. ind[0] = '*';
  975. else
  976. ind[0] = '\0';
  977. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  978. dents[cur].name, ind);
  979. printmsg(cwd);
  980. } else
  981. printmsg("0 items");
  982. }
  983. }
  984. static void
  985. browse(char *ipath, char *ifilter)
  986. {
  987. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  988. static char lastdir[PATH_MAX];
  989. static char fltr[LINE_MAX];
  990. char *bin, *dir, *tmp, *run, *env;
  991. struct stat sb;
  992. regex_t re;
  993. int r, fd;
  994. enum action sel = SEL_RUNARG + 1;
  995. xstrlcpy(path, ipath, sizeof(path));
  996. xstrlcpy(lastdir, ipath, sizeof(lastdir));
  997. xstrlcpy(fltr, ifilter, sizeof(fltr));
  998. oldpath[0] = '\0';
  999. newpath[0] = '\0';
  1000. begin:
  1001. if (sel == SEL_GOIN && S_ISDIR(sb.st_mode))
  1002. r = populate(path, NULL, fltr);
  1003. else
  1004. r = populate(path, oldpath, fltr);
  1005. if (r == -1) {
  1006. printwarn();
  1007. goto nochange;
  1008. }
  1009. for (;;) {
  1010. redraw(path);
  1011. nochange:
  1012. sel = nextsel(&run, &env);
  1013. switch (sel) {
  1014. case SEL_QUIT:
  1015. dentfree(dents);
  1016. return;
  1017. case SEL_BACK:
  1018. /* There is no going back */
  1019. if (strcmp(path, "/") == 0 ||
  1020. strcmp(path, ".") == 0 ||
  1021. strchr(path, '/') == NULL) {
  1022. printmsg("You are at /");
  1023. goto nochange;
  1024. }
  1025. dir = xdirname(path);
  1026. if (canopendir(dir) == 0) {
  1027. printwarn();
  1028. goto nochange;
  1029. }
  1030. /* Save history */
  1031. xstrlcpy(oldpath, path, sizeof(oldpath));
  1032. /* Save last working directory */
  1033. xstrlcpy(lastdir, path, sizeof(lastdir));
  1034. xstrlcpy(path, dir, sizeof(path));
  1035. /* Reset filter */
  1036. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1037. goto begin;
  1038. case SEL_GOIN:
  1039. /* Cannot descend in empty directories */
  1040. if (ndents == 0)
  1041. goto nochange;
  1042. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  1043. DPRINTF_S(newpath);
  1044. /* Get path info */
  1045. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1046. if (fd == -1) {
  1047. printwarn();
  1048. goto nochange;
  1049. }
  1050. r = fstat(fd, &sb);
  1051. if (r == -1) {
  1052. printwarn();
  1053. close(fd);
  1054. goto nochange;
  1055. }
  1056. close(fd);
  1057. DPRINTF_U(sb.st_mode);
  1058. switch (sb.st_mode & S_IFMT) {
  1059. case S_IFDIR:
  1060. if (canopendir(newpath) == 0) {
  1061. printwarn();
  1062. goto nochange;
  1063. }
  1064. /* Save last working directory */
  1065. xstrlcpy(lastdir, path, sizeof(lastdir));
  1066. xstrlcpy(path, newpath, sizeof(path));
  1067. /* Reset filter */
  1068. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1069. goto begin;
  1070. case S_IFREG:
  1071. {
  1072. static char cmd[MAX_CMD_LEN];
  1073. static char *runvi = "vi";
  1074. static int status;
  1075. /* If default mime opener is set, use it */
  1076. if (opener) {
  1077. snprintf(cmd, MAX_CMD_LEN,
  1078. "%s \"%s\" > /dev/null 2>&1",
  1079. opener, newpath);
  1080. status = system(cmd);
  1081. continue;
  1082. }
  1083. /* Try custom applications */
  1084. bin = openwith(newpath);
  1085. /* If custom app doesn't exist try fallback */
  1086. snprintf(cmd, MAX_CMD_LEN, "which \"%s\"", bin);
  1087. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1088. bin = NULL;
  1089. if (bin == NULL) {
  1090. /* If a custom handler application is
  1091. not set, open plain text files with
  1092. vi, then try fallback_opener */
  1093. snprintf(cmd, MAX_CMD_LEN,
  1094. "file \"%s\"", newpath);
  1095. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1096. goto nochange;
  1097. if (strstr(cmd, "ASCII text") != NULL)
  1098. bin = runvi;
  1099. else if (fallback_opener) {
  1100. snprintf(cmd, MAX_CMD_LEN,
  1101. "%s \"%s\" > \
  1102. /dev/null 2>&1",
  1103. fallback_opener,
  1104. newpath);
  1105. status = system(cmd);
  1106. continue;
  1107. } else {
  1108. status++; /* Dummy operation */
  1109. printmsg("No association");
  1110. goto nochange;
  1111. }
  1112. }
  1113. exitcurses();
  1114. spawn(bin, newpath, NULL, 0);
  1115. initcurses();
  1116. continue;
  1117. }
  1118. default:
  1119. printmsg("Unsupported file");
  1120. goto nochange;
  1121. }
  1122. case SEL_FLTR:
  1123. /* Read filter */
  1124. printprompt("filter: ");
  1125. tmp = readln();
  1126. if (tmp == NULL)
  1127. tmp = ifilter;
  1128. /* Check and report regex errors */
  1129. r = setfilter(&re, tmp);
  1130. if (r != 0)
  1131. goto nochange;
  1132. xstrlcpy(fltr, tmp, sizeof(fltr));
  1133. DPRINTF_S(fltr);
  1134. /* Save current */
  1135. if (ndents > 0)
  1136. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1137. goto begin;
  1138. case SEL_NEXT:
  1139. if (cur < ndents - 1)
  1140. cur++;
  1141. else if (ndents)
  1142. /* Roll over, set cursor to first entry */
  1143. cur = 0;
  1144. break;
  1145. case SEL_PREV:
  1146. if (cur > 0)
  1147. cur--;
  1148. else if (ndents)
  1149. /* Roll over, set cursor to last entry */
  1150. cur = ndents - 1;
  1151. break;
  1152. case SEL_PGDN:
  1153. if (cur < ndents - 1)
  1154. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1155. break;
  1156. case SEL_PGUP:
  1157. if (cur > 0)
  1158. cur -= MIN((LINES - 4) / 2, cur);
  1159. break;
  1160. case SEL_HOME:
  1161. cur = 0;
  1162. break;
  1163. case SEL_END:
  1164. cur = ndents - 1;
  1165. break;
  1166. case SEL_CD:
  1167. /* Read target dir */
  1168. printprompt("chdir: ");
  1169. tmp = readln();
  1170. if (tmp == NULL) {
  1171. clearprompt();
  1172. goto nochange;
  1173. }
  1174. if (tmp[0] == '~') {
  1175. char *home = getenv("HOME");
  1176. if (home)
  1177. snprintf(newpath, PATH_MAX,
  1178. "%s%s", home, tmp + 1);
  1179. else
  1180. mkpath(path, tmp, newpath, sizeof(newpath));
  1181. } else if (tmp[0] == '-' && tmp[1] == '\0')
  1182. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1183. else
  1184. mkpath(path, tmp, newpath, sizeof(newpath));
  1185. if (canopendir(newpath) == 0) {
  1186. printwarn();
  1187. goto nochange;
  1188. }
  1189. /* Save last working directory */
  1190. xstrlcpy(lastdir, path, sizeof(lastdir));
  1191. xstrlcpy(path, newpath, sizeof(path));
  1192. /* Reset filter */
  1193. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1194. DPRINTF_S(path);
  1195. goto begin;
  1196. case SEL_CDHOME:
  1197. tmp = getenv("HOME");
  1198. if (tmp == NULL) {
  1199. clearprompt();
  1200. goto nochange;
  1201. }
  1202. if (canopendir(tmp) == 0) {
  1203. printwarn();
  1204. goto nochange;
  1205. }
  1206. /* Save last working directory */
  1207. xstrlcpy(lastdir, path, sizeof(lastdir));
  1208. xstrlcpy(path, tmp, sizeof(path));
  1209. /* Reset filter */
  1210. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1211. DPRINTF_S(path);
  1212. goto begin;
  1213. case SEL_LAST:
  1214. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1215. xstrlcpy(lastdir, path, sizeof(lastdir));
  1216. xstrlcpy(path, newpath, sizeof(path));
  1217. DPRINTF_S(path);
  1218. goto begin;
  1219. case SEL_TOGGLEDOT:
  1220. showhidden ^= 1;
  1221. initfilter(showhidden, &ifilter);
  1222. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1223. goto begin;
  1224. case SEL_DETAIL:
  1225. showdetail = !showdetail;
  1226. showdetail ? (printptr = &printent_long)
  1227. : (printptr = &printent);
  1228. /* Save current */
  1229. if (ndents > 0)
  1230. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1231. goto begin;
  1232. case SEL_STATS:
  1233. {
  1234. struct stat sb;
  1235. if (ndents > 0)
  1236. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1237. r = lstat(oldpath, &sb);
  1238. if (r == -1)
  1239. printerr(1, "lstat");
  1240. else
  1241. show_stats(oldpath, dents[cur].name, &sb);
  1242. goto begin;
  1243. }
  1244. case SEL_FSIZE:
  1245. sizeorder = !sizeorder;
  1246. mtimeorder = 0;
  1247. bsizeorder = 0;
  1248. /* Save current */
  1249. if (ndents > 0)
  1250. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1251. goto begin;
  1252. case SEL_BSIZE:
  1253. bsizeorder = !bsizeorder;
  1254. mtimeorder = 0;
  1255. sizeorder = 0;
  1256. /* Save current */
  1257. if (ndents > 0)
  1258. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1259. goto begin;
  1260. case SEL_MTIME:
  1261. mtimeorder = !mtimeorder;
  1262. sizeorder = 0;
  1263. bsizeorder = 0;
  1264. /* Save current */
  1265. if (ndents > 0)
  1266. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1267. goto begin;
  1268. case SEL_REDRAW:
  1269. /* Save current */
  1270. if (ndents > 0)
  1271. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1272. goto begin;
  1273. case SEL_COPY:
  1274. if (copier && ndents) {
  1275. char abspath[PATH_MAX];
  1276. if (strcmp(path, "/") == 0)
  1277. snprintf(abspath, PATH_MAX, "/%s",
  1278. dents[cur].name);
  1279. else
  1280. snprintf(abspath, PATH_MAX, "%s/%s",
  1281. path, dents[cur].name);
  1282. spawn(copier, abspath, NULL, 0);
  1283. printmsg(abspath);
  1284. } else if (!copier)
  1285. printmsg("NNN_COPIER is not set");
  1286. goto nochange;
  1287. case SEL_HELP:
  1288. show_help();
  1289. /* Save current */
  1290. if (ndents > 0)
  1291. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1292. goto begin;
  1293. case SEL_RUN:
  1294. run = xgetenv(env, run);
  1295. exitcurses();
  1296. spawn(run, NULL, path, 1);
  1297. initcurses();
  1298. /* Repopulate as directory content may have changed */
  1299. goto begin;
  1300. case SEL_RUNARG:
  1301. run = xgetenv(env, run);
  1302. exitcurses();
  1303. spawn(run, dents[cur].name, path, 0);
  1304. initcurses();
  1305. break;
  1306. }
  1307. /* Screensaver */
  1308. if (idletimeout != 0 && idle == idletimeout) {
  1309. idle = 0;
  1310. exitcurses();
  1311. spawn(idlecmd, NULL, NULL, 0);
  1312. initcurses();
  1313. }
  1314. }
  1315. }
  1316. static void
  1317. usage(void)
  1318. {
  1319. fprintf(stderr, "usage: nnn [-d] [dir]\n");
  1320. exit(1);
  1321. }
  1322. int
  1323. main(int argc, char *argv[])
  1324. {
  1325. char cwd[PATH_MAX], *ipath;
  1326. char *ifilter;
  1327. int opt = 0;
  1328. /* Confirm we are in a terminal */
  1329. if (!isatty(0) || !isatty(1)) {
  1330. fprintf(stderr, "stdin or stdout is not a tty\n");
  1331. exit(1);
  1332. }
  1333. if (argc > 3)
  1334. usage();
  1335. while ((opt = getopt(argc, argv, "d")) != -1) {
  1336. switch (opt) {
  1337. case 'd':
  1338. /* Open in detail mode, if set */
  1339. showdetail = 1;
  1340. printptr = &printent_long;
  1341. break;
  1342. default:
  1343. usage();
  1344. }
  1345. }
  1346. if (argc == optind) {
  1347. /* Start in the current directory */
  1348. ipath = getcwd(cwd, sizeof(cwd));
  1349. if (ipath == NULL)
  1350. ipath = "/";
  1351. } else {
  1352. ipath = realpath(argv[optind], cwd);
  1353. if (!ipath) {
  1354. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1355. exit(1);
  1356. }
  1357. }
  1358. if (getuid() == 0)
  1359. showhidden = 1;
  1360. initfilter(showhidden, &ifilter);
  1361. /* Get the default desktop mime opener, if set */
  1362. opener = getenv("NNN_OPENER");
  1363. /* Get the fallback desktop mime opener, if set */
  1364. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  1365. /* Get the default copier, if set */
  1366. copier = getenv("NNN_COPIER");
  1367. signal(SIGINT, SIG_IGN);
  1368. /* Test initial path */
  1369. if (canopendir(ipath) == 0) {
  1370. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  1371. exit(1);
  1372. }
  1373. /* Set locale before curses setup */
  1374. setlocale(LC_ALL, "");
  1375. initcurses();
  1376. browse(ipath, ifilter);
  1377. exitcurses();
  1378. exit(0);
  1379. }