My build of nnn with minor changes
 
 
 
 
 
 

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