My build of nnn with minor changes
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

1584 行
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 << (D)");
  767. while ((*buf = getch()) != 'D');
  768. return;
  769. }
  770. static 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 Toggle current file details screen\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. ? Toggle help screen\n\
  802. q Quit\n");
  803. /* Show exit keys */
  804. printw("\n\n << (?)");
  805. while ((c = getch()) != '?');
  806. return;
  807. }
  808. static int
  809. sum_sizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  810. {
  811. if (!fpath || !ftwbuf)
  812. printmsg("fpath or ftwbuf NULL"); /* TODO: on %s", fpath); */
  813. /* Handle permission problems */
  814. if(typeflag == FTW_NS) {
  815. printmsg("No stats (permissions ?)"); /* TODO: on %s", fpath); */
  816. return 0;
  817. }
  818. blk_size += sb->st_blocks;
  819. return 0;
  820. }
  821. static int
  822. dentfill(char *path, struct entry **dents,
  823. int (*filter)(regex_t *, char *), regex_t *re)
  824. {
  825. static char newpath[PATH_MAX];
  826. static DIR *dirp;
  827. static struct dirent *dp;
  828. static struct stat sb;
  829. static struct statvfs svb;
  830. static int r, n;
  831. r = n = 0;
  832. dirp = opendir(path);
  833. if (dirp == NULL)
  834. return 0;
  835. r = statvfs(path, &svb);
  836. if (r == -1)
  837. fs_free = 0;
  838. else
  839. fs_free = svb.f_bsize * svb.f_bavail;
  840. while ((dp = readdir(dirp)) != NULL) {
  841. /* Skip self and parent */
  842. if (strcmp(dp->d_name, ".") == 0 ||
  843. strcmp(dp->d_name, "..") == 0)
  844. continue;
  845. if (filter(re, dp->d_name) == 0)
  846. continue;
  847. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  848. xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  849. /* Get mode flags */
  850. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  851. r = lstat(newpath, &sb);
  852. if (r == -1)
  853. printerr(1, "lstat");
  854. (*dents)[n].mode = sb.st_mode;
  855. (*dents)[n].t = sb.st_mtime;
  856. (*dents)[n].size = sb.st_size;
  857. if (bsizeorder) {
  858. if (S_ISDIR(sb.st_mode)) {
  859. blk_size = 0;
  860. if (nftw(newpath, sum_sizes, 128, FTW_MOUNT | FTW_PHYS) == -1) {
  861. printmsg("nftw(3) failed"); /* TODO: , newpath); */
  862. (*dents)[n].bsize = sb.st_blocks;
  863. } else
  864. (*dents)[n].bsize = blk_size;
  865. } else
  866. (*dents)[n].bsize = sb.st_blocks;
  867. }
  868. n++;
  869. }
  870. /* Should never be null */
  871. r = closedir(dirp);
  872. if (r == -1)
  873. printerr(1, "closedir");
  874. return n;
  875. }
  876. static void
  877. dentfree(struct entry *dents)
  878. {
  879. free(dents);
  880. }
  881. /* Return the position of the matching entry or 0 otherwise */
  882. static int
  883. dentfind(struct entry *dents, int n, char *path)
  884. {
  885. if (!path)
  886. return 0;
  887. static int i;
  888. static char *p;
  889. p = xmemrchr(path, '/', strlen(path));
  890. if (!p)
  891. p = path;
  892. else
  893. /* We are assuming an entry with actual
  894. name ending in '/' will not appear */
  895. p++;
  896. DPRINTF_S(p);
  897. for (i = 0; i < n; i++)
  898. if (strcmp(p, dents[i].name) == 0)
  899. return i;
  900. return 0;
  901. }
  902. static int
  903. populate(char *path, char *oldpath, char *fltr)
  904. {
  905. static regex_t re;
  906. static int r;
  907. /* Can fail when permissions change while browsing */
  908. if (canopendir(path) == 0)
  909. return -1;
  910. /* Search filter */
  911. r = setfilter(&re, fltr);
  912. if (r != 0)
  913. return -1;
  914. dentfree(dents);
  915. ndents = 0;
  916. dents = NULL;
  917. ndents = dentfill(path, &dents, visible, &re);
  918. qsort(dents, ndents, sizeof(*dents), entrycmp);
  919. /* Find cur from history */
  920. cur = dentfind(dents, ndents, oldpath);
  921. return 0;
  922. }
  923. static void
  924. redraw(char *path)
  925. {
  926. static char cwd[PATH_MAX];
  927. static int nlines, odd;
  928. static int i;
  929. nlines = MIN(LINES - 4, ndents);
  930. /* Clean screen */
  931. erase();
  932. /* Strip trailing slashes */
  933. for (i = strlen(path) - 1; i > 0; i--)
  934. if (path[i] == '/')
  935. path[i] = '\0';
  936. else
  937. break;
  938. DPRINTF_D(cur);
  939. DPRINTF_S(path);
  940. /* No text wrapping in cwd line */
  941. if (!realpath(path, cwd)) {
  942. printmsg("Cannot resolve path");
  943. return;
  944. }
  945. printw(CWD "%s\n\n", cwd);
  946. /* Print listing */
  947. odd = ISODD(nlines);
  948. if (cur < (nlines >> 1)) {
  949. for (i = 0; i < nlines; i++)
  950. printptr(&dents[i], i == cur);
  951. } else if (cur >= ndents - (nlines >> 1)) {
  952. for (i = ndents - nlines; i < ndents; i++)
  953. printptr(&dents[i], i == cur);
  954. } else {
  955. nlines >>= 1;
  956. for (i = cur - nlines; i < cur + nlines + odd; i++)
  957. printptr(&dents[i], i == cur);
  958. }
  959. if (showdetail) {
  960. if (ndents) {
  961. static char ind[2] = "\0\0";
  962. static char sort[17];
  963. if (mtimeorder)
  964. sprintf(sort, "by time ");
  965. else if (sizeorder)
  966. sprintf(sort, "by size ");
  967. else
  968. sort[0] = '\0';
  969. if (S_ISDIR(dents[cur].mode))
  970. ind[0] = '/';
  971. else if (S_ISLNK(dents[cur].mode))
  972. ind[0] = '@';
  973. else if (S_ISSOCK(dents[cur].mode))
  974. ind[0] = '=';
  975. else if (S_ISFIFO(dents[cur].mode))
  976. ind[0] = '|';
  977. else if (dents[cur].mode & S_IXUSR)
  978. ind[0] = '*';
  979. else
  980. ind[0] = '\0';
  981. if (!bsizeorder)
  982. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  983. dents[cur].name, ind);
  984. else
  985. sprintf(cwd, "total %d by disk usage, %s free [%s%s]",
  986. ndents, coolsize(fs_free), dents[cur].name, ind);
  987. printmsg(cwd);
  988. } else
  989. printmsg("0 items");
  990. }
  991. }
  992. static void
  993. browse(char *ipath, char *ifilter)
  994. {
  995. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  996. static char lastdir[PATH_MAX];
  997. static char fltr[LINE_MAX];
  998. char *bin, *dir, *tmp, *run, *env;
  999. struct stat sb;
  1000. regex_t re;
  1001. int r, fd;
  1002. enum action sel = SEL_RUNARG + 1;
  1003. xstrlcpy(path, ipath, sizeof(path));
  1004. xstrlcpy(lastdir, ipath, sizeof(lastdir));
  1005. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1006. oldpath[0] = '\0';
  1007. newpath[0] = '\0';
  1008. begin:
  1009. if (sel == SEL_GOIN && S_ISDIR(sb.st_mode))
  1010. r = populate(path, NULL, fltr);
  1011. else
  1012. r = populate(path, oldpath, fltr);
  1013. if (r == -1) {
  1014. printwarn();
  1015. goto nochange;
  1016. }
  1017. for (;;) {
  1018. redraw(path);
  1019. nochange:
  1020. sel = nextsel(&run, &env);
  1021. switch (sel) {
  1022. case SEL_QUIT:
  1023. dentfree(dents);
  1024. return;
  1025. case SEL_BACK:
  1026. /* There is no going back */
  1027. if (strcmp(path, "/") == 0 ||
  1028. strcmp(path, ".") == 0 ||
  1029. strchr(path, '/') == NULL) {
  1030. printmsg("You are at /");
  1031. goto nochange;
  1032. }
  1033. dir = xdirname(path);
  1034. if (canopendir(dir) == 0) {
  1035. printwarn();
  1036. goto nochange;
  1037. }
  1038. /* Save history */
  1039. xstrlcpy(oldpath, path, sizeof(oldpath));
  1040. /* Save last working directory */
  1041. xstrlcpy(lastdir, path, sizeof(lastdir));
  1042. xstrlcpy(path, dir, sizeof(path));
  1043. /* Reset filter */
  1044. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1045. goto begin;
  1046. case SEL_GOIN:
  1047. /* Cannot descend in empty directories */
  1048. if (ndents == 0)
  1049. goto nochange;
  1050. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  1051. DPRINTF_S(newpath);
  1052. /* Get path info */
  1053. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1054. if (fd == -1) {
  1055. printwarn();
  1056. goto nochange;
  1057. }
  1058. r = fstat(fd, &sb);
  1059. if (r == -1) {
  1060. printwarn();
  1061. close(fd);
  1062. goto nochange;
  1063. }
  1064. close(fd);
  1065. DPRINTF_U(sb.st_mode);
  1066. switch (sb.st_mode & S_IFMT) {
  1067. case S_IFDIR:
  1068. if (canopendir(newpath) == 0) {
  1069. printwarn();
  1070. goto nochange;
  1071. }
  1072. /* Save last working directory */
  1073. xstrlcpy(lastdir, path, sizeof(lastdir));
  1074. xstrlcpy(path, newpath, sizeof(path));
  1075. /* Reset filter */
  1076. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1077. goto begin;
  1078. case S_IFREG:
  1079. {
  1080. static char cmd[MAX_CMD_LEN];
  1081. static char *runvi = "vi";
  1082. static int status;
  1083. /* If default mime opener is set, use it */
  1084. if (opener) {
  1085. snprintf(cmd, MAX_CMD_LEN,
  1086. "%s \"%s\" > /dev/null 2>&1",
  1087. opener, newpath);
  1088. status = system(cmd);
  1089. continue;
  1090. }
  1091. /* Try custom applications */
  1092. bin = openwith(newpath);
  1093. /* If custom app doesn't exist try fallback */
  1094. snprintf(cmd, MAX_CMD_LEN, "which \"%s\"", bin);
  1095. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1096. bin = NULL;
  1097. if (bin == NULL) {
  1098. /* If a custom handler application is
  1099. not set, open plain text files with
  1100. vi, then try fallback_opener */
  1101. snprintf(cmd, MAX_CMD_LEN,
  1102. "file \"%s\"", newpath);
  1103. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1104. goto nochange;
  1105. if (strstr(cmd, "ASCII text") != NULL)
  1106. bin = runvi;
  1107. else if (fallback_opener) {
  1108. snprintf(cmd, MAX_CMD_LEN,
  1109. "%s \"%s\" > \
  1110. /dev/null 2>&1",
  1111. fallback_opener,
  1112. newpath);
  1113. status = system(cmd);
  1114. continue;
  1115. } else {
  1116. status++; /* Dummy operation */
  1117. printmsg("No association");
  1118. goto nochange;
  1119. }
  1120. }
  1121. exitcurses();
  1122. spawn(bin, newpath, NULL, 0);
  1123. initcurses();
  1124. continue;
  1125. }
  1126. default:
  1127. printmsg("Unsupported file");
  1128. goto nochange;
  1129. }
  1130. case SEL_FLTR:
  1131. /* Read filter */
  1132. printprompt("filter: ");
  1133. tmp = readln();
  1134. if (tmp == NULL)
  1135. tmp = ifilter;
  1136. /* Check and report regex errors */
  1137. r = setfilter(&re, tmp);
  1138. if (r != 0)
  1139. goto nochange;
  1140. xstrlcpy(fltr, tmp, sizeof(fltr));
  1141. DPRINTF_S(fltr);
  1142. /* Save current */
  1143. if (ndents > 0)
  1144. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1145. goto begin;
  1146. case SEL_NEXT:
  1147. if (cur < ndents - 1)
  1148. cur++;
  1149. else if (ndents)
  1150. /* Roll over, set cursor to first entry */
  1151. cur = 0;
  1152. break;
  1153. case SEL_PREV:
  1154. if (cur > 0)
  1155. cur--;
  1156. else if (ndents)
  1157. /* Roll over, set cursor to last entry */
  1158. cur = ndents - 1;
  1159. break;
  1160. case SEL_PGDN:
  1161. if (cur < ndents - 1)
  1162. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1163. break;
  1164. case SEL_PGUP:
  1165. if (cur > 0)
  1166. cur -= MIN((LINES - 4) / 2, cur);
  1167. break;
  1168. case SEL_HOME:
  1169. cur = 0;
  1170. break;
  1171. case SEL_END:
  1172. cur = ndents - 1;
  1173. break;
  1174. case SEL_CD:
  1175. /* Read target dir */
  1176. printprompt("chdir: ");
  1177. tmp = readln();
  1178. if (tmp == NULL) {
  1179. clearprompt();
  1180. goto nochange;
  1181. }
  1182. if (tmp[0] == '~') {
  1183. char *home = getenv("HOME");
  1184. if (home)
  1185. snprintf(newpath, PATH_MAX,
  1186. "%s%s", home, tmp + 1);
  1187. else
  1188. mkpath(path, tmp, newpath, sizeof(newpath));
  1189. } else if (tmp[0] == '-' && tmp[1] == '\0')
  1190. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1191. else
  1192. mkpath(path, tmp, newpath, sizeof(newpath));
  1193. if (canopendir(newpath) == 0) {
  1194. printwarn();
  1195. goto nochange;
  1196. }
  1197. /* Save last working directory */
  1198. xstrlcpy(lastdir, path, sizeof(lastdir));
  1199. xstrlcpy(path, newpath, sizeof(path));
  1200. /* Reset filter */
  1201. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1202. DPRINTF_S(path);
  1203. goto begin;
  1204. case SEL_CDHOME:
  1205. tmp = getenv("HOME");
  1206. if (tmp == NULL) {
  1207. clearprompt();
  1208. goto nochange;
  1209. }
  1210. if (canopendir(tmp) == 0) {
  1211. printwarn();
  1212. goto nochange;
  1213. }
  1214. /* Save last working directory */
  1215. xstrlcpy(lastdir, path, sizeof(lastdir));
  1216. xstrlcpy(path, tmp, sizeof(path));
  1217. /* Reset filter */
  1218. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1219. DPRINTF_S(path);
  1220. goto begin;
  1221. case SEL_LAST:
  1222. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1223. xstrlcpy(lastdir, path, sizeof(lastdir));
  1224. xstrlcpy(path, newpath, sizeof(path));
  1225. DPRINTF_S(path);
  1226. goto begin;
  1227. case SEL_TOGGLEDOT:
  1228. showhidden ^= 1;
  1229. initfilter(showhidden, &ifilter);
  1230. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1231. goto begin;
  1232. case SEL_DETAIL:
  1233. showdetail = !showdetail;
  1234. showdetail ? (printptr = &printent_long)
  1235. : (printptr = &printent);
  1236. /* Save current */
  1237. if (ndents > 0)
  1238. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1239. goto begin;
  1240. case SEL_STATS:
  1241. {
  1242. struct stat sb;
  1243. if (ndents > 0)
  1244. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1245. r = lstat(oldpath, &sb);
  1246. if (r == -1)
  1247. printerr(1, "lstat");
  1248. else
  1249. show_stats(oldpath, dents[cur].name, &sb);
  1250. goto begin;
  1251. }
  1252. case SEL_FSIZE:
  1253. sizeorder = !sizeorder;
  1254. mtimeorder = 0;
  1255. bsizeorder = 0;
  1256. /* Save current */
  1257. if (ndents > 0)
  1258. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1259. goto begin;
  1260. case SEL_BSIZE:
  1261. bsizeorder = !bsizeorder;
  1262. mtimeorder = 0;
  1263. sizeorder = 0;
  1264. /* Save current */
  1265. if (ndents > 0)
  1266. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1267. goto begin;
  1268. case SEL_MTIME:
  1269. mtimeorder = !mtimeorder;
  1270. sizeorder = 0;
  1271. bsizeorder = 0;
  1272. /* Save current */
  1273. if (ndents > 0)
  1274. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1275. goto begin;
  1276. case SEL_REDRAW:
  1277. /* Save current */
  1278. if (ndents > 0)
  1279. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1280. goto begin;
  1281. case SEL_COPY:
  1282. if (copier && ndents) {
  1283. char abspath[PATH_MAX];
  1284. if (strcmp(path, "/") == 0)
  1285. snprintf(abspath, PATH_MAX, "/%s",
  1286. dents[cur].name);
  1287. else
  1288. snprintf(abspath, PATH_MAX, "%s/%s",
  1289. path, dents[cur].name);
  1290. spawn(copier, abspath, NULL, 0);
  1291. printmsg(abspath);
  1292. } else if (!copier)
  1293. printmsg("NNN_COPIER is not set");
  1294. goto nochange;
  1295. case SEL_HELP:
  1296. show_help();
  1297. /* Save current */
  1298. if (ndents > 0)
  1299. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1300. goto begin;
  1301. case SEL_RUN:
  1302. run = xgetenv(env, run);
  1303. exitcurses();
  1304. spawn(run, NULL, path, 1);
  1305. initcurses();
  1306. /* Repopulate as directory content may have changed */
  1307. goto begin;
  1308. case SEL_RUNARG:
  1309. run = xgetenv(env, run);
  1310. exitcurses();
  1311. spawn(run, dents[cur].name, path, 0);
  1312. initcurses();
  1313. break;
  1314. }
  1315. /* Screensaver */
  1316. if (idletimeout != 0 && idle == idletimeout) {
  1317. idle = 0;
  1318. exitcurses();
  1319. spawn(idlecmd, NULL, NULL, 0);
  1320. initcurses();
  1321. }
  1322. }
  1323. }
  1324. static void
  1325. usage(void)
  1326. {
  1327. fprintf(stderr, "usage: nnn [-d] [dir]\n");
  1328. exit(1);
  1329. }
  1330. int
  1331. main(int argc, char *argv[])
  1332. {
  1333. char cwd[PATH_MAX], *ipath;
  1334. char *ifilter;
  1335. int opt = 0;
  1336. /* Confirm we are in a terminal */
  1337. if (!isatty(0) || !isatty(1)) {
  1338. fprintf(stderr, "stdin or stdout is not a tty\n");
  1339. exit(1);
  1340. }
  1341. if (argc > 3)
  1342. usage();
  1343. while ((opt = getopt(argc, argv, "d")) != -1) {
  1344. switch (opt) {
  1345. case 'd':
  1346. /* Open in detail mode, if set */
  1347. showdetail = 1;
  1348. printptr = &printent_long;
  1349. break;
  1350. default:
  1351. usage();
  1352. }
  1353. }
  1354. if (argc == optind) {
  1355. /* Start in the current directory */
  1356. ipath = getcwd(cwd, sizeof(cwd));
  1357. if (ipath == NULL)
  1358. ipath = "/";
  1359. } else {
  1360. ipath = realpath(argv[optind], cwd);
  1361. if (!ipath) {
  1362. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1363. exit(1);
  1364. }
  1365. }
  1366. if (getuid() == 0)
  1367. showhidden = 1;
  1368. initfilter(showhidden, &ifilter);
  1369. /* Get the default desktop mime opener, if set */
  1370. opener = getenv("NNN_OPENER");
  1371. /* Get the fallback desktop mime opener, if set */
  1372. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  1373. /* Get the default copier, if set */
  1374. copier = getenv("NNN_COPIER");
  1375. signal(SIGINT, SIG_IGN);
  1376. /* Test initial path */
  1377. if (canopendir(ipath) == 0) {
  1378. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  1379. exit(1);
  1380. }
  1381. /* Set locale before curses setup */
  1382. setlocale(LC_ALL, "");
  1383. initcurses();
  1384. browse(ipath, ifilter);
  1385. exitcurses();
  1386. exit(0);
  1387. }