My build of nnn with minor changes
 
 
 
 
 
 

1588 lines
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 off_t blk_size;
  112. static size_t fs_free;
  113. static const double div_2_pow_10 = 1.0 / 1024.0;
  114. static const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  115. /*
  116. * Layout:
  117. * .---------
  118. * | cwd: /mnt/path
  119. * |
  120. * | file0
  121. * | file1
  122. * | > file2
  123. * | file3
  124. * | file4
  125. * ...
  126. * | filen
  127. * |
  128. * | Permission denied
  129. * '------
  130. */
  131. static void printmsg(char *);
  132. static void printwarn(void);
  133. static void printerr(int, char *);
  134. static void *
  135. xrealloc(void *p, size_t size)
  136. {
  137. p = realloc(p, size);
  138. if (p == NULL)
  139. printerr(1, "realloc");
  140. return p;
  141. }
  142. static size_t
  143. xstrlcpy(char *dest, const char *src, size_t n)
  144. {
  145. size_t i;
  146. for (i = 0; i < n && *src; i++)
  147. *dest++ = *src++;
  148. if (n) {
  149. *dest = '\0';
  150. #ifdef CHECK_XSTRLCPY_RET
  151. /* Compiling this out as we are not checking
  152. the return value anywhere (controlled case).
  153. Just returning the number of bytes copied. */
  154. while(*src++)
  155. i++;
  156. #endif
  157. }
  158. return i;
  159. }
  160. /*
  161. * The poor man's implementation of memrchr(3).
  162. * We are only looking for '/' in this program.
  163. */
  164. static void *
  165. xmemrchr(const void *s, int c, size_t n)
  166. {
  167. unsigned char *p;
  168. unsigned char ch = (unsigned char)c;
  169. if (!s || !n)
  170. return NULL;
  171. p = (unsigned char *)s + n - 1;
  172. while(n--)
  173. if ((*p--) == ch)
  174. return ++p;
  175. return NULL;
  176. }
  177. #if 0
  178. /* Some implementations of dirname(3) may modify `path' and some
  179. * return a pointer inside `path'. */
  180. static char *
  181. xdirname(const char *path)
  182. {
  183. static char out[PATH_MAX];
  184. char tmp[PATH_MAX], *p;
  185. xstrlcpy(tmp, path, sizeof(tmp));
  186. p = dirname(tmp);
  187. if (p == NULL)
  188. printerr(1, "dirname");
  189. xstrlcpy(out, p, sizeof(out));
  190. return out;
  191. }
  192. #endif
  193. /*
  194. * The following dirname(3) implementation does not
  195. * change the input. We use a copy of the original.
  196. *
  197. * Modified from the glibc (GNU LGPL) version.
  198. */
  199. static char *
  200. xdirname(const char *path)
  201. {
  202. static char name[PATH_MAX];
  203. char *last_slash;
  204. xstrlcpy(name, path, PATH_MAX);
  205. /* Find last '/'. */
  206. last_slash = strrchr(name, '/');
  207. if (last_slash != NULL && last_slash != name && last_slash[1] == '\0') {
  208. /* Determine whether all remaining characters are slashes. */
  209. char *runp;
  210. for (runp = last_slash; runp != name; --runp)
  211. if (runp[-1] != '/')
  212. break;
  213. /* The '/' is the last character, we have to look further. */
  214. if (runp != name)
  215. last_slash = xmemrchr(name, '/', runp - name);
  216. }
  217. if (last_slash != NULL) {
  218. /* Determine whether all remaining characters are slashes. */
  219. char *runp;
  220. for (runp = last_slash; runp != name; --runp)
  221. if (runp[-1] != '/')
  222. break;
  223. /* Terminate the name. */
  224. if (runp == name) {
  225. /* The last slash is the first character in the string.
  226. We have to return "/". As a special case we have to
  227. return "//" if there are exactly two slashes at the
  228. beginning of the string. See XBD 4.10 Path Name
  229. Resolution for more information. */
  230. if (last_slash == name + 1)
  231. ++last_slash;
  232. else
  233. last_slash = name + 1;
  234. } else
  235. last_slash = runp;
  236. last_slash[0] = '\0';
  237. } else {
  238. /* This assignment is ill-designed but the XPG specs require to
  239. return a string containing "." in any case no directory part
  240. is found and so a static and constant string is required. */
  241. name[0] = '.';
  242. name[1] = '\0';
  243. }
  244. return name;
  245. }
  246. static void
  247. spawn(char *file, char *arg, char *dir, int notify)
  248. {
  249. pid_t pid;
  250. int status;
  251. pid = fork();
  252. if (pid == 0) {
  253. if (dir != NULL)
  254. status = chdir(dir);
  255. if (notify)
  256. fprintf(stdout, "\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
  257. execlp(file, file, arg, NULL);
  258. _exit(1);
  259. } else {
  260. /* Ignore interruptions */
  261. while (waitpid(pid, &status, 0) == -1)
  262. DPRINTF_D(status);
  263. DPRINTF_D(pid);
  264. }
  265. }
  266. static char *
  267. xgetenv(char *name, char *fallback)
  268. {
  269. char *value;
  270. if (name == NULL)
  271. return fallback;
  272. value = getenv(name);
  273. return value && value[0] ? value : fallback;
  274. }
  275. /*
  276. * We assume none of the strings are NULL.
  277. *
  278. * Let's have the logic to sort numeric names in numeric order.
  279. * E.g., the order '1, 10, 2' doesn't make sense to human eyes.
  280. *
  281. * If the absolute numeric values are same, we fallback to alphasort.
  282. */
  283. static int
  284. xstricmp(const char *s1, const char *s2)
  285. {
  286. static char *c1, *c2;
  287. static long long num1, num2;
  288. num1 = strtoll(s1, &c1, 10);
  289. num2 = strtoll(s2, &c2, 10);
  290. if (*c1 == '\0' && *c2 == '\0') {
  291. if (num1 != num2) {
  292. if (num1 > num2)
  293. return 1;
  294. else
  295. return -1;
  296. }
  297. } else if (*c1 == '\0' && *c2 != '\0')
  298. return -1;
  299. else if (*c1 != '\0' && *c2 == '\0')
  300. return 1;
  301. while (*s2 && *s1 && TOUPPER(*s1) == TOUPPER(*s2))
  302. s1++, s2++;
  303. /* In case of alphabetically same names, make sure
  304. lower case one comes before upper case one */
  305. if (!*s1 && !*s2)
  306. return 1;
  307. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  308. }
  309. static char *
  310. openwith(char *file)
  311. {
  312. regex_t regex;
  313. char *bin = NULL;
  314. unsigned int i;
  315. for (i = 0; i < LEN(assocs); i++) {
  316. if (regcomp(&regex, assocs[i].regex,
  317. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  318. continue;
  319. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  320. bin = assocs[i].bin;
  321. break;
  322. }
  323. }
  324. DPRINTF_S(bin);
  325. return bin;
  326. }
  327. static int
  328. setfilter(regex_t *regex, char *filter)
  329. {
  330. char errbuf[LINE_MAX];
  331. size_t len;
  332. int r;
  333. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  334. if (r != 0) {
  335. len = COLS;
  336. if (len > sizeof(errbuf))
  337. len = sizeof(errbuf);
  338. regerror(r, regex, errbuf, len);
  339. printmsg(errbuf);
  340. }
  341. return r;
  342. }
  343. static void
  344. initfilter(int dot, char **ifilter)
  345. {
  346. *ifilter = dot ? "." : "^[^.]";
  347. }
  348. static int
  349. visible(regex_t *regex, char *file)
  350. {
  351. return regexec(regex, file, 0, NULL, 0) == 0;
  352. }
  353. static int
  354. entrycmp(const void *va, const void *vb)
  355. {
  356. static pEntry pa, pb;
  357. pa = (pEntry)va;
  358. pb = (pEntry)vb;
  359. /* Sort directories first */
  360. if (S_ISDIR(pb->mode) && !S_ISDIR(pa->mode))
  361. return 1;
  362. else if (S_ISDIR(pa->mode) && !S_ISDIR(pb->mode))
  363. return -1;
  364. /* Do the actual sorting */
  365. if (mtimeorder)
  366. return pb->t - pa->t;
  367. if (sizeorder) {
  368. if (pb->size > pa->size)
  369. return 1;
  370. else if (pb->size < pa->size)
  371. return -1;
  372. }
  373. if (bsizeorder) {
  374. if (pb->bsize > pa->bsize)
  375. return 1;
  376. else if (pb->bsize < pa->bsize)
  377. return -1;
  378. }
  379. return xstricmp(pa->name, pb->name);
  380. }
  381. static void
  382. initcurses(void)
  383. {
  384. if (initscr() == NULL) {
  385. char *term = getenv("TERM");
  386. if (term != NULL)
  387. fprintf(stderr, "error opening terminal: %s\n", term);
  388. else
  389. fprintf(stderr, "failed to initialize curses\n");
  390. exit(1);
  391. }
  392. cbreak();
  393. noecho();
  394. nonl();
  395. intrflush(stdscr, FALSE);
  396. keypad(stdscr, TRUE);
  397. curs_set(FALSE); /* Hide cursor */
  398. timeout(1000); /* One second */
  399. }
  400. static void
  401. exitcurses(void)
  402. {
  403. endwin(); /* Restore terminal */
  404. }
  405. /* Messages show up at the bottom */
  406. static void
  407. printmsg(char *msg)
  408. {
  409. move(LINES - 1, 0);
  410. printw("%s\n", msg);
  411. }
  412. /* Display warning as a message */
  413. static void
  414. printwarn(void)
  415. {
  416. printmsg(strerror(errno));
  417. }
  418. /* Kill curses and display error before exiting */
  419. static void
  420. printerr(int ret, char *prefix)
  421. {
  422. exitcurses();
  423. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  424. exit(ret);
  425. }
  426. /* Clear the last line */
  427. static void
  428. clearprompt(void)
  429. {
  430. printmsg("");
  431. }
  432. /* Print prompt on the last line */
  433. static void
  434. printprompt(char *str)
  435. {
  436. clearprompt();
  437. printw(str);
  438. }
  439. /* Returns SEL_* if key is bound and 0 otherwise.
  440. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  441. static int
  442. nextsel(char **run, char **env)
  443. {
  444. int c;
  445. unsigned int i;
  446. c = getch();
  447. if (c == -1)
  448. idle++;
  449. else
  450. idle = 0;
  451. for (i = 0; i < LEN(bindings); i++)
  452. if (c == bindings[i].sym) {
  453. *run = bindings[i].run;
  454. *env = bindings[i].env;
  455. return bindings[i].act;
  456. }
  457. return 0;
  458. }
  459. static char *
  460. readln(void)
  461. {
  462. static char ln[LINE_MAX];
  463. timeout(-1);
  464. echo();
  465. curs_set(TRUE);
  466. memset(ln, 0, sizeof(ln));
  467. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  468. noecho();
  469. curs_set(FALSE);
  470. timeout(1000);
  471. return ln[0] ? ln : NULL;
  472. }
  473. static int
  474. canopendir(char *path)
  475. {
  476. static DIR *dirp;
  477. dirp = opendir(path);
  478. if (dirp == NULL)
  479. return 0;
  480. closedir(dirp);
  481. return 1;
  482. }
  483. /*
  484. * Returns "dir/name or "/name"
  485. */
  486. static char *
  487. mkpath(char *dir, char *name, char *out, size_t n)
  488. {
  489. /* Handle absolute path */
  490. if (name[0] == '/')
  491. xstrlcpy(out, name, n);
  492. else {
  493. /* Handle root case */
  494. if (strcmp(dir, "/") == 0)
  495. snprintf(out, n, "/%s", name);
  496. else
  497. snprintf(out, n, "%s/%s", dir, name);
  498. }
  499. return out;
  500. }
  501. static void
  502. printent(struct entry *ent, int active)
  503. {
  504. if (S_ISDIR(ent->mode))
  505. printw("%s%s/\n", CURSYM(active), ent->name);
  506. else if (S_ISLNK(ent->mode))
  507. printw("%s%s@\n", CURSYM(active), ent->name);
  508. else if (S_ISSOCK(ent->mode))
  509. printw("%s%s=\n", CURSYM(active), ent->name);
  510. else if (S_ISFIFO(ent->mode))
  511. printw("%s%s|\n", CURSYM(active), ent->name);
  512. else if (ent->mode & S_IXUSR)
  513. printw("%s%s*\n", CURSYM(active), ent->name);
  514. else
  515. printw("%s%s\n", CURSYM(active), ent->name);
  516. }
  517. static void (*printptr)(struct entry *ent, int active) = &printent;
  518. static char*
  519. coolsize(off_t size)
  520. {
  521. static char size_buf[12]; /* Buffer to hold human readable size */
  522. static int i;
  523. static off_t fsize, tmp;
  524. static long double rem;
  525. i = 0;
  526. fsize = size;
  527. 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. static 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. static 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. static 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. static 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. 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. static DIR *dirp;
  829. static struct dirent *dp;
  830. static struct stat sb;
  831. static struct statvfs svb;
  832. static int r, n;
  833. r = n = 0;
  834. dirp = opendir(path);
  835. if (dirp == NULL)
  836. return 0;
  837. r = statvfs(path, &svb);
  838. if (r == -1)
  839. fs_free = 0;
  840. else
  841. fs_free = svb.f_bsize * svb.f_bavail;
  842. while ((dp = readdir(dirp)) != NULL) {
  843. /* Skip self and parent */
  844. if (strcmp(dp->d_name, ".") == 0 ||
  845. strcmp(dp->d_name, "..") == 0)
  846. continue;
  847. if (filter(re, dp->d_name) == 0)
  848. continue;
  849. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  850. xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  851. /* Get mode flags */
  852. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  853. r = lstat(newpath, &sb);
  854. if (r == -1)
  855. printerr(1, "lstat");
  856. (*dents)[n].mode = sb.st_mode;
  857. (*dents)[n].t = sb.st_mtime;
  858. (*dents)[n].size = sb.st_size;
  859. if (bsizeorder) {
  860. if (S_ISDIR(sb.st_mode)) {
  861. blk_size = 0;
  862. if (nftw(newpath, sum_sizes, 128, FTW_MOUNT | FTW_PHYS) == -1) {
  863. printmsg("nftw(3) failed"); /* TODO: , newpath); */
  864. (*dents)[n].bsize = sb.st_blocks;
  865. } else
  866. (*dents)[n].bsize = blk_size;
  867. } else
  868. (*dents)[n].bsize = sb.st_blocks;
  869. }
  870. n++;
  871. }
  872. /* Should never be null */
  873. r = closedir(dirp);
  874. if (r == -1)
  875. printerr(1, "closedir");
  876. return n;
  877. }
  878. static void
  879. dentfree(struct entry *dents)
  880. {
  881. free(dents);
  882. }
  883. /* Return the position of the matching entry or 0 otherwise */
  884. static int
  885. dentfind(struct entry *dents, int n, char *path)
  886. {
  887. if (!path)
  888. return 0;
  889. static int i;
  890. static char *p;
  891. 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. static regex_t re;
  908. static 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. }