My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

470 lines
7.7 KiB

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <dirent.h>
  6. #include <curses.h>
  7. #include <libgen.h>
  8. #include <locale.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #ifdef LINUX
  15. #include <bsd/string.h>
  16. #endif
  17. #ifdef DEBUG
  18. #define DEBUG_FD 8
  19. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  20. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  21. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  22. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  23. #else
  24. #define DPRINTF_D(x)
  25. #define DPRINTF_U(x)
  26. #define DPRINTF_S(x)
  27. #define DPRINTF_P(x)
  28. #endif /* DEBUG */
  29. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  30. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  31. #define ISODD(x) ((x) & 1)
  32. struct assoc {
  33. char *ext; /* Extension */
  34. char *bin; /* Program */
  35. };
  36. /* Configuration */
  37. struct assoc assocs[] = {
  38. { ".avi", "mplayer" },
  39. { ".mp4", "mplayer" },
  40. { ".mkv", "mplayer" },
  41. { ".mp3", "mplayer" },
  42. { ".ogg", "mplayer" },
  43. { ".srt", "less" },
  44. { ".txt", "less" },
  45. { ".sh", "sh" },
  46. { "README", "less" },
  47. };
  48. #define CWD "cwd: "
  49. #define CURSR " > "
  50. #define EMPTY " "
  51. /*
  52. * Layout:
  53. * .---------
  54. * | cwd: /mnt/path
  55. * |
  56. * | file0
  57. * | file1
  58. * | > file2
  59. * | file3
  60. * | file4
  61. * ...
  62. * | filen
  63. * |
  64. * | Permission denied
  65. * '------
  66. */
  67. int die = 0;
  68. char *
  69. openwith(char *file)
  70. {
  71. char *ext = NULL;
  72. char *bin = NULL;
  73. int i;
  74. ext = strrchr(file, '.');
  75. if (ext == NULL)
  76. ext = file;
  77. DPRINTF_S(ext);
  78. for (i = 0; i < LEN(assocs); i++)
  79. if (strcmp(assocs[i].ext, ext) == 0)
  80. bin = assocs[i].bin;
  81. DPRINTF_S(bin);
  82. return bin;
  83. }
  84. int
  85. dentcmp(const void *va, const void *vb)
  86. {
  87. const struct dirent *a, *b;
  88. a = (struct dirent *)va;
  89. b = (struct dirent *)vb;
  90. return strcmp(a->d_name, b->d_name);
  91. }
  92. void
  93. initcurses(void)
  94. {
  95. initscr();
  96. cbreak();
  97. noecho();
  98. nonl();
  99. intrflush(stdscr, FALSE);
  100. keypad(stdscr, TRUE);
  101. curs_set(FALSE); /* Hide cursor */
  102. }
  103. void
  104. exitcurses(void)
  105. {
  106. endwin(); /* Restore terminal */
  107. }
  108. /* Messages show up at the bottom */
  109. void
  110. printmsg(char *msg)
  111. {
  112. move(LINES - 1, 0);
  113. printw("%s\n", msg);
  114. }
  115. /* Display warning as a message */
  116. void
  117. printwarn(void)
  118. {
  119. printmsg(strerror(errno));
  120. }
  121. /* Kill curses and display error before exiting */
  122. void
  123. printerr(int ret, char *prefix)
  124. {
  125. exitcurses();
  126. printf("%s: %s\n", prefix, strerror(errno));
  127. exit(ret);
  128. }
  129. /*
  130. * Returns 0 normally
  131. * On movement it updates *cur
  132. * Returns 1 on quit
  133. * Returns 2 on go in
  134. * Returns 3 on go up
  135. */
  136. int
  137. nextsel(int *cur, int max)
  138. {
  139. int c;
  140. c = getch();
  141. switch (c) {
  142. case 'q':
  143. return 1;
  144. /* go up */
  145. case KEY_BACKSPACE:
  146. case KEY_LEFT:
  147. case 'h':
  148. return 2;
  149. /* go in */
  150. case KEY_ENTER:
  151. case '\r':
  152. case KEY_RIGHT:
  153. case 'l':
  154. return 3;
  155. /* next */
  156. case 'j':
  157. case KEY_DOWN:
  158. if (*cur < max - 1)
  159. (*cur)++;
  160. break;
  161. /* prev */
  162. case 'k':
  163. case KEY_UP:
  164. if (*cur > 0)
  165. (*cur)--;
  166. break;
  167. }
  168. return 0;
  169. }
  170. int
  171. testopen(char *path)
  172. {
  173. int fd;
  174. fd = open(path, O_RDONLY);
  175. if (fd == -1) {
  176. return 0;
  177. } else {
  178. close(fd);
  179. return 1;
  180. }
  181. }
  182. int
  183. testopendir(char *path)
  184. {
  185. DIR *dirp;
  186. dirp = opendir(path);
  187. if (dirp == NULL) {
  188. return 0;
  189. } else {
  190. closedir(dirp);
  191. return 1;
  192. }
  193. }
  194. void
  195. browse(const char *ipath)
  196. {
  197. DIR *dirp;
  198. struct dirent *dp;
  199. struct dirent *dents;
  200. int i, n, cur;
  201. int r, ret;
  202. char *path = strdup(ipath);
  203. char *cwd;
  204. begin:
  205. /* Path should be a malloc(3)-ed string at all times */
  206. n = 0;
  207. cur = 0;
  208. dents = NULL;
  209. dirp = opendir(path);
  210. if (dirp == NULL) {
  211. printwarn();
  212. goto nochange;
  213. }
  214. while ((dp = readdir(dirp)) != NULL) {
  215. /* Skip self and parent */
  216. if (strcmp(dp->d_name, ".") == 0
  217. || strcmp(dp->d_name, "..") == 0)
  218. continue;
  219. dents = realloc(dents, (n + 1) * sizeof(*dents));
  220. if (dents == NULL)
  221. printerr(1, "realloc");
  222. memcpy(&dents[n], dp, sizeof(*dents));
  223. n++;
  224. }
  225. qsort(dents, n, sizeof(*dents), dentcmp);
  226. for (;;) {
  227. int nlines;
  228. struct dirent *tmpents;
  229. int odd;
  230. redraw:
  231. nlines = MIN(LINES - 4, n);
  232. /* Clean screen */
  233. erase();
  234. /* Strip trailing slashes */
  235. for (i = strlen(path) - 1; i > -1; i--)
  236. if (path[i] == '/')
  237. path[i] = '\0';
  238. else
  239. break;
  240. DPRINTF_D(cur);
  241. DPRINTF_S(path);
  242. /* No text wrapping in cwd line */
  243. cwd = malloc(COLS * sizeof(char));
  244. strlcpy(cwd, path, COLS * sizeof(char));
  245. cwd[COLS - strlen(CWD) - 1] = '\0';
  246. /* No text wrapping in entries */
  247. tmpents = malloc(n * sizeof(*tmpents));
  248. memcpy(tmpents, dents, n * sizeof(*tmpents));
  249. for (i = 0; i < n; i++)
  250. tmpents[i].d_name[COLS - strlen(CURSR) - 1] = '\0';
  251. /* Print cwd. If empty we are on the root. We store it
  252. * as an empty string so that when we navigate in /mnt
  253. * is doesn't come up as //mnt. */
  254. printw(CWD "%s%s\n\n",
  255. strcmp(cwd, "") == 0 ? "/" : "",
  256. cwd);
  257. /* Print listing */
  258. odd = ISODD(nlines);
  259. if (cur < nlines / 2) {
  260. for (i = 0; i < nlines; i++)
  261. printw("%s%s\n",
  262. i == cur ? CURSR : EMPTY,
  263. tmpents[i].d_name);
  264. } else if (cur >= n - nlines / 2) {
  265. for (i = n - nlines; i < n; i++)
  266. printw("%s%s\n",
  267. i == cur ? CURSR : EMPTY,
  268. tmpents[i].d_name);
  269. } else {
  270. for (i = cur - nlines / 2;
  271. i < cur + nlines / 2 + odd; i++)
  272. printw("%s%s\n",
  273. i == cur ? CURSR : EMPTY,
  274. tmpents[i].d_name);
  275. }
  276. free(tmpents);
  277. nochange:
  278. ret = nextsel(&cur, n);
  279. if (ret == 1) {
  280. free(path);
  281. return;
  282. }
  283. if (ret == 2) {
  284. /* Handle root case */
  285. if (strcmp(path, "") == 0) {
  286. goto nochange;
  287. } else {
  288. char *dir, *tmp;
  289. dir = dirname(path);
  290. tmp = malloc(strlen(dir) + 1);
  291. strlcpy(tmp, dir, strlen(dir) + 1);
  292. free(path);
  293. path = tmp;
  294. goto out;
  295. }
  296. }
  297. if (ret == 3) {
  298. char *pathnew, *pathtmp;
  299. char *name;
  300. u_int8_t type;
  301. char *bin;
  302. pid_t pid;
  303. struct stat sb;
  304. /* Cannot descend in empty directories */
  305. if (n == 0)
  306. goto nochange;
  307. name = dents[cur].d_name;
  308. type = dents[cur].d_type;
  309. asprintf(&pathnew, "%s/%s", path, name);
  310. DPRINTF_S(name);
  311. DPRINTF_U(type);
  312. DPRINTF_S(pathnew);
  313. again:
  314. switch (type) {
  315. case DT_LNK:
  316. /* Resolve link */
  317. pathtmp = realpath(pathnew, NULL);
  318. if (pathtmp == NULL) {
  319. printwarn();
  320. free(pathnew);
  321. goto nochange;
  322. } else {
  323. r = stat(pathtmp, &sb);
  324. free(pathtmp);
  325. if (r == -1) {
  326. printwarn();
  327. free(pathnew);
  328. goto nochange;
  329. }
  330. /* Directory or file */
  331. if (S_ISDIR(sb.st_mode)) {
  332. type = DT_DIR;
  333. goto again;
  334. }
  335. if (S_ISREG(sb.st_mode)) {
  336. type = DT_REG;
  337. goto again;
  338. }
  339. /* All the rest */
  340. printmsg("Unsupported file");
  341. free(pathnew);
  342. goto nochange;
  343. }
  344. case DT_DIR:
  345. /* Change to new path */
  346. if (testopen(pathnew)) {
  347. free(path);
  348. path = pathnew;
  349. goto out;
  350. } else {
  351. printwarn();
  352. free(pathnew);
  353. goto nochange;
  354. }
  355. case DT_REG:
  356. if (!testopen(pathnew)) {
  357. printwarn();
  358. free(pathnew);
  359. goto nochange;
  360. }
  361. /* Open with */
  362. bin = openwith(name);
  363. if (bin == NULL) {
  364. printmsg("No association");
  365. free(pathnew);
  366. goto nochange;
  367. }
  368. exitcurses();
  369. /* Run program */
  370. pid = fork();
  371. if (pid == 0)
  372. execlp(bin, bin, pathnew, NULL);
  373. else
  374. waitpid(pid, NULL, 0);
  375. initcurses();
  376. free(pathnew);
  377. goto redraw;
  378. default:
  379. printmsg("Unsupported file");
  380. free(pathnew);
  381. goto nochange;
  382. }
  383. }
  384. }
  385. out:
  386. free(dents);
  387. r = closedir(dirp);
  388. if (r == -1)
  389. printerr(1, "closedir");
  390. goto begin;
  391. }
  392. int
  393. main(int argc, char *argv[])
  394. {
  395. char *ipath = argv[1] != NULL ? argv[1] : "/";
  396. /* Test initial path */
  397. if (!testopendir(ipath))
  398. printerr(1, ipath);
  399. /* Set locale before curses setup */
  400. setlocale(LC_ALL, "");
  401. initcurses();
  402. browse(ipath);
  403. exitcurses();
  404. return 0;
  405. }