My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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