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.
 
 
 
 
 
 

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