My build of nnn with minor changes
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

473 строки
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 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. size_t pathsiz;
  300. char *name;
  301. u_int8_t type;
  302. char *bin;
  303. pid_t pid;
  304. struct stat sb;
  305. /* Cannot descend in empty directories */
  306. if (n == 0)
  307. goto nochange;
  308. name = dents[cur].d_name;
  309. type = dents[cur].d_type;
  310. pathsiz = strlen(path) + 1 + strlen(name) + 1;
  311. pathnew = malloc(pathsiz);
  312. snprintf(pathnew, pathsiz, "%s/%s", path, name);
  313. DPRINTF_S(name);
  314. DPRINTF_U(type);
  315. DPRINTF_S(pathnew);
  316. again:
  317. switch (type) {
  318. case DT_LNK:
  319. /* Resolve link */
  320. pathtmp = realpath(pathnew, NULL);
  321. if (pathtmp == NULL) {
  322. printwarn();
  323. free(pathnew);
  324. goto nochange;
  325. } else {
  326. r = stat(pathtmp, &sb);
  327. free(pathtmp);
  328. if (r == -1) {
  329. printwarn();
  330. free(pathnew);
  331. goto nochange;
  332. }
  333. /* Directory or file */
  334. if (S_ISDIR(sb.st_mode)) {
  335. type = DT_DIR;
  336. goto again;
  337. }
  338. if (S_ISREG(sb.st_mode)) {
  339. type = DT_REG;
  340. goto again;
  341. }
  342. /* All the rest */
  343. printmsg("Unsupported file");
  344. free(pathnew);
  345. goto nochange;
  346. }
  347. case DT_DIR:
  348. /* Change to new path */
  349. if (testopen(pathnew)) {
  350. free(path);
  351. path = pathnew;
  352. goto out;
  353. } else {
  354. printwarn();
  355. free(pathnew);
  356. goto nochange;
  357. }
  358. case DT_REG:
  359. if (!testopen(pathnew)) {
  360. printwarn();
  361. free(pathnew);
  362. goto nochange;
  363. }
  364. /* Open with */
  365. bin = openwith(name);
  366. if (bin == NULL) {
  367. printmsg("No association");
  368. free(pathnew);
  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. free(pathnew);
  384. goto nochange;
  385. }
  386. }
  387. }
  388. out:
  389. free(dents);
  390. r = closedir(dirp);
  391. if (r == -1)
  392. printerr(1, "closedir");
  393. goto begin;
  394. }
  395. int
  396. main(int argc, char *argv[])
  397. {
  398. char *ipath = argv[1] != NULL ? argv[1] : "/";
  399. /* Test initial path */
  400. if (!testopendir(ipath))
  401. printerr(1, ipath);
  402. /* Set locale before curses setup */
  403. setlocale(LC_ALL, "");
  404. initcurses();
  405. browse(ipath);
  406. exitcurses();
  407. return 0;
  408. }