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.
 
 
 
 
 
 

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