My build of nnn with minor changes
 
 
 
 
 
 

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