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.
 
 
 
 
 
 

472 lines
7.8 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 DEBUG
  15. #define DEBUG_FD 8
  16. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  17. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  18. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  19. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  20. #else
  21. #define DPRINTF_D(x)
  22. #define DPRINTF_U(x)
  23. #define DPRINTF_S(x)
  24. #define DPRINTF_P(x)
  25. #endif /* DEBUG */
  26. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  27. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  28. #define ISODD(x) ((x) & 1)
  29. struct assoc {
  30. char *ext; /* Extension */
  31. char *bin; /* Program */
  32. };
  33. /* Configuration */
  34. struct assoc assocs[] = {
  35. { ".avi", "mplayer" },
  36. { ".mp4", "mplayer" },
  37. { ".mkv", "mplayer" },
  38. { ".mp3", "mplayer" },
  39. { ".ogg", "mplayer" },
  40. { ".srt", "less" },
  41. { ".txt", "less" },
  42. { ".sh", "sh" },
  43. { "README", "less" },
  44. };
  45. #define CWD "cwd: "
  46. #define CURSR " > "
  47. #define EMPTY " "
  48. /*
  49. * Layout:
  50. * .---------
  51. * | cwd: /mnt/path
  52. * |
  53. * | file0
  54. * | file1
  55. * | > file2
  56. * | file3
  57. * | file4
  58. * ...
  59. * | filen
  60. * |
  61. * | Permission denied
  62. * '------
  63. */
  64. int die = 0;
  65. struct entry {
  66. char name[MAXNAMLEN + 1];
  67. };
  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 (strncmp(assocs[i].ext, ext, strlen(ext) + 1) == 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 (strncmp(dp->d_name, ".", 2) == 0
  217. || strncmp(dp->d_name, "..", 3) == 0)
  218. continue;
  219. dents = realloc(dents, (n + 1) * sizeof(*dents));
  220. if (dents == NULL)
  221. printerr(1, "realloc");
  222. dents[n] = dp;
  223. n++;
  224. }
  225. qsort(dents, n, sizeof(*dents), dentcmp);
  226. for (;;) {
  227. int nlines;
  228. struct entry *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. strncpy(cwd, path, COLS);
  245. cwd[COLS - strlen(CWD) - 1] = '\0';
  246. /* No text wrapping in entries */
  247. tmpents = malloc(n * sizeof(*tmpents));
  248. for (i = 0; i < n; i++) {
  249. strncpy(tmpents[i].name, dents[i]->d_name,
  250. sizeof(tmpents[i].name));
  251. tmpents[i].name[COLS - strlen(CURSR) - 1] = '\0';
  252. }
  253. /* Print cwd. If empty we are on the root. We store it
  254. * as an empty string so that when we navigate in /mnt
  255. * is doesn't come up as //mnt. */
  256. printw(CWD "%s%s\n\n",
  257. strncmp(cwd, "", 1) == 0 ? "/" : "",
  258. cwd);
  259. /* Print listing */
  260. odd = ISODD(nlines);
  261. if (cur < nlines / 2) {
  262. for (i = 0; i < nlines; i++)
  263. printw("%s%s\n",
  264. i == cur ? CURSR : EMPTY,
  265. tmpents[i].name);
  266. } else if (cur >= n - nlines / 2) {
  267. for (i = n - nlines; i < n; i++)
  268. printw("%s%s\n",
  269. i == cur ? CURSR : EMPTY,
  270. tmpents[i].name);
  271. } else {
  272. for (i = cur - nlines / 2;
  273. i < cur + nlines / 2 + odd; i++)
  274. printw("%s%s\n",
  275. i == cur ? CURSR : EMPTY,
  276. tmpents[i].name);
  277. }
  278. free(tmpents);
  279. nochange:
  280. ret = nextsel(&cur, n);
  281. if (ret == 1) {
  282. free(path);
  283. return;
  284. }
  285. if (ret == 2) {
  286. /* Handle root case */
  287. if (strncmp(path, "", 1) == 0) {
  288. goto nochange;
  289. } else {
  290. char *dir, *tmp;
  291. dir = dirname(path);
  292. tmp = malloc(strlen(dir) + 1);
  293. strncpy(tmp, dir, strlen(dir) + 1);
  294. free(path);
  295. path = tmp;
  296. goto out;
  297. }
  298. }
  299. if (ret == 3) {
  300. char *pathnew, *pathtmp;
  301. char *name;
  302. u_int8_t type;
  303. char *bin;
  304. pid_t pid;
  305. struct stat sb;
  306. /* Cannot descend in empty directories */
  307. if (n == 0)
  308. goto nochange;
  309. name = dents[cur]->d_name;
  310. type = dents[cur]->d_type;
  311. pathnew = malloc(strlen(path) + 1
  312. + strlen(name) + 1);
  313. sprintf(pathnew, "%s/%s", path, name);
  314. DPRINTF_S(name);
  315. DPRINTF_U(type);
  316. DPRINTF_S(pathnew);
  317. again:
  318. switch (type) {
  319. case DT_LNK:
  320. /* Resolve link */
  321. pathtmp = realpath(pathnew, NULL);
  322. if (pathtmp == NULL) {
  323. printwarn();
  324. free(pathnew);
  325. goto nochange;
  326. } else {
  327. r = stat(pathtmp, &sb);
  328. free(pathtmp);
  329. if (r == -1) {
  330. printwarn();
  331. free(pathnew);
  332. goto nochange;
  333. }
  334. /* Directory or file */
  335. if (S_ISDIR(sb.st_mode)) {
  336. type = DT_DIR;
  337. goto again;
  338. }
  339. if (S_ISREG(sb.st_mode)) {
  340. type = DT_REG;
  341. goto again;
  342. }
  343. /* All the rest */
  344. printmsg("Unsupported file");
  345. free(pathnew);
  346. goto nochange;
  347. }
  348. case DT_DIR:
  349. /* Change to new path */
  350. if (testopen(pathnew)) {
  351. free(path);
  352. path = pathnew;
  353. goto out;
  354. } else {
  355. printwarn();
  356. free(pathnew);
  357. goto nochange;
  358. }
  359. case DT_REG:
  360. if (!testopen(pathnew)) {
  361. printwarn();
  362. free(pathnew);
  363. goto nochange;
  364. }
  365. /* Open with */
  366. bin = openwith(name);
  367. if (bin == NULL) {
  368. printmsg("No association");
  369. goto nochange;
  370. }
  371. exitcurses();
  372. /* Run program */
  373. pid = fork();
  374. if (pid == 0)
  375. execlp(bin, bin, pathnew, NULL);
  376. else
  377. waitpid(pid, NULL, 0);
  378. initcurses();
  379. free(pathnew);
  380. goto redraw;
  381. default:
  382. printmsg("Unsupported file");
  383. goto nochange;
  384. }
  385. }
  386. }
  387. out:
  388. free(dents);
  389. r = closedir(dirp);
  390. if (r == -1)
  391. printerr(1, "closedir");
  392. goto begin;
  393. }
  394. int
  395. main(int argc, char *argv[])
  396. {
  397. char *ipath = argv[1] != NULL ? argv[1] : "/";
  398. /* Test initial path */
  399. if (!testopendir(ipath))
  400. printerr(1, ipath);
  401. /* Set locale before curses setup */
  402. setlocale(LC_ALL, "");
  403. initcurses();
  404. browse(ipath);
  405. exitcurses();
  406. return 0;
  407. }