My build of nnn with minor changes
 
 
 
 
 
 

1682 lines
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. }
  413. static void
  414. exitcurses(void)
  415. {
  416. endwin(); /* Restore terminal */
  417. }
  418. /* Messages show up at the bottom */
  419. static void
  420. printmsg(char *msg)
  421. {
  422. move(LINES - 1, 0);
  423. printw("%s\n", msg);
  424. }
  425. /* Display warning as a message */
  426. static void
  427. printwarn(void)
  428. {
  429. printmsg(strerror(errno));
  430. }
  431. /* Kill curses and display error before exiting */
  432. static void
  433. printerr(int ret, char *prefix)
  434. {
  435. exitcurses();
  436. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  437. exit(ret);
  438. }
  439. /* Clear the last line */
  440. static void
  441. clearprompt(void)
  442. {
  443. printmsg("");
  444. }
  445. /* Print prompt on the last line */
  446. static void
  447. printprompt(char *str)
  448. {
  449. clearprompt();
  450. printw(str);
  451. }
  452. /* Returns SEL_* if key is bound and 0 otherwise.
  453. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  454. static int
  455. nextsel(char **run, char **env)
  456. {
  457. int c;
  458. unsigned int i;
  459. c = getch();
  460. if (c == -1)
  461. idle++;
  462. else
  463. idle = 0;
  464. for (i = 0; i < LEN(bindings); i++)
  465. if (c == bindings[i].sym) {
  466. *run = bindings[i].run;
  467. *env = bindings[i].env;
  468. return bindings[i].act;
  469. }
  470. return 0;
  471. }
  472. static char *
  473. readln(void)
  474. {
  475. static char ln[LINE_MAX];
  476. timeout(-1);
  477. echo();
  478. curs_set(TRUE);
  479. memset(ln, 0, sizeof(ln));
  480. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  481. noecho();
  482. curs_set(FALSE);
  483. timeout(1000);
  484. return ln[0] ? ln : NULL;
  485. }
  486. static int
  487. canopendir(char *path)
  488. {
  489. static DIR *dirp;
  490. dirp = opendir(path);
  491. if (dirp == NULL)
  492. return 0;
  493. closedir(dirp);
  494. return 1;
  495. }
  496. /*
  497. * Returns "dir/name or "/name"
  498. */
  499. static char *
  500. mkpath(char *dir, char *name, char *out, size_t n)
  501. {
  502. /* Handle absolute path */
  503. if (name[0] == '/')
  504. xstrlcpy(out, name, n);
  505. else {
  506. /* Handle root case */
  507. if (strcmp(dir, "/") == 0)
  508. snprintf(out, n, "/%s", name);
  509. else
  510. snprintf(out, n, "%s/%s", dir, name);
  511. }
  512. return out;
  513. }
  514. static void
  515. printent(struct entry *ent, int active)
  516. {
  517. if (S_ISDIR(ent->mode))
  518. printw("%s%s/\n", CURSYM(active), ent->name);
  519. else if (S_ISLNK(ent->mode))
  520. printw("%s%s@\n", CURSYM(active), ent->name);
  521. else if (S_ISSOCK(ent->mode))
  522. printw("%s%s=\n", CURSYM(active), ent->name);
  523. else if (S_ISFIFO(ent->mode))
  524. printw("%s%s|\n", CURSYM(active), ent->name);
  525. else if (ent->mode & S_IXUSR)
  526. printw("%s%s*\n", CURSYM(active), ent->name);
  527. else
  528. printw("%s%s\n", CURSYM(active), ent->name);
  529. }
  530. static void (*printptr)(struct entry *ent, int active) = &printent;
  531. static char*
  532. coolsize(off_t size)
  533. {
  534. static char size_buf[12]; /* Buffer to hold human readable size */
  535. static int i;
  536. static off_t fsize, tmp;
  537. static long double rem;
  538. i = 0;
  539. fsize = size;
  540. rem = 0;
  541. while (fsize > 1024) {
  542. tmp = fsize;
  543. //fsize *= div_2_pow_10;
  544. fsize >>= 10;
  545. rem = tmp - (fsize << 10);
  546. i++;
  547. }
  548. snprintf(size_buf, 12, "%.*Lf%s", i, fsize + rem * div_2_pow_10, size_units[i]);
  549. return size_buf;
  550. }
  551. static void
  552. printent_long(struct entry *ent, int active)
  553. {
  554. static char buf[18];
  555. strftime(buf, 18, "%d %m %Y %H:%M", localtime(&ent->t));
  556. if (active)
  557. attron(A_REVERSE);
  558. if (!bsizeorder) {
  559. if (S_ISDIR(ent->mode))
  560. printw("%s%-16.16s / %s/\n",
  561. CURSYM(active), buf, ent->name);
  562. else if (S_ISLNK(ent->mode))
  563. printw("%s%-16.16s @ %s@\n",
  564. CURSYM(active), buf, ent->name);
  565. else if (S_ISSOCK(ent->mode))
  566. printw("%s%-16.16s = %s=\n",
  567. CURSYM(active), buf, ent->name);
  568. else if (S_ISFIFO(ent->mode))
  569. printw("%s%-16.16s | %s|\n",
  570. CURSYM(active), buf, ent->name);
  571. else if (S_ISBLK(ent->mode))
  572. printw("%s%-16.16s b %s\n",
  573. CURSYM(active), buf, ent->name);
  574. else if (S_ISCHR(ent->mode))
  575. printw("%s%-16.16s c %s\n",
  576. CURSYM(active), buf, ent->name);
  577. else if (ent->mode & S_IXUSR)
  578. printw("%s%-16.16s %8.8s* %s*\n", CURSYM(active),
  579. buf, coolsize(ent->size), ent->name);
  580. else
  581. printw("%s%-16.16s %8.8s %s\n", CURSYM(active),
  582. buf, coolsize(ent->size), ent->name);
  583. } else {
  584. if (S_ISDIR(ent->mode))
  585. printw("%s%-16.16s %8.8s/ %s/\n", CURSYM(active),
  586. buf, coolsize(ent->bsize << 9), ent->name);
  587. else if (S_ISLNK(ent->mode))
  588. printw("%s%-16.16s @ %s@\n",
  589. CURSYM(active), buf, ent->name);
  590. else if (S_ISSOCK(ent->mode))
  591. printw("%s%-16.16s = %s=\n",
  592. CURSYM(active), buf, ent->name);
  593. else if (S_ISFIFO(ent->mode))
  594. printw("%s%-16.16s | %s|\n",
  595. CURSYM(active), buf, ent->name);
  596. else if (S_ISBLK(ent->mode))
  597. printw("%s%-16.16s b %s\n",
  598. CURSYM(active), buf, ent->name);
  599. else if (S_ISCHR(ent->mode))
  600. printw("%s%-16.16s c %s\n",
  601. CURSYM(active), buf, ent->name);
  602. else if (ent->mode & S_IXUSR)
  603. printw("%s%-16.16s %8.8s* %s*\n", CURSYM(active),
  604. buf, coolsize(ent->bsize << 9), ent->name);
  605. else
  606. printw("%s%-16.16s %8.8s %s\n", CURSYM(active),
  607. buf, coolsize(ent->bsize << 9), ent->name);
  608. }
  609. if (active)
  610. attroff(A_REVERSE);
  611. }
  612. static char
  613. get_fileind(mode_t mode, char *desc)
  614. {
  615. static char c;
  616. if (S_ISREG(mode)) {
  617. c = '-';
  618. sprintf(desc, "%s", "regular file");
  619. if (mode & S_IXUSR)
  620. strcat(desc, ", executable");
  621. } else if (S_ISDIR(mode)) {
  622. c = 'd';
  623. sprintf(desc, "%s", "directory");
  624. } else if (S_ISBLK(mode)) {
  625. c = 'b';
  626. sprintf(desc, "%s", "block special device");
  627. } else if (S_ISCHR(mode)) {
  628. c = 'c';
  629. sprintf(desc, "%s", "character special device");
  630. #ifdef S_ISFIFO
  631. } else if (S_ISFIFO(mode)) {
  632. c = 'p';
  633. sprintf(desc, "%s", "FIFO");
  634. #endif /* S_ISFIFO */
  635. #ifdef S_ISLNK
  636. } else if (S_ISLNK(mode)) {
  637. c = 'l';
  638. sprintf(desc, "%s", "symbolic link");
  639. #endif /* S_ISLNK */
  640. #ifdef S_ISSOCK
  641. } else if (S_ISSOCK(mode)) {
  642. c = 's';
  643. sprintf(desc, "%s", "socket");
  644. #endif /* S_ISSOCK */
  645. #ifdef S_ISDOOR
  646. /* Solaris 2.6, etc. */
  647. } else if (S_ISDOOR(mode)) {
  648. c = 'D';
  649. desc[0] = '\0';
  650. #endif /* S_ISDOOR */
  651. } else {
  652. /* Unknown type -- possibly a regular file? */
  653. c = '?';
  654. desc[0] = '\0';
  655. }
  656. return(c);
  657. }
  658. /* Convert a mode field into "ls -l" type perms field. */
  659. static char *
  660. get_lsperms(mode_t mode, char *desc)
  661. {
  662. static const char *rwx[] = {"---", "--x", "-w-", "-wx",
  663. "r--", "r-x", "rw-", "rwx"};
  664. static char bits[11];
  665. bits[0] = get_fileind(mode, desc);
  666. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  667. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  668. strcpy(&bits[7], rwx[(mode & 7)]);
  669. if (mode & S_ISUID)
  670. bits[3] = (mode & S_IXUSR) ? 's' : 'S';
  671. if (mode & S_ISGID)
  672. bits[6] = (mode & S_IXGRP) ? 's' : 'l';
  673. if (mode & S_ISVTX)
  674. bits[9] = (mode & S_IXOTH) ? 't' : 'T';
  675. bits[10] = '\0';
  676. return(bits);
  677. }
  678. static char *
  679. get_output(char *buf, size_t bytes)
  680. {
  681. char *ret;
  682. FILE *pf = popen(buf, "r");
  683. if (pf) {
  684. ret = fgets(buf, bytes, pf);
  685. pclose(pf);
  686. return ret;
  687. }
  688. return NULL;
  689. }
  690. /*
  691. * Follows the stat(1) output closely
  692. */
  693. static void
  694. show_stats(char* fpath, char* fname, struct stat *sb)
  695. {
  696. char buf[PATH_MAX + 48];
  697. char *perms = get_lsperms(sb->st_mode, buf);
  698. char *p, *begin = buf;
  699. clear();
  700. /* Show file name or 'symlink' -> 'target' */
  701. if (perms[0] == 'l') {
  702. char symtgt[PATH_MAX];
  703. ssize_t len = readlink(fpath, symtgt, PATH_MAX);
  704. if (len != -1) {
  705. symtgt[len] = '\0';
  706. printw("\n\n File: '%s' -> '%s'", fname, symtgt);
  707. }
  708. } else
  709. printw("\n File: '%s'", fname);
  710. /* Show size, blocks, file type */
  711. printw("\n Size: %-15llu Blocks: %-10llu IO Block: %-6llu %s",
  712. sb->st_size, sb->st_blocks, sb->st_blksize, buf);
  713. /* Show containing device, inode, hardlink count */
  714. sprintf(buf, "%lxh/%lud", (ulong)sb->st_dev, (ulong)sb->st_dev);
  715. printw("\n Device: %-15s Inode: %-11lu Links: %-9lu",
  716. buf, sb->st_ino, sb->st_nlink);
  717. /* Show major, minor number for block or char device */
  718. if (perms[0] == 'b' || perms[0] == 'c')
  719. printw(" Device type: %lx,%lx",
  720. major(sb->st_rdev), minor(sb->st_rdev));
  721. /* Show permissions, owner, group */
  722. printw("\n Access: 0%d%d%d/%s Uid: (%lu/%s) Gid: (%lu/%s)",
  723. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  724. perms,
  725. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  726. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  727. /* Show last access time */
  728. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  729. printw("\n\n Access: %s", buf);
  730. /* Show last modification time */
  731. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  732. printw("\n Modify: %s", buf);
  733. /* Show last status change time */
  734. strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  735. printw("\n Change: %s", buf);
  736. if (S_ISREG(sb->st_mode)) {
  737. /* Show file(1) output */
  738. sprintf(buf, "file -b \"%s\" 2>&1", fpath);
  739. p = get_output(buf, PATH_MAX + 48);
  740. if (p) {
  741. printw("\n\n ");
  742. while (*p) {
  743. if (*p == ',') {
  744. *p = '\0';
  745. printw(" %s\n", begin);
  746. begin = p + 1;
  747. }
  748. p++;
  749. }
  750. printw(" %s", begin);
  751. }
  752. #ifdef SUPPORT_CHKSUM
  753. /* Calculating checksums can take VERY long */
  754. /* Show md5 */
  755. sprintf(buf, "openssl md5 \"%s\" 2>&1", fpath);
  756. p = get_output(buf, PATH_MAX + 48);
  757. if (p) {
  758. p = xmemrchr(buf, ' ', strlen(buf));
  759. if (!p)
  760. p = buf;
  761. else
  762. p++;
  763. printw("\n md5: %s", p);
  764. }
  765. /* Show sha256 */
  766. sprintf(buf, "openssl sha256 \"%s\" 2>&1", fpath);
  767. p = get_output(buf, PATH_MAX + 48);
  768. if (p) {
  769. p = xmemrchr(buf, ' ', strlen(buf));
  770. if (!p)
  771. p = buf;
  772. else
  773. p++;
  774. printw(" sha256: %s", p);
  775. }
  776. #endif
  777. }
  778. /* Show exit keys */
  779. printw("\n\n << (D/q)");
  780. while ((*buf = getch()))
  781. if (*buf == 'D' || *buf == 'q')
  782. break;
  783. return;
  784. }
  785. static void
  786. show_help(void)
  787. {
  788. char c;
  789. clear();
  790. printw("\n\
  791. << Key >> << Function >>\n\n\
  792. [Up], k, ^P Previous entry\n\
  793. [Down], j, ^N Next entry\n\
  794. [PgUp], ^U Scroll half page up\n\
  795. [PgDn], ^D Scroll half page down\n\
  796. [Home], g, ^, ^A Jump to first entry\n\
  797. [End], G, $, ^E Jump to last entry\n\
  798. [Right], [Enter], l, ^M Open file or enter dir\n\
  799. [Left], [Backspace], h, ^H Go to parent dir\n\
  800. ~ Jump to HOME dir\n\
  801. - Jump to last visited dir\n\
  802. o Open dir in desktop file manager\n\
  803. /, & Filter dir contents\n\
  804. c Show change dir prompt\n\
  805. d Toggle detail view\n\
  806. D Toggle current file details screen\n\
  807. . Toggle hide .dot files\n\
  808. s Toggle sort by file size\n\
  809. S Toggle disk usage analyzer mode\n\
  810. t Toggle sort by modified time\n\
  811. ! Spawn SHELL in PWD (fallback sh)\n\
  812. z Run top\n\
  813. e Edit entry in EDITOR (fallback vi)\n\
  814. p Open entry in PAGER (fallback less)\n\
  815. ^K Invoke file name copier\n\
  816. ^L Force a redraw\n\
  817. ? Toggle help screen\n\
  818. q Quit\n");
  819. /* Show exit keys */
  820. printw("\n\n << (?/q)");
  821. while ((c = getch()))
  822. if (c == '?' || c == 'q')
  823. break;
  824. return;
  825. }
  826. static int
  827. sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  828. {
  829. /* Handle permission problems */
  830. if(typeflag == FTW_NS) {
  831. printmsg("No stats (permissions ?)");
  832. return 0;
  833. }
  834. blk_size += sb->st_blocks;
  835. return 0;
  836. }
  837. static int
  838. getorder(size_t size)
  839. {
  840. switch (size) {
  841. case 4096:
  842. return 12;
  843. case 512:
  844. return 9;
  845. case 8192:
  846. return 13;
  847. case 16384:
  848. return 14;
  849. case 32768:
  850. return 15;
  851. case 65536:
  852. return 16;
  853. case 131072:
  854. return 17;
  855. case 262144:
  856. return 18;
  857. case 524288:
  858. return 19;
  859. case 1048576:
  860. return 20;
  861. case 2048:
  862. return 11;
  863. case 1024:
  864. return 10;
  865. default:
  866. return 0;
  867. }
  868. }
  869. static int
  870. dentfill(char *path, struct entry **dents,
  871. int (*filter)(regex_t *, char *), regex_t *re)
  872. {
  873. static char newpath[PATH_MAX];
  874. static DIR *dirp;
  875. static struct dirent *dp;
  876. static struct stat sb;
  877. static struct statvfs svb;
  878. static int r, n;
  879. r = n = 0;
  880. dirp = opendir(path);
  881. if (dirp == NULL)
  882. return 0;
  883. while ((dp = readdir(dirp)) != NULL) {
  884. /* Skip self and parent */
  885. if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
  886. (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
  887. continue;
  888. if (filter(re, dp->d_name) == 0)
  889. continue;
  890. if (((n >> 5) << 5) == n) {
  891. *dents = realloc(*dents, (n + 32) * sizeof(**dents));
  892. if (*dents == NULL)
  893. printerr(1, "realloc");
  894. }
  895. xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  896. /* Get mode flags */
  897. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  898. r = lstat(newpath, &sb);
  899. if (r == -1)
  900. printerr(1, "lstat");
  901. (*dents)[n].mode = sb.st_mode;
  902. (*dents)[n].t = sb.st_mtime;
  903. (*dents)[n].size = sb.st_size;
  904. if (bsizeorder) {
  905. if (S_ISDIR(sb.st_mode)) {
  906. blk_size = 0;
  907. if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) {
  908. printmsg("nftw(3) failed");
  909. (*dents)[n].bsize = sb.st_blocks;
  910. } else
  911. (*dents)[n].bsize = blk_size;
  912. } else
  913. (*dents)[n].bsize = sb.st_blocks;
  914. }
  915. n++;
  916. }
  917. if (bsizeorder) {
  918. r = statvfs(path, &svb);
  919. if (r == -1)
  920. fs_free = 0;
  921. else
  922. fs_free = svb.f_bavail << getorder(svb.f_bsize);
  923. }
  924. /* Should never be null */
  925. r = closedir(dirp);
  926. if (r == -1)
  927. printerr(1, "closedir");
  928. return n;
  929. }
  930. static void
  931. dentfree(struct entry *dents)
  932. {
  933. free(dents);
  934. }
  935. /* Return the position of the matching entry or 0 otherwise */
  936. static int
  937. dentfind(struct entry *dents, int n, char *path)
  938. {
  939. if (!path)
  940. return 0;
  941. static int i;
  942. static char *p;
  943. p = xmemrchr(path, '/', strlen(path));
  944. if (!p)
  945. p = path;
  946. else
  947. /* We are assuming an entry with actual
  948. name ending in '/' will not appear */
  949. p++;
  950. DPRINTF_S(p);
  951. for (i = 0; i < n; i++)
  952. if (strcmp(p, dents[i].name) == 0)
  953. return i;
  954. return 0;
  955. }
  956. static int
  957. populate(char *path, char *oldpath, char *fltr)
  958. {
  959. static regex_t re;
  960. static int r;
  961. /* Can fail when permissions change while browsing */
  962. if (canopendir(path) == 0)
  963. return -1;
  964. /* Search filter */
  965. r = setfilter(&re, fltr);
  966. if (r != 0)
  967. return -1;
  968. dentfree(dents);
  969. ndents = 0;
  970. dents = NULL;
  971. ndents = dentfill(path, &dents, visible, &re);
  972. qsort(dents, ndents, sizeof(*dents), entrycmp);
  973. /* Find cur from history */
  974. cur = dentfind(dents, ndents, oldpath);
  975. return 0;
  976. }
  977. static void
  978. redraw(char *path)
  979. {
  980. static char cwd[PATH_MAX];
  981. static int nlines, odd;
  982. static int i;
  983. nlines = MIN(LINES - 4, ndents);
  984. /* Clean screen */
  985. erase();
  986. /* Strip trailing slashes */
  987. for (i = strlen(path) - 1; i > 0; i--)
  988. if (path[i] == '/')
  989. path[i] = '\0';
  990. else
  991. break;
  992. DPRINTF_D(cur);
  993. DPRINTF_S(path);
  994. /* No text wrapping in cwd line */
  995. if (!realpath(path, cwd)) {
  996. printmsg("Cannot resolve path");
  997. return;
  998. }
  999. printw(CWD "%s\n\n", cwd);
  1000. /* Print listing */
  1001. odd = ISODD(nlines);
  1002. if (cur < (nlines >> 1)) {
  1003. for (i = 0; i < nlines; i++)
  1004. printptr(&dents[i], i == cur);
  1005. } else if (cur >= ndents - (nlines >> 1)) {
  1006. for (i = ndents - nlines; i < ndents; i++)
  1007. printptr(&dents[i], i == cur);
  1008. } else {
  1009. nlines >>= 1;
  1010. for (i = cur - nlines; i < cur + nlines + odd; i++)
  1011. printptr(&dents[i], i == cur);
  1012. }
  1013. if (showdetail) {
  1014. if (ndents) {
  1015. static char ind[2] = "\0\0";
  1016. static char sort[17];
  1017. if (mtimeorder)
  1018. sprintf(sort, "by time ");
  1019. else if (sizeorder)
  1020. sprintf(sort, "by size ");
  1021. else
  1022. sort[0] = '\0';
  1023. if (S_ISDIR(dents[cur].mode))
  1024. ind[0] = '/';
  1025. else if (S_ISLNK(dents[cur].mode))
  1026. ind[0] = '@';
  1027. else if (S_ISSOCK(dents[cur].mode))
  1028. ind[0] = '=';
  1029. else if (S_ISFIFO(dents[cur].mode))
  1030. ind[0] = '|';
  1031. else if (dents[cur].mode & S_IXUSR)
  1032. ind[0] = '*';
  1033. else
  1034. ind[0] = '\0';
  1035. if (!bsizeorder)
  1036. sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
  1037. dents[cur].name, ind);
  1038. else
  1039. sprintf(cwd, "total %d by disk usage, %s free [%s%s]",
  1040. ndents, coolsize(fs_free), dents[cur].name, ind);
  1041. printmsg(cwd);
  1042. } else
  1043. printmsg("0 items");
  1044. }
  1045. }
  1046. static void
  1047. browse(char *ipath, char *ifilter)
  1048. {
  1049. static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  1050. static char lastdir[PATH_MAX];
  1051. static char fltr[LINE_MAX];
  1052. char *bin, *dir, *tmp, *run, *env;
  1053. struct stat sb;
  1054. regex_t re;
  1055. int r, fd;
  1056. enum action sel = SEL_RUNARG + 1;
  1057. xstrlcpy(path, ipath, sizeof(path));
  1058. xstrlcpy(lastdir, ipath, sizeof(lastdir));
  1059. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1060. oldpath[0] = '\0';
  1061. newpath[0] = '\0';
  1062. begin:
  1063. if (sel == SEL_GOIN && S_ISDIR(sb.st_mode))
  1064. r = populate(path, NULL, fltr);
  1065. else
  1066. r = populate(path, oldpath, fltr);
  1067. if (r == -1) {
  1068. printwarn();
  1069. goto nochange;
  1070. }
  1071. for (;;) {
  1072. redraw(path);
  1073. nochange:
  1074. sel = nextsel(&run, &env);
  1075. switch (sel) {
  1076. case SEL_QUIT:
  1077. dentfree(dents);
  1078. return;
  1079. case SEL_BACK:
  1080. /* There is no going back */
  1081. if (strcmp(path, "/") == 0 ||
  1082. strcmp(path, ".") == 0 ||
  1083. strchr(path, '/') == NULL) {
  1084. printmsg("You are at /");
  1085. goto nochange;
  1086. }
  1087. dir = xdirname(path);
  1088. if (canopendir(dir) == 0) {
  1089. printwarn();
  1090. goto nochange;
  1091. }
  1092. /* Save history */
  1093. xstrlcpy(oldpath, path, sizeof(oldpath));
  1094. /* Save last working directory */
  1095. xstrlcpy(lastdir, path, sizeof(lastdir));
  1096. xstrlcpy(path, dir, sizeof(path));
  1097. /* Reset filter */
  1098. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1099. goto begin;
  1100. case SEL_GOIN:
  1101. /* Cannot descend in empty directories */
  1102. if (ndents == 0)
  1103. goto nochange;
  1104. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  1105. DPRINTF_S(newpath);
  1106. /* Get path info */
  1107. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1108. if (fd == -1) {
  1109. printwarn();
  1110. goto nochange;
  1111. }
  1112. r = fstat(fd, &sb);
  1113. if (r == -1) {
  1114. printwarn();
  1115. close(fd);
  1116. goto nochange;
  1117. }
  1118. close(fd);
  1119. DPRINTF_U(sb.st_mode);
  1120. switch (sb.st_mode & S_IFMT) {
  1121. case S_IFDIR:
  1122. if (canopendir(newpath) == 0) {
  1123. printwarn();
  1124. goto nochange;
  1125. }
  1126. /* Save last working directory */
  1127. xstrlcpy(lastdir, path, sizeof(lastdir));
  1128. xstrlcpy(path, newpath, sizeof(path));
  1129. /* Reset filter */
  1130. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1131. goto begin;
  1132. case S_IFREG:
  1133. {
  1134. static char cmd[MAX_CMD_LEN];
  1135. static char *runvi = "vi";
  1136. static int status;
  1137. /* If default mime opener is set, use it */
  1138. if (opener) {
  1139. snprintf(cmd, MAX_CMD_LEN,
  1140. "%s \"%s\" > /dev/null 2>&1",
  1141. opener, newpath);
  1142. status = system(cmd);
  1143. continue;
  1144. }
  1145. /* Try custom applications */
  1146. bin = openwith(newpath);
  1147. /* If custom app doesn't exist try fallback */
  1148. snprintf(cmd, MAX_CMD_LEN, "which \"%s\"", bin);
  1149. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1150. bin = NULL;
  1151. if (bin == NULL) {
  1152. /* If a custom handler application is
  1153. not set, open plain text files with
  1154. vi, then try fallback_opener */
  1155. snprintf(cmd, MAX_CMD_LEN,
  1156. "file \"%s\"", newpath);
  1157. if (get_output(cmd, MAX_CMD_LEN) == NULL)
  1158. goto nochange;
  1159. if (strstr(cmd, "ASCII text") != NULL)
  1160. bin = runvi;
  1161. else if (fallback_opener) {
  1162. snprintf(cmd, MAX_CMD_LEN,
  1163. "%s \"%s\" > \
  1164. /dev/null 2>&1",
  1165. fallback_opener,
  1166. newpath);
  1167. status = system(cmd);
  1168. continue;
  1169. } else {
  1170. status++; /* Dummy operation */
  1171. printmsg("No association");
  1172. goto nochange;
  1173. }
  1174. }
  1175. exitcurses();
  1176. spawn(bin, newpath, NULL, 0);
  1177. initcurses();
  1178. continue;
  1179. }
  1180. default:
  1181. printmsg("Unsupported file");
  1182. goto nochange;
  1183. }
  1184. case SEL_FLTR:
  1185. /* Read filter */
  1186. printprompt("filter: ");
  1187. tmp = readln();
  1188. if (tmp == NULL)
  1189. tmp = ifilter;
  1190. /* Check and report regex errors */
  1191. r = setfilter(&re, tmp);
  1192. if (r != 0)
  1193. goto nochange;
  1194. xstrlcpy(fltr, tmp, sizeof(fltr));
  1195. DPRINTF_S(fltr);
  1196. /* Save current */
  1197. if (ndents > 0)
  1198. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1199. goto begin;
  1200. case SEL_NEXT:
  1201. if (cur < ndents - 1)
  1202. cur++;
  1203. else if (ndents)
  1204. /* Roll over, set cursor to first entry */
  1205. cur = 0;
  1206. break;
  1207. case SEL_PREV:
  1208. if (cur > 0)
  1209. cur--;
  1210. else if (ndents)
  1211. /* Roll over, set cursor to last entry */
  1212. cur = ndents - 1;
  1213. break;
  1214. case SEL_PGDN:
  1215. if (cur < ndents - 1)
  1216. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1217. break;
  1218. case SEL_PGUP:
  1219. if (cur > 0)
  1220. cur -= MIN((LINES - 4) / 2, cur);
  1221. break;
  1222. case SEL_HOME:
  1223. cur = 0;
  1224. break;
  1225. case SEL_END:
  1226. cur = ndents - 1;
  1227. break;
  1228. case SEL_CD:
  1229. /* Read target dir */
  1230. printprompt("chdir: ");
  1231. tmp = readln();
  1232. if (tmp == NULL) {
  1233. clearprompt();
  1234. goto nochange;
  1235. }
  1236. if (tmp[0] == '~') {
  1237. char *home = getenv("HOME");
  1238. if (home)
  1239. snprintf(newpath, PATH_MAX,
  1240. "%s%s", home, tmp + 1);
  1241. else
  1242. mkpath(path, tmp, newpath, sizeof(newpath));
  1243. } else if (tmp[0] == '-' && tmp[1] == '\0')
  1244. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1245. else
  1246. mkpath(path, tmp, newpath, sizeof(newpath));
  1247. if (canopendir(newpath) == 0) {
  1248. printwarn();
  1249. goto nochange;
  1250. }
  1251. /* Save last working directory */
  1252. xstrlcpy(lastdir, path, sizeof(lastdir));
  1253. xstrlcpy(path, newpath, sizeof(path));
  1254. /* Reset filter */
  1255. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1256. DPRINTF_S(path);
  1257. goto begin;
  1258. case SEL_CDHOME:
  1259. tmp = getenv("HOME");
  1260. if (tmp == NULL) {
  1261. clearprompt();
  1262. goto nochange;
  1263. }
  1264. if (canopendir(tmp) == 0) {
  1265. printwarn();
  1266. goto nochange;
  1267. }
  1268. /* Save last working directory */
  1269. xstrlcpy(lastdir, path, sizeof(lastdir));
  1270. xstrlcpy(path, tmp, sizeof(path));
  1271. /* Reset filter */
  1272. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1273. DPRINTF_S(path);
  1274. goto begin;
  1275. case SEL_LAST:
  1276. xstrlcpy(newpath, lastdir, sizeof(newpath));
  1277. xstrlcpy(lastdir, path, sizeof(lastdir));
  1278. xstrlcpy(path, newpath, sizeof(path));
  1279. DPRINTF_S(path);
  1280. goto begin;
  1281. case SEL_TOGGLEDOT:
  1282. showhidden ^= 1;
  1283. initfilter(showhidden, &ifilter);
  1284. xstrlcpy(fltr, ifilter, sizeof(fltr));
  1285. goto begin;
  1286. case SEL_DETAIL:
  1287. showdetail = !showdetail;
  1288. showdetail ? (printptr = &printent_long)
  1289. : (printptr = &printent);
  1290. /* Save current */
  1291. if (ndents > 0)
  1292. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1293. goto begin;
  1294. case SEL_STATS:
  1295. {
  1296. struct stat sb;
  1297. if (ndents > 0)
  1298. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1299. r = lstat(oldpath, &sb);
  1300. if (r == -1)
  1301. printerr(1, "lstat");
  1302. else
  1303. show_stats(oldpath, dents[cur].name, &sb);
  1304. goto begin;
  1305. }
  1306. case SEL_DFB:
  1307. if (!desktop_manager)
  1308. goto nochange;
  1309. exitcurses();
  1310. spawn(desktop_manager, path, path, 0);
  1311. initcurses();
  1312. goto nochange;
  1313. case SEL_FSIZE:
  1314. sizeorder = !sizeorder;
  1315. mtimeorder = 0;
  1316. bsizeorder = 0;
  1317. /* Save current */
  1318. if (ndents > 0)
  1319. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1320. goto begin;
  1321. case SEL_BSIZE:
  1322. bsizeorder = !bsizeorder;
  1323. if (bsizeorder) {
  1324. showdetail = 1;
  1325. printptr = &printent_long;
  1326. }
  1327. mtimeorder = 0;
  1328. sizeorder = 0;
  1329. /* Save current */
  1330. if (ndents > 0)
  1331. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1332. goto begin;
  1333. case SEL_MTIME:
  1334. mtimeorder = !mtimeorder;
  1335. sizeorder = 0;
  1336. bsizeorder = 0;
  1337. /* Save current */
  1338. if (ndents > 0)
  1339. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1340. goto begin;
  1341. case SEL_REDRAW:
  1342. /* Save current */
  1343. if (ndents > 0)
  1344. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1345. goto begin;
  1346. case SEL_COPY:
  1347. if (copier && ndents) {
  1348. char abspath[PATH_MAX];
  1349. if (strcmp(path, "/") == 0)
  1350. snprintf(abspath, PATH_MAX, "/%s",
  1351. dents[cur].name);
  1352. else
  1353. snprintf(abspath, PATH_MAX, "%s/%s",
  1354. path, dents[cur].name);
  1355. spawn(copier, abspath, NULL, 0);
  1356. printmsg(abspath);
  1357. } else if (!copier)
  1358. printmsg("NNN_COPIER is not set");
  1359. goto nochange;
  1360. case SEL_HELP:
  1361. show_help();
  1362. /* Save current */
  1363. if (ndents > 0)
  1364. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  1365. goto begin;
  1366. case SEL_RUN:
  1367. run = xgetenv(env, run);
  1368. exitcurses();
  1369. spawn(run, NULL, path, 1);
  1370. initcurses();
  1371. /* Repopulate as directory content may have changed */
  1372. goto begin;
  1373. case SEL_RUNARG:
  1374. run = xgetenv(env, run);
  1375. exitcurses();
  1376. spawn(run, dents[cur].name, path, 0);
  1377. initcurses();
  1378. break;
  1379. }
  1380. /* Screensaver */
  1381. if (idletimeout != 0 && idle == idletimeout) {
  1382. idle = 0;
  1383. exitcurses();
  1384. spawn(idlecmd, NULL, NULL, 0);
  1385. initcurses();
  1386. }
  1387. }
  1388. }
  1389. static void
  1390. usage(void)
  1391. {
  1392. fprintf(stdout, "usage: nnn [-d] [-S] [-v] [h] [PATH]\n\n\
  1393. The missing terminal file browser for X.\n\n\
  1394. positional arguments:\n\
  1395. PATH directory to open [default: current dir]\n\n\
  1396. optional arguments:\n\
  1397. -d start in detail view mode\n\
  1398. -S start in disk usage analyzer mode\n\
  1399. -v show program version and exit\n\
  1400. -h show this help and exit\n\n\
  1401. Version: %s\n\
  1402. License: BSD 2-Clause\n\
  1403. Webpage: https://github.com/jarun/nnn\n", VERSION);
  1404. exit(0);
  1405. }
  1406. int
  1407. main(int argc, char *argv[])
  1408. {
  1409. char cwd[PATH_MAX], *ipath;
  1410. char *ifilter;
  1411. int opt = 0;
  1412. /* Confirm we are in a terminal */
  1413. if (!isatty(0) || !isatty(1)) {
  1414. fprintf(stderr, "stdin or stdout is not a tty\n");
  1415. exit(1);
  1416. }
  1417. if (argc > 3)
  1418. usage();
  1419. while ((opt = getopt(argc, argv, "dSvh")) != -1) {
  1420. switch (opt) {
  1421. case 'S':
  1422. bsizeorder = 1;
  1423. case 'd':
  1424. /* Open in detail mode, if set */
  1425. showdetail = 1;
  1426. printptr = &printent_long;
  1427. break;
  1428. case 'v':
  1429. fprintf(stdout, "%s\n", VERSION);
  1430. return 0;
  1431. case 'h':
  1432. default:
  1433. usage();
  1434. }
  1435. }
  1436. if (argc == optind) {
  1437. /* Start in the current directory */
  1438. ipath = getcwd(cwd, sizeof(cwd));
  1439. if (ipath == NULL)
  1440. ipath = "/";
  1441. } else {
  1442. ipath = realpath(argv[optind], cwd);
  1443. if (!ipath) {
  1444. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  1445. exit(1);
  1446. }
  1447. }
  1448. open_max = max_openfds();
  1449. if (getuid() == 0)
  1450. showhidden = 1;
  1451. initfilter(showhidden, &ifilter);
  1452. /* Get the default desktop mime opener, if set */
  1453. opener = getenv("NNN_OPENER");
  1454. /* Get the fallback desktop mime opener, if set */
  1455. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  1456. /* Get the desktop file browser, if set */
  1457. desktop_manager = getenv("NNN_DE_FILE_MANAGER");
  1458. /* Get the default copier, if set */
  1459. copier = getenv("NNN_COPIER");
  1460. signal(SIGINT, SIG_IGN);
  1461. /* Test initial path */
  1462. if (canopendir(ipath) == 0) {
  1463. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  1464. exit(1);
  1465. }
  1466. /* Set locale */
  1467. setlocale(LC_ALL, "");
  1468. initcurses();
  1469. browse(ipath, ifilter);
  1470. exitcurses();
  1471. exit(0);
  1472. }