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.

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. testopendir(char *path)
  172. {
  173. DIR *dirp;
  174. dirp = opendir(path);
  175. if (dirp == NULL) {
  176. return 0;
  177. } else {
  178. closedir(dirp);
  179. return 1;
  180. }
  181. }
  182. void
  183. browse(const char *ipath)
  184. {
  185. DIR *dirp;
  186. struct dirent *dp;
  187. struct dirent *dents;
  188. int i, n, cur;
  189. int r, ret;
  190. char *path = strdup(ipath);
  191. char *cwd;
  192. begin:
  193. /* Path should be a malloc(3)-ed string at all times */
  194. n = 0;
  195. cur = 0;
  196. dents = NULL;
  197. dirp = opendir(path);
  198. if (dirp == NULL) {
  199. printwarn();
  200. goto nochange;
  201. }
  202. while ((dp = readdir(dirp)) != NULL) {
  203. /* Skip self and parent */
  204. if (strcmp(dp->d_name, ".") == 0
  205. || strcmp(dp->d_name, "..") == 0)
  206. continue;
  207. dents = realloc(dents, (n + 1) * sizeof(*dents));
  208. if (dents == NULL)
  209. printerr(1, "realloc");
  210. memcpy(&dents[n], dp, sizeof(*dents));
  211. n++;
  212. }
  213. qsort(dents, n, sizeof(*dents), dentcmp);
  214. for (;;) {
  215. int nlines;
  216. struct dirent *tmpents;
  217. int odd;
  218. redraw:
  219. nlines = MIN(LINES - 4, n);
  220. /* Clean screen */
  221. erase();
  222. /* Strip trailing slashes */
  223. for (i = strlen(path) - 1; i > -1; i--)
  224. if (path[i] == '/')
  225. path[i] = '\0';
  226. else
  227. break;
  228. DPRINTF_D(cur);
  229. DPRINTF_S(path);
  230. /* No text wrapping in cwd line */
  231. cwd = malloc(COLS * sizeof(char));
  232. strlcpy(cwd, path, COLS * sizeof(char));
  233. cwd[COLS - strlen(CWD) - 1] = '\0';
  234. /* No text wrapping in entries */
  235. tmpents = malloc(n * sizeof(*tmpents));
  236. memcpy(tmpents, dents, n * sizeof(*tmpents));
  237. for (i = 0; i < n; i++)
  238. tmpents[i].d_name[COLS - strlen(CURSR) - 1] = '\0';
  239. /* Print cwd. If empty we are on the root. We store it
  240. * as an empty string so that when we navigate in /mnt
  241. * is doesn't come up as //mnt. */
  242. printw(CWD "%s%s\n\n",
  243. strcmp(cwd, "") == 0 ? "/" : "",
  244. cwd);
  245. /* Print listing */
  246. odd = ISODD(nlines);
  247. if (cur < nlines / 2) {
  248. for (i = 0; i < nlines; i++)
  249. printw("%s%s\n",
  250. i == cur ? CURSR : EMPTY,
  251. tmpents[i].d_name);
  252. } else if (cur >= n - nlines / 2) {
  253. for (i = n - nlines; i < n; i++)
  254. printw("%s%s\n",
  255. i == cur ? CURSR : EMPTY,
  256. tmpents[i].d_name);
  257. } else {
  258. for (i = cur - nlines / 2;
  259. i < cur + nlines / 2 + odd; i++)
  260. printw("%s%s\n",
  261. i == cur ? CURSR : EMPTY,
  262. tmpents[i].d_name);
  263. }
  264. free(tmpents);
  265. nochange:
  266. ret = nextsel(&cur, n);
  267. if (ret == 1) {
  268. free(path);
  269. return;
  270. }
  271. if (ret == 2) {
  272. /* Handle root case */
  273. if (strcmp(path, "") == 0) {
  274. goto nochange;
  275. } else {
  276. char *dir, *tmp;
  277. dir = dirname(path);
  278. tmp = malloc(strlen(dir) + 1);
  279. strlcpy(tmp, dir, strlen(dir) + 1);
  280. free(path);
  281. path = tmp;
  282. goto out;
  283. }
  284. }
  285. if (ret == 3) {
  286. char *pathnew;
  287. char *name;
  288. char *bin;
  289. pid_t pid;
  290. int fd;
  291. struct stat sb;
  292. /* Cannot descend in empty directories */
  293. if (n == 0)
  294. goto nochange;
  295. name = dents[cur].d_name;
  296. asprintf(&pathnew, "%s/%s", path, name);
  297. DPRINTF_S(name);
  298. DPRINTF_S(pathnew);
  299. /* Get path info */
  300. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  301. if (fd == -1) {
  302. printwarn();
  303. free(pathnew);
  304. goto nochange;
  305. }
  306. r = fstat(fd, &sb);
  307. close(fd);
  308. if (r == -1) {
  309. printwarn();
  310. free(pathnew);
  311. goto nochange;
  312. }
  313. DPRINTF_U(sb.st_mode);
  314. /* Directory */
  315. if (S_ISDIR(sb.st_mode)) {
  316. free(path);
  317. path = pathnew;
  318. goto out;
  319. }
  320. /* Regular file */
  321. if (S_ISREG(sb.st_mode)) {
  322. /* Open with */
  323. bin = openwith(name);
  324. if (bin == NULL) {
  325. printmsg("No association");
  326. free(pathnew);
  327. goto nochange;
  328. }
  329. exitcurses();
  330. /* Run program */
  331. pid = fork();
  332. if (pid == 0)
  333. execlp(bin, bin, pathnew, NULL);
  334. else
  335. waitpid(pid, NULL, 0);
  336. initcurses();
  337. free(pathnew);
  338. goto redraw;
  339. }
  340. /* All the rest */
  341. printmsg("Unsupported file");
  342. free(pathnew);
  343. goto nochange;
  344. }
  345. }
  346. out:
  347. free(dents);
  348. r = closedir(dirp);
  349. if (r == -1)
  350. printerr(1, "closedir");
  351. goto begin;
  352. }
  353. int
  354. main(int argc, char *argv[])
  355. {
  356. char *ipath = argv[1] != NULL ? argv[1] : "/";
  357. /* Test initial path */
  358. if (!testopendir(ipath))
  359. printerr(1, ipath);
  360. /* Set locale before curses setup */
  361. setlocale(LC_ALL, "");
  362. initcurses();
  363. browse(ipath);
  364. exitcurses();
  365. return 0;
  366. }