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.

nnn.c 50 KiB

il y a 9 ans
il y a 10 ans
il y a 9 ans
il y a 9 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 9 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 10 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 10 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 9 ans
il y a 7 ans
il y a 7 ans
il y a 9 ans
il y a 7 ans
il y a 9 ans
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
  4. || defined(__APPLE__)
  5. # include <sys/types.h>
  6. #else
  7. # include <sys/sysmacros.h>
  8. #endif
  9. #include <sys/wait.h>
  10. #include <sys/statvfs.h>
  11. #include <sys/resource.h>
  12. #include <ctype.h>
  13. #include <curses.h>
  14. #include <dirent.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <grp.h>
  18. #include <libgen.h>
  19. #include <limits.h>
  20. #ifdef __gnu_hurd__
  21. #define PATH_MAX 4096
  22. #endif
  23. #include <locale.h>
  24. #include <pwd.h>
  25. #include <regex.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <wchar.h>
  34. #include <readline/readline.h>
  35. #ifndef __USE_XOPEN_EXTENDED
  36. #define __USE_XOPEN_EXTENDED 1
  37. #endif
  38. #include <ftw.h>
  39. #include "config.h"
  40. #ifdef DEBUGMODE
  41. static int DEBUG_FD;
  42. static int
  43. xprintf(int fd, const char *fmt, ...)
  44. {
  45. char buf[BUFSIZ];
  46. int r;
  47. va_list ap;
  48. va_start(ap, fmt);
  49. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  50. if (r > 0)
  51. r = write(fd, buf, r);
  52. va_end(ap);
  53. return r;
  54. }
  55. static int
  56. enabledbg()
  57. {
  58. FILE *fp = fopen("/tmp/nnn_debug", "w");
  59. if (!fp) {
  60. fprintf(stderr, "Cannot open debug file\n");
  61. return -1;
  62. }
  63. DEBUG_FD = fileno(fp);
  64. if (DEBUG_FD == -1) {
  65. fprintf(stderr, "Cannot open debug file descriptor\n");
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. static void
  71. disabledbg()
  72. {
  73. close(DEBUG_FD);
  74. }
  75. #define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
  76. #define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x)
  77. #define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x)
  78. #define DPRINTF_P(x) xprintf(DEBUG_FD, #x "=0x%p\n", x)
  79. #else
  80. #define DPRINTF_D(x)
  81. #define DPRINTF_U(x)
  82. #define DPRINTF_S(x)
  83. #define DPRINTF_P(x)
  84. #endif /* DEBUGMODE */
  85. /* Macro definitions */
  86. #define VERSION "v1.1"
  87. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  88. #undef MIN
  89. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  90. #define ISODD(x) ((x) & 1)
  91. #define TOUPPER(ch) \
  92. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  93. #define MAX_CMD_LEN 5120
  94. #define CURSYM(flag) (flag ? CURSR : EMPTY)
  95. #define FILTER '/'
  96. #define MAX_BM 10
  97. /* Macros to define process spawn behaviour as flags */
  98. #define SP_NONE 0x00 /* no flag set */
  99. #define SP_MARKER 0x01 /* draw marker to indicate nnn spawned (e.g. shell) */
  100. #define SP_NOWAIT 0x02 /* don't wait for child process (e.g. file manager) */
  101. #define SP_NOTRACE 0x04 /* suppress stdout and strerr (no traces) */
  102. #define SP_SIGINT 0x08 /* restore default SIGINT handler */
  103. #define SP_NORMAL 0x80 /* spawn child process in non-curses regular mode */
  104. typedef unsigned long ulong;
  105. typedef unsigned int uint;
  106. typedef unsigned char uchar;
  107. /* Directory entry */
  108. typedef struct entry {
  109. char name[NAME_MAX];
  110. mode_t mode;
  111. time_t t;
  112. off_t size;
  113. blkcnt_t blocks; /* number of 512B blocks allocated */
  114. } *pEntry;
  115. /* Bookmark */
  116. typedef struct {
  117. char *key;
  118. char *loc;
  119. } bm;
  120. /* Settings */
  121. typedef struct
  122. {
  123. uchar filtermode : 1; /* Set to enter filter mode */
  124. uchar mtimeorder : 1; /* Set to sort by time modified */
  125. uchar sizeorder : 1; /* Set to sort by file size */
  126. uchar blkorder : 1; /* Set to sort by blocks used (disk usage) */
  127. uchar showhidden : 1; /* Set to show hidden files */
  128. uchar showdetail : 1; /* Clear to show fewer file info */
  129. uchar reserved : 2;
  130. } settings;
  131. /* Externs */
  132. #ifdef __APPLE__
  133. extern int add_history(const char *string);
  134. #else
  135. extern void add_history(const char *string);
  136. #endif
  137. extern int wget_wch(WINDOW *win, wint_t *wch);
  138. /* Globals */
  139. static settings cfg = {0, 0, 0, 0, 0, 1, 0};
  140. /* Idle timeout in seconds, 0 to disable */
  141. static int idletimeout;
  142. static struct entry *dents;
  143. static int ndents, cur, total_dents;
  144. static int idle;
  145. static char *player;
  146. static char *copier;
  147. static char *editor;
  148. static char *desktop_manager;
  149. static blkcnt_t ent_blocks;
  150. static blkcnt_t dir_blocks;
  151. static ulong num_files;
  152. static size_t fs_free;
  153. static uint open_max;
  154. static bm bookmark[MAX_BM];
  155. static const double div_2_pow_10 = 1.0 / 1024.0;
  156. /* Utilities to open files, run actions */
  157. static char *utils[] = {
  158. #ifdef __APPLE__
  159. "/usr/bin/open",
  160. #else
  161. "/usr/bin/xdg-open",
  162. #endif
  163. "nlay"
  164. };
  165. /* For use in functions which are isolated and don't return the buffer */
  166. static char g_buf[MAX_CMD_LEN];
  167. /*
  168. * Layout:
  169. * .---------
  170. * | cwd: /mnt/path
  171. * |
  172. * | file0
  173. * | file1
  174. * | > file2
  175. * | file3
  176. * | file4
  177. * ...
  178. * | filen
  179. * |
  180. * | Permission denied
  181. * '------
  182. */
  183. /* Forward declarations */
  184. static void printmsg(char *);
  185. static void printwarn(void);
  186. static void printerr(int, char *);
  187. static void redraw(char *path);
  188. /* Functions */
  189. /* Increase the limit on open file descriptors, if possible */
  190. static rlim_t
  191. max_openfds()
  192. {
  193. struct rlimit rl;
  194. rlim_t limit = getrlimit(RLIMIT_NOFILE, &rl);
  195. if (limit != 0)
  196. return 32;
  197. limit = rl.rlim_cur;
  198. rl.rlim_cur = rl.rlim_max;
  199. if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
  200. return rl.rlim_max - 64;
  201. if (limit > 128)
  202. return limit - 64;
  203. return 32;
  204. }
  205. /* Just a safe strncpy(3) */
  206. static void
  207. xstrlcpy(char *dest, const char *src, size_t n)
  208. {
  209. while (--n && (*dest = *src))
  210. ++dest, ++src;
  211. if (!n)
  212. *dest = '\0';
  213. }
  214. /*
  215. * The poor man's implementation of memrchr(3).
  216. * We are only looking for '/' in this program.
  217. */
  218. static void *
  219. xmemrchr(const void *s, uchar ch, size_t n)
  220. {
  221. if (!s || !n)
  222. return NULL;
  223. static uchar *p;
  224. p = (uchar *)s + n - 1;
  225. while (n) {
  226. if (*p == ch)
  227. return p;
  228. --p;
  229. --n;
  230. }
  231. return NULL;
  232. }
  233. /*
  234. * The following dirname(3) implementation does not
  235. * modify the input. We use a copy of the original.
  236. *
  237. * Modified from the glibc (GNU LGPL) version.
  238. */
  239. static char *
  240. xdirname(const char *path)
  241. {
  242. static char buf[PATH_MAX];
  243. static char *last_slash;
  244. xstrlcpy(buf, path, PATH_MAX);
  245. /* Find last '/'. */
  246. last_slash = strrchr(buf, '/');
  247. if (last_slash != NULL && last_slash != buf && last_slash[1] == '\0') {
  248. /* Determine whether all remaining characters are slashes. */
  249. char *runp;
  250. for (runp = last_slash; runp != buf; --runp)
  251. if (runp[-1] != '/')
  252. break;
  253. /* The '/' is the last character, we have to look further. */
  254. if (runp != buf)
  255. last_slash = xmemrchr(buf, '/', runp - buf);
  256. }
  257. if (last_slash != NULL) {
  258. /* Determine whether all remaining characters are slashes. */
  259. char *runp;
  260. for (runp = last_slash; runp != buf; --runp)
  261. if (runp[-1] != '/')
  262. break;
  263. /* Terminate the buffer. */
  264. if (runp == buf) {
  265. /* The last slash is the first character in the string.
  266. * We have to return "/". As a special case we have to
  267. * return "//" if there are exactly two slashes at the
  268. * beginning of the string. See XBD 4.10 Path Name
  269. * Resolution for more information.
  270. */
  271. if (last_slash == buf + 1)
  272. ++last_slash;
  273. else
  274. last_slash = buf + 1;
  275. } else
  276. last_slash = runp;
  277. last_slash[0] = '\0';
  278. } else {
  279. /* This assignment is ill-designed but the XPG specs require to
  280. * return a string containing "." in any case no directory part
  281. * is found and so a static and constant string is required.
  282. */
  283. buf[0] = '.';
  284. buf[1] = '\0';
  285. }
  286. return buf;
  287. }
  288. /*
  289. * Return number of dots if all chars in a string are dots, else 0
  290. */
  291. static int
  292. all_dots(const char *ptr)
  293. {
  294. if (!ptr)
  295. return FALSE;
  296. int count = 0;
  297. while (*ptr == '.')
  298. ++count, ++ptr;
  299. if (*ptr)
  300. return 0;
  301. return count;
  302. }
  303. /* Initialize curses mode */
  304. static void
  305. initcurses(void)
  306. {
  307. if (initscr() == NULL) {
  308. char *term = getenv("TERM");
  309. if (term != NULL)
  310. fprintf(stderr, "error opening terminal: %s\n", term);
  311. else
  312. fprintf(stderr, "failed to initialize curses\n");
  313. exit(1);
  314. }
  315. cbreak();
  316. noecho();
  317. nonl();
  318. intrflush(stdscr, FALSE);
  319. keypad(stdscr, TRUE);
  320. curs_set(FALSE); /* Hide cursor */
  321. start_color();
  322. use_default_colors();
  323. timeout(1000); /* One second */
  324. }
  325. /* Exit curses mode */
  326. static void
  327. exitcurses(void)
  328. {
  329. endwin(); /* Restore terminal */
  330. }
  331. /*
  332. * Spawns a child process. Behaviour can be controlled using flag.
  333. * Limited to 2 arguments to a program, flag works on bit set.
  334. */
  335. static void
  336. spawn(char *file, char *arg1, char *arg2, char *dir, uchar flag)
  337. {
  338. pid_t pid;
  339. int status;
  340. if (flag & SP_NORMAL)
  341. exitcurses();
  342. pid = fork();
  343. if (pid == 0) {
  344. if (dir != NULL)
  345. status = chdir(dir);
  346. /* Show a marker (to indicate nnn spawned shell) */
  347. if (flag & SP_MARKER) {
  348. printf("\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
  349. printf("Spawned shell level: %d\n", atoi(getenv("SHLVL")) + 1);
  350. }
  351. /* Suppress stdout and stderr */
  352. if (flag & SP_NOTRACE) {
  353. int fd = open("/dev/null", O_WRONLY, 0200);
  354. dup2(fd, 1);
  355. dup2(fd, 2);
  356. close(fd);
  357. }
  358. if (flag & SP_SIGINT)
  359. signal(SIGINT, SIG_DFL);
  360. execlp(file, file, arg1, arg2, NULL);
  361. _exit(1);
  362. } else {
  363. if (!(flag & SP_NOWAIT))
  364. /* Ignore interruptions */
  365. while (waitpid(pid, &status, 0) == -1)
  366. DPRINTF_D(status);
  367. DPRINTF_D(pid);
  368. if (flag & SP_NORMAL)
  369. initcurses();
  370. }
  371. }
  372. /* Get program name from env var, else return fallback program */
  373. static char *
  374. xgetenv(char *name, char *fallback)
  375. {
  376. if (name == NULL)
  377. return fallback;
  378. char *value = getenv(name);
  379. return value && value[0] ? value : fallback;
  380. }
  381. /*
  382. * We assume none of the strings are NULL.
  383. *
  384. * Let's have the logic to sort numeric names in numeric order.
  385. * E.g., the order '1, 10, 2' doesn't make sense to human eyes.
  386. *
  387. * If the absolute numeric values are same, we fallback to alphasort.
  388. */
  389. static int
  390. xstricmp(char *s1, char *s2)
  391. {
  392. static char *c1, *c2;
  393. c1 = s1;
  394. while (isspace(*c1))
  395. ++c1;
  396. if (*c1 == '-' || *c1 == '+')
  397. ++c1;
  398. while (*c1 >= '0' && *c1 <= '9')
  399. ++c1;
  400. c2 = s2;
  401. while (isspace(*c2))
  402. ++c2;
  403. if (*c2 == '-' || *c2 == '+')
  404. ++c2;
  405. while (*c2 >= '0' && *c2 <= '9')
  406. ++c2;
  407. if (*c1 == '\0' && *c2 == '\0') {
  408. static long long num1, num2;
  409. num1 = strtoll(s1, &c1, 10);
  410. num2 = strtoll(s2, &c2, 10);
  411. if (num1 != num2) {
  412. if (num1 > num2)
  413. return 1;
  414. else
  415. return -1;
  416. }
  417. } else if (*c1 == '\0' && *c2 != '\0')
  418. return -1;
  419. else if (*c1 != '\0' && *c2 == '\0')
  420. return 1;
  421. while (*s2 && *s1 && TOUPPER(*s1) == TOUPPER(*s2))
  422. ++s1, ++s2;
  423. /* In case of alphabetically same names, make sure
  424. * lower case one comes before upper case one
  425. */
  426. if (!*s1 && !*s2)
  427. return 1;
  428. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  429. }
  430. /* Trim all whitespace from both ends, / from end */
  431. static char *
  432. strstrip(char *s)
  433. {
  434. if (!s || !*s)
  435. return s;
  436. size_t len = strlen(s) - 1;
  437. while (len != 0 && (isspace(s[len]) || s[len] == '/'))
  438. --len;
  439. s[len + 1] = '\0';
  440. while (*s && isspace(*s))
  441. ++s;
  442. return s;
  443. }
  444. static char *
  445. getmime(char *file)
  446. {
  447. regex_t regex;
  448. uint i;
  449. static uint len = LEN(assocs);
  450. for (i = 0; i < len; ++i) {
  451. if (regcomp(&regex, assocs[i].regex,
  452. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  453. continue;
  454. if (regexec(&regex, file, 0, NULL, 0) == 0)
  455. return assocs[i].mime;
  456. }
  457. return NULL;
  458. }
  459. static int
  460. setfilter(regex_t *regex, char *filter)
  461. {
  462. static size_t len;
  463. static int r;
  464. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  465. if (r != 0 && filter && filter[0] != '\0') {
  466. len = COLS;
  467. if (len > LINE_MAX)
  468. len = LINE_MAX;
  469. regerror(r, regex, g_buf, len);
  470. printmsg(g_buf);
  471. }
  472. return r;
  473. }
  474. static void
  475. initfilter(int dot, char **ifilter)
  476. {
  477. *ifilter = dot ? "." : "^[^.]";
  478. }
  479. static int
  480. visible(regex_t *regex, char *file)
  481. {
  482. return regexec(regex, file, 0, NULL, 0) == 0;
  483. }
  484. static int
  485. entrycmp(const void *va, const void *vb)
  486. {
  487. static pEntry pa, pb;
  488. pa = (pEntry)va;
  489. pb = (pEntry)vb;
  490. /* Sort directories first */
  491. if (S_ISDIR(pb->mode) && !S_ISDIR(pa->mode))
  492. return 1;
  493. else if (S_ISDIR(pa->mode) && !S_ISDIR(pb->mode))
  494. return -1;
  495. /* Do the actual sorting */
  496. if (cfg.mtimeorder)
  497. return pb->t - pa->t;
  498. if (cfg.sizeorder) {
  499. if (pb->size > pa->size)
  500. return 1;
  501. else if (pb->size < pa->size)
  502. return -1;
  503. }
  504. if (cfg.blkorder) {
  505. if (pb->blocks > pa->blocks)
  506. return 1;
  507. else if (pb->blocks < pa->blocks)
  508. return -1;
  509. }
  510. return xstricmp(pa->name, pb->name);
  511. }
  512. /* Messages show up at the bottom */
  513. static void
  514. printmsg(char *msg)
  515. {
  516. mvprintw(LINES - 1, 0, "%s\n", msg);
  517. }
  518. /* Display warning as a message */
  519. static void
  520. printwarn(void)
  521. {
  522. printmsg(strerror(errno));
  523. }
  524. /* Kill curses and display error before exiting */
  525. static void
  526. printerr(int ret, char *prefix)
  527. {
  528. exitcurses();
  529. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  530. exit(ret);
  531. }
  532. /* Clear the last line */
  533. static void
  534. clearprompt(void)
  535. {
  536. printmsg("");
  537. }
  538. /* Print prompt on the last line */
  539. static void
  540. printprompt(char *str)
  541. {
  542. clearprompt();
  543. printw(str);
  544. }
  545. /* Returns SEL_* if key is bound and 0 otherwise.
  546. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}).
  547. * The next keyboard input can be simulated by presel.
  548. */
  549. static int
  550. nextsel(char **run, char **env, int *presel)
  551. {
  552. static int c;
  553. static uchar i;
  554. static uint len = LEN(bindings);
  555. c = *presel;
  556. if (c == 0)
  557. c = getch();
  558. else
  559. *presel = 0;
  560. if (c == -1)
  561. ++idle;
  562. else
  563. idle = 0;
  564. for (i = 0; i < len; ++i)
  565. if (c == bindings[i].sym) {
  566. *run = bindings[i].run;
  567. *env = bindings[i].env;
  568. return bindings[i].act;
  569. }
  570. return 0;
  571. }
  572. /*
  573. * Move non-matching entries to the end
  574. */
  575. static void
  576. fill(struct entry **dents,
  577. int (*filter)(regex_t *, char *), regex_t *re)
  578. {
  579. static int count;
  580. for (count = 0; count < ndents; ++count) {
  581. if (filter(re, (*dents)[count].name) == 0) {
  582. if (count != --ndents) {
  583. static struct entry _dent;
  584. /* Copy count to tmp */
  585. xstrlcpy(_dent.name, (*dents)[count].name,
  586. NAME_MAX);
  587. _dent.mode = (*dents)[count].mode;
  588. _dent.t = (*dents)[count].t;
  589. _dent.size = (*dents)[count].size;
  590. _dent.blocks = (*dents)[count].blocks;
  591. /* Copy ndents - 1 to count */
  592. xstrlcpy((*dents)[count].name,
  593. (*dents)[ndents].name, NAME_MAX);
  594. (*dents)[count].mode = (*dents)[ndents].mode;
  595. (*dents)[count].t = (*dents)[ndents].t;
  596. (*dents)[count].size = (*dents)[ndents].size;
  597. (*dents)[count].blocks = (*dents)[ndents].blocks;
  598. /* Copy tmp to ndents - 1 */
  599. xstrlcpy((*dents)[ndents].name, _dent.name,
  600. NAME_MAX);
  601. (*dents)[ndents].mode = _dent.mode;
  602. (*dents)[ndents].t = _dent.t;
  603. (*dents)[ndents].size = _dent.size;
  604. (*dents)[ndents].blocks = _dent.blocks;
  605. --count;
  606. }
  607. continue;
  608. }
  609. }
  610. }
  611. static int
  612. matches(char *fltr)
  613. {
  614. static regex_t re;
  615. /* Search filter */
  616. if (setfilter(&re, fltr) != 0)
  617. return -1;
  618. fill(&dents, visible, &re);
  619. qsort(dents, ndents, sizeof(*dents), entrycmp);
  620. return 0;
  621. }
  622. static int
  623. readln(char *path)
  624. {
  625. static char ln[LINE_MAX << 2];
  626. static wchar_t wln[LINE_MAX];
  627. static wint_t ch[2] = {0};
  628. int r, total = ndents;
  629. int oldcur = cur;
  630. int len = 1;
  631. char *pln = ln + 1;
  632. memset(wln, 0, LINE_MAX << 2);
  633. wln[0] = FILTER;
  634. ln[0] = FILTER;
  635. ln[1] = '\0';
  636. cur = 0;
  637. timeout(-1);
  638. echo();
  639. curs_set(TRUE);
  640. printprompt(ln);
  641. while ((r = wget_wch(stdscr, ch)) != ERR) {
  642. if (r == OK) {
  643. switch (*ch) {
  644. case '\r': // with nonl(), this is ENTER key value
  645. if (len == 1) {
  646. cur = oldcur;
  647. goto end;
  648. }
  649. if (matches(pln) == -1)
  650. goto end;
  651. redraw(path);
  652. goto end;
  653. case 127: // handle DEL
  654. if (len == 1) {
  655. cur = oldcur;
  656. *ch = CONTROL('L');
  657. goto end;
  658. }
  659. if (len == 2)
  660. cur = oldcur;
  661. wln[--len] = '\0';
  662. wcstombs(ln, wln, LINE_MAX << 2);
  663. ndents = total;
  664. if (matches(pln) == -1) {
  665. printprompt(ln);
  666. continue;
  667. }
  668. redraw(path);
  669. printprompt(ln);
  670. break;
  671. case CONTROL('L'):
  672. if (len == 1)
  673. cur = oldcur; // fallthrough
  674. case CONTROL('Q'):
  675. goto end;
  676. default:
  677. wln[len] = (wchar_t)*ch;
  678. ++len;
  679. wln[len] = '\0';
  680. wcstombs(ln, wln, LINE_MAX << 2);
  681. ndents = total;
  682. if (matches(pln) == -1)
  683. continue;
  684. redraw(path);
  685. printprompt(ln);
  686. }
  687. } else {
  688. switch (*ch) {
  689. case KEY_DC: // fallthrough
  690. case KEY_BACKSPACE:
  691. if (len == 1) {
  692. cur = oldcur;
  693. *ch = CONTROL('L');
  694. goto end;
  695. }
  696. if (len == 2)
  697. cur = oldcur;
  698. wln[--len] = '\0';
  699. wcstombs(ln, wln, LINE_MAX << 2);
  700. ndents = total;
  701. if (matches(pln) == -1)
  702. continue;
  703. redraw(path);
  704. printprompt(ln);
  705. break;
  706. case KEY_DOWN: // fallthrough
  707. case KEY_UP: // fallthrough
  708. case KEY_LEFT: // fallthrough
  709. case KEY_RIGHT: // fallthrough
  710. case KEY_F(2):
  711. if (len == 1)
  712. cur = oldcur; // fallthrough
  713. default:
  714. goto end;
  715. }
  716. }
  717. }
  718. end:
  719. noecho();
  720. curs_set(FALSE);
  721. timeout(1000);
  722. /* Return keys for navigation etc. */
  723. return *ch;
  724. }
  725. static int
  726. canopendir(char *path)
  727. {
  728. static DIR *dirp;
  729. dirp = opendir(path);
  730. if (dirp == NULL)
  731. return 0;
  732. closedir(dirp);
  733. return 1;
  734. }
  735. /*
  736. * Returns "dir/name or "/name"
  737. */
  738. static char *
  739. mkpath(char *dir, char *name, char *out, size_t n)
  740. {
  741. /* Handle absolute path */
  742. if (name[0] == '/')
  743. xstrlcpy(out, name, n);
  744. else {
  745. /* Handle root case */
  746. if (strcmp(dir, "/") == 0)
  747. snprintf(out, n, "/%s", name);
  748. else
  749. snprintf(out, n, "%s/%s", dir, name);
  750. }
  751. return out;
  752. }
  753. static void
  754. parsebmstr(char *bms)
  755. {
  756. int i = 0;
  757. while (*bms && i < MAX_BM) {
  758. bookmark[i].key = bms;
  759. ++bms;
  760. while (*bms && *bms != ':')
  761. ++bms;
  762. if (!*bms) {
  763. bookmark[i].key = NULL;
  764. break;
  765. }
  766. *bms = '\0';
  767. bookmark[i].loc = ++bms;
  768. if (bookmark[i].loc[0] == '\0' || bookmark[i].loc[0] == ';') {
  769. bookmark[i].key = NULL;
  770. break;
  771. }
  772. while (*bms && *bms != ';')
  773. ++bms;
  774. if (*bms)
  775. *bms = '\0';
  776. else
  777. break;
  778. ++bms;
  779. ++i;
  780. }
  781. }
  782. static char *
  783. readinput(void)
  784. {
  785. timeout(-1);
  786. echo();
  787. curs_set(TRUE);
  788. memset(g_buf, 0, LINE_MAX);
  789. wgetnstr(stdscr, g_buf, LINE_MAX - 1);
  790. noecho();
  791. curs_set(FALSE);
  792. timeout(1000);
  793. return g_buf[0] ? g_buf : NULL;
  794. }
  795. static char *
  796. replace_escape(const char *str)
  797. {
  798. static char buffer[PATH_MAX];
  799. static wchar_t wbuf[PATH_MAX];
  800. static wchar_t *buf;
  801. buffer[0] = '\0';
  802. buf = wbuf;
  803. /* Convert multi-byte to wide char */
  804. mbstowcs(wbuf, str, PATH_MAX);
  805. while (*buf) {
  806. if (*buf <= '\x1f' || *buf == '\x7f')
  807. *buf = '\?';
  808. ++buf;
  809. }
  810. /* Convert wide char to multi-byte */
  811. wcstombs(buffer, wbuf, PATH_MAX);
  812. return buffer;
  813. }
  814. static void
  815. printent(struct entry *ent, int sel)
  816. {
  817. static int ncols;
  818. if (PATH_MAX + 16 < COLS)
  819. ncols = PATH_MAX + 16;
  820. else
  821. ncols = COLS;
  822. if (S_ISDIR(ent->mode))
  823. snprintf(g_buf, ncols, "%s%s/", CURSYM(sel),
  824. replace_escape(ent->name));
  825. else if (S_ISLNK(ent->mode))
  826. snprintf(g_buf, ncols, "%s%s@", CURSYM(sel),
  827. replace_escape(ent->name));
  828. else if (S_ISSOCK(ent->mode))
  829. snprintf(g_buf, ncols, "%s%s=", CURSYM(sel),
  830. replace_escape(ent->name));
  831. else if (S_ISFIFO(ent->mode))
  832. snprintf(g_buf, ncols, "%s%s|", CURSYM(sel),
  833. replace_escape(ent->name));
  834. else if (ent->mode & 0100)
  835. snprintf(g_buf, ncols, "%s%s*", CURSYM(sel),
  836. replace_escape(ent->name));
  837. else
  838. snprintf(g_buf, ncols, "%s%s", CURSYM(sel),
  839. replace_escape(ent->name));
  840. printw("%s\n", g_buf);
  841. }
  842. static char*
  843. coolsize(off_t size)
  844. {
  845. static const char * const U[]
  846. = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  847. static char size_buf[12]; /* Buffer to hold human readable size */
  848. static int i;
  849. static off_t tmp;
  850. static long double rem;
  851. i = 0;
  852. rem = 0;
  853. while (size > 1024) {
  854. tmp = size;
  855. size >>= 10;
  856. rem = tmp - (size << 10);
  857. ++i;
  858. }
  859. snprintf(size_buf, 12, "%.*Lf%s", i, size + rem * div_2_pow_10, U[i]);
  860. return size_buf;
  861. }
  862. static void
  863. printent_long(struct entry *ent, int sel)
  864. {
  865. static int ncols;
  866. static char buf[18];
  867. if (PATH_MAX + 32 < COLS)
  868. ncols = PATH_MAX + 32;
  869. else
  870. ncols = COLS;
  871. strftime(buf, 18, "%d %m %Y %H:%M", localtime(&ent->t));
  872. if (sel)
  873. attron(A_REVERSE);
  874. if (!cfg.blkorder) {
  875. if (S_ISDIR(ent->mode))
  876. snprintf(g_buf, ncols, "%s%-16.16s / %s/",
  877. CURSYM(sel), buf, replace_escape(ent->name));
  878. else if (S_ISLNK(ent->mode))
  879. snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
  880. CURSYM(sel), buf, replace_escape(ent->name));
  881. else if (S_ISSOCK(ent->mode))
  882. snprintf(g_buf, ncols, "%s%-16.16s = %s=",
  883. CURSYM(sel), buf, replace_escape(ent->name));
  884. else if (S_ISFIFO(ent->mode))
  885. snprintf(g_buf, ncols, "%s%-16.16s | %s|",
  886. CURSYM(sel), buf, replace_escape(ent->name));
  887. else if (S_ISBLK(ent->mode))
  888. snprintf(g_buf, ncols, "%s%-16.16s b %s",
  889. CURSYM(sel), buf, replace_escape(ent->name));
  890. else if (S_ISCHR(ent->mode))
  891. snprintf(g_buf, ncols, "%s%-16.16s c %s",
  892. CURSYM(sel), buf, replace_escape(ent->name));
  893. else if (ent->mode & 0100)
  894. snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
  895. CURSYM(sel), buf, coolsize(ent->size),
  896. replace_escape(ent->name));
  897. else
  898. snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
  899. CURSYM(sel), buf, coolsize(ent->size),
  900. replace_escape(ent->name));
  901. } else {
  902. if (S_ISDIR(ent->mode))
  903. snprintf(g_buf, ncols, "%s%-16.16s %8.8s/ %s/",
  904. CURSYM(sel), buf, coolsize(ent->blocks << 9),
  905. replace_escape(ent->name));
  906. else if (S_ISLNK(ent->mode))
  907. snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
  908. CURSYM(sel), buf,
  909. replace_escape(ent->name));
  910. else if (S_ISSOCK(ent->mode))
  911. snprintf(g_buf, ncols, "%s%-16.16s = %s=",
  912. CURSYM(sel), buf,
  913. replace_escape(ent->name));
  914. else if (S_ISFIFO(ent->mode))
  915. snprintf(g_buf, ncols, "%s%-16.16s | %s|",
  916. CURSYM(sel), buf,
  917. replace_escape(ent->name));
  918. else if (S_ISBLK(ent->mode))
  919. snprintf(g_buf, ncols, "%s%-16.16s b %s",
  920. CURSYM(sel), buf,
  921. replace_escape(ent->name));
  922. else if (S_ISCHR(ent->mode))
  923. snprintf(g_buf, ncols, "%s%-16.16s c %s",
  924. CURSYM(sel), buf,
  925. replace_escape(ent->name));
  926. else if (ent->mode & 0100)
  927. snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
  928. CURSYM(sel), buf, coolsize(ent->blocks << 9),
  929. replace_escape(ent->name));
  930. else
  931. snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
  932. CURSYM(sel), buf, coolsize(ent->blocks << 9),
  933. replace_escape(ent->name));
  934. }
  935. printw("%s\n", g_buf);
  936. if (sel)
  937. attroff(A_REVERSE);
  938. }
  939. static void (*printptr)(struct entry *ent, int sel) = &printent_long;
  940. static char
  941. get_fileind(mode_t mode, char *desc)
  942. {
  943. static char c;
  944. if (S_ISREG(mode)) {
  945. c = '-';
  946. sprintf(desc, "%s", "regular file");
  947. if (mode & 0100)
  948. strcat(desc, ", executable");
  949. } else if (S_ISDIR(mode)) {
  950. c = 'd';
  951. sprintf(desc, "%s", "directory");
  952. } else if (S_ISBLK(mode)) {
  953. c = 'b';
  954. sprintf(desc, "%s", "block special device");
  955. } else if (S_ISCHR(mode)) {
  956. c = 'c';
  957. sprintf(desc, "%s", "character special device");
  958. #ifdef S_ISFIFO
  959. } else if (S_ISFIFO(mode)) {
  960. c = 'p';
  961. sprintf(desc, "%s", "FIFO");
  962. #endif /* S_ISFIFO */
  963. #ifdef S_ISLNK
  964. } else if (S_ISLNK(mode)) {
  965. c = 'l';
  966. sprintf(desc, "%s", "symbolic link");
  967. #endif /* S_ISLNK */
  968. #ifdef S_ISSOCK
  969. } else if (S_ISSOCK(mode)) {
  970. c = 's';
  971. sprintf(desc, "%s", "socket");
  972. #endif /* S_ISSOCK */
  973. #ifdef S_ISDOOR
  974. /* Solaris 2.6, etc. */
  975. } else if (S_ISDOOR(mode)) {
  976. c = 'D';
  977. desc[0] = '\0';
  978. #endif /* S_ISDOOR */
  979. } else {
  980. /* Unknown type -- possibly a regular file? */
  981. c = '?';
  982. desc[0] = '\0';
  983. }
  984. return c;
  985. }
  986. /* Convert a mode field into "ls -l" type perms field. */
  987. static char *
  988. get_lsperms(mode_t mode, char *desc)
  989. {
  990. static const char * const rwx[] = {"---", "--x", "-w-", "-wx",
  991. "r--", "r-x", "rw-", "rwx"};
  992. static char bits[11];
  993. bits[0] = get_fileind(mode, desc);
  994. strcpy(&bits[1], rwx[(mode >> 6) & 7]);
  995. strcpy(&bits[4], rwx[(mode >> 3) & 7]);
  996. strcpy(&bits[7], rwx[(mode & 7)]);
  997. if (mode & S_ISUID)
  998. bits[3] = (mode & 0100) ? 's' : 'S'; /* user executable */
  999. if (mode & S_ISGID)
  1000. bits[6] = (mode & 0010) ? 's' : 'l'; /* group executable */
  1001. if (mode & S_ISVTX)
  1002. bits[9] = (mode & 0001) ? 't' : 'T'; /* others executable */
  1003. bits[10] = '\0';
  1004. return bits;
  1005. }
  1006. /*
  1007. * Gets only a single line (that's what we need
  1008. * for now) or shows full command output in pager.
  1009. *
  1010. * If pager is valid, returns NULL
  1011. */
  1012. static char *
  1013. get_output(char *buf, size_t bytes, char *file,
  1014. char *arg1, char *arg2, int pager)
  1015. {
  1016. pid_t pid;
  1017. int pipefd[2];
  1018. FILE *pf;
  1019. int status;
  1020. char *ret = NULL;
  1021. if (pipe(pipefd) == -1)
  1022. printerr(1, "pipe(2)");
  1023. pid = fork();
  1024. if (pid == 0) {
  1025. /* In child */
  1026. close(pipefd[0]);
  1027. dup2(pipefd[1], STDOUT_FILENO);
  1028. dup2(pipefd[1], STDERR_FILENO);
  1029. execlp(file, file, arg1, arg2, NULL);
  1030. _exit(1);
  1031. }
  1032. /* In parent */
  1033. waitpid(pid, &status, 0);
  1034. close(pipefd[1]);
  1035. if (!pager) {
  1036. pf = fdopen(pipefd[0], "r");
  1037. if (pf) {
  1038. ret = fgets(buf, bytes, pf);
  1039. close(pipefd[0]);
  1040. }
  1041. return ret;
  1042. }
  1043. pid = fork();
  1044. if (pid == 0) {
  1045. /* Show in pager in child */
  1046. dup2(pipefd[0], STDIN_FILENO);
  1047. execlp("less", "less", NULL);
  1048. close(pipefd[0]);
  1049. _exit(1);
  1050. }
  1051. /* In parent */
  1052. waitpid(pid, &status, 0);
  1053. return NULL;
  1054. }
  1055. /*
  1056. * Follows the stat(1) output closely
  1057. */
  1058. static int
  1059. show_stats(char *fpath, char *fname, struct stat *sb)
  1060. {
  1061. char *perms = get_lsperms(sb->st_mode, g_buf);
  1062. char *p, *begin = g_buf;
  1063. char tmp[] = "/tmp/nnnXXXXXX";
  1064. int fd = mkstemp(tmp);
  1065. if (fd == -1)
  1066. return -1;
  1067. /* Show file name or 'symlink' -> 'target' */
  1068. if (perms[0] == 'l') {
  1069. /* Note that MAX_CMD_LEN > PATH_MAX */
  1070. ssize_t len = readlink(fpath, g_buf, MAX_CMD_LEN);
  1071. if (len != -1) {
  1072. g_buf[len] = '\0';
  1073. dprintf(fd, " File: '%s' -> ",
  1074. replace_escape(fname));
  1075. dprintf(fd, "'%s'",
  1076. replace_escape(g_buf));
  1077. }
  1078. } else
  1079. dprintf(fd, " File: '%s'", replace_escape(fname));
  1080. /* Show size, blocks, file type */
  1081. #ifdef __APPLE__
  1082. dprintf(fd, "\n Size: %-15lld Blocks: %-10lld IO Block: %-6d %s",
  1083. #else
  1084. dprintf(fd, "\n Size: %-15ld Blocks: %-10ld IO Block: %-6ld %s",
  1085. #endif
  1086. sb->st_size, sb->st_blocks, sb->st_blksize, g_buf);
  1087. /* Show containing device, inode, hardlink count */
  1088. #ifdef __APPLE__
  1089. sprintf(g_buf, "%xh/%ud", sb->st_dev, sb->st_dev);
  1090. dprintf(fd, "\n Device: %-15s Inode: %-11llu Links: %-9hu",
  1091. #else
  1092. sprintf(g_buf, "%lxh/%lud", sb->st_dev, sb->st_dev);
  1093. dprintf(fd, "\n Device: %-15s Inode: %-11lu Links: %-9lu",
  1094. #endif
  1095. g_buf, sb->st_ino, sb->st_nlink);
  1096. /* Show major, minor number for block or char device */
  1097. if (perms[0] == 'b' || perms[0] == 'c')
  1098. dprintf(fd, " Device type: %x,%x",
  1099. major(sb->st_rdev), minor(sb->st_rdev));
  1100. /* Show permissions, owner, group */
  1101. dprintf(fd, "\n Access: 0%d%d%d/%s Uid: (%u/%s) Gid: (%u/%s)",
  1102. (sb->st_mode >> 6) & 7, (sb->st_mode >> 3) & 7, sb->st_mode & 7,
  1103. perms,
  1104. sb->st_uid, (getpwuid(sb->st_uid))->pw_name,
  1105. sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
  1106. /* Show last access time */
  1107. strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
  1108. dprintf(fd, "\n\n Access: %s", g_buf);
  1109. /* Show last modification time */
  1110. strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
  1111. dprintf(fd, "\n Modify: %s", g_buf);
  1112. /* Show last status change time */
  1113. strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
  1114. dprintf(fd, "\n Change: %s", g_buf);
  1115. if (S_ISREG(sb->st_mode)) {
  1116. /* Show file(1) output */
  1117. p = get_output(g_buf, MAX_CMD_LEN, "file", "-b", fpath, 0);
  1118. if (p) {
  1119. dprintf(fd, "\n\n ");
  1120. while (*p) {
  1121. if (*p == ',') {
  1122. *p = '\0';
  1123. dprintf(fd, " %s\n", begin);
  1124. begin = p + 1;
  1125. }
  1126. ++p;
  1127. }
  1128. dprintf(fd, " %s", begin);
  1129. }
  1130. dprintf(fd, "\n\n");
  1131. } else
  1132. dprintf(fd, "\n\n\n");
  1133. close(fd);
  1134. exitcurses();
  1135. get_output(NULL, 0, "cat", tmp, NULL, 1);
  1136. unlink(tmp);
  1137. initcurses();
  1138. return 0;
  1139. }
  1140. static int
  1141. show_mediainfo(char *fpath, char *arg)
  1142. {
  1143. if (!get_output(g_buf, MAX_CMD_LEN, "which", "mediainfo", NULL, 0))
  1144. return -1;
  1145. exitcurses();
  1146. get_output(NULL, 0, "mediainfo", fpath, arg, 1);
  1147. initcurses();
  1148. return 0;
  1149. }
  1150. static int
  1151. show_help(void)
  1152. {
  1153. char tmp[] = "/tmp/nnnXXXXXX";
  1154. static char helpstr[] = ("\
  1155. Key | Function\n\
  1156. -+-\n\
  1157. Up, k, ^P | Previous entry\n\
  1158. Down, j, ^N | Next entry\n\
  1159. PgUp, ^U | Scroll half page up\n\
  1160. PgDn, ^D | Scroll half page down\n\
  1161. Home, g, ^, ^A | Jump to first entry\n\
  1162. End, G, $, ^E | Jump to last entry\n\
  1163. Right, Enter, l, ^M | Open file or enter dir\n\
  1164. Left, Bksp, h, ^H | Go to parent dir\n\
  1165. Insert | Toggle navigate-as-you-type mode\n\
  1166. ~ | Jump to HOME dir\n\
  1167. & | Jump to initial dir\n\
  1168. - | Jump to last visited dir\n\
  1169. / | Filter dir contents\n\
  1170. ^/ | Search dir in desktop search tool\n\
  1171. . | Toggle hide .dot files\n\
  1172. b | Show bookmark key prompt\n\
  1173. c | Show change dir prompt\n\
  1174. d | Toggle detail view\n\
  1175. D | Toggle current file details screen\n\
  1176. m | Show concise mediainfo\n\
  1177. M | Show full mediainfo\n\
  1178. s | Toggle sort by file size\n\
  1179. S | Toggle disk usage analyzer mode\n\
  1180. t | Toggle sort by modified time\n\
  1181. ! | Spawn SHELL in PWD (fallback sh)\n\
  1182. e | Edit entry in EDITOR (fallback vi)\n\
  1183. o | Open dir in NNN_DE_FILE_MANAGER\n\
  1184. p | Open entry in PAGER (fallback less)\n\
  1185. ^K | Invoke file path copier\n\
  1186. ^L, F2 | Force a redraw, exit filter prompt\n\
  1187. ? | Toggle help and settings screen\n\
  1188. Q | Quit and change directory\n\
  1189. q, ^Q | Quit\n\n\n");
  1190. int i = 0, fd = mkstemp(tmp);
  1191. if (fd == -1)
  1192. return -1;
  1193. dprintf(fd, "%s", helpstr);
  1194. if (getenv("NNN_BMS")) {
  1195. dprintf(fd, "BOOKMARKS\n");
  1196. for (; i < MAX_BM; ++i)
  1197. if (bookmark[i].key)
  1198. dprintf(fd, " %s: %s\n",
  1199. bookmark[i].key, bookmark[i].loc);
  1200. else
  1201. break;
  1202. dprintf(fd, "\n");
  1203. }
  1204. if (editor)
  1205. dprintf(fd, "NNN_USE_EDITOR: %s\n", editor);
  1206. if (desktop_manager)
  1207. dprintf(fd, "NNN_DE_FILE_MANAGER: %s\n", desktop_manager);
  1208. if (idletimeout)
  1209. dprintf(fd, "NNN_IDLE_TIMEOUT: %d secs\n", idletimeout);
  1210. if (copier)
  1211. dprintf(fd, "NNN_COPIER: %s\n", copier);
  1212. dprintf(fd, "\n");
  1213. close(fd);
  1214. exitcurses();
  1215. get_output(NULL, 0, "cat", tmp, NULL, 1);
  1216. unlink(tmp);
  1217. initcurses();
  1218. return 0;
  1219. }
  1220. static int
  1221. sum_bsizes(const char *fpath, const struct stat *sb,
  1222. int typeflag, struct FTW *ftwbuf)
  1223. {
  1224. if (sb->st_blocks && (typeflag == FTW_F || typeflag == FTW_D))
  1225. ent_blocks += sb->st_blocks;
  1226. ++num_files;
  1227. return 0;
  1228. }
  1229. static int
  1230. getorder(size_t size)
  1231. {
  1232. switch (size) {
  1233. case 4096:
  1234. return 12;
  1235. case 512:
  1236. return 9;
  1237. case 8192:
  1238. return 13;
  1239. case 16384:
  1240. return 14;
  1241. case 32768:
  1242. return 15;
  1243. case 65536:
  1244. return 16;
  1245. case 131072:
  1246. return 17;
  1247. case 262144:
  1248. return 18;
  1249. case 524288:
  1250. return 19;
  1251. case 1048576:
  1252. return 20;
  1253. case 2048:
  1254. return 11;
  1255. case 1024:
  1256. return 10;
  1257. default:
  1258. return 0;
  1259. }
  1260. }
  1261. static int
  1262. dentfill(char *path, struct entry **dents,
  1263. int (*filter)(regex_t *, char *), regex_t *re)
  1264. {
  1265. static DIR *dirp;
  1266. static struct dirent *dp;
  1267. static struct stat sb;
  1268. static int fd, n;
  1269. n = 0;
  1270. if (cfg.blkorder) {
  1271. num_files = 0;
  1272. dir_blocks = 0;
  1273. }
  1274. dirp = opendir(path);
  1275. if (dirp == NULL)
  1276. return 0;
  1277. fd = dirfd(dirp);
  1278. while ((dp = readdir(dirp)) != NULL) {
  1279. /* Skip self and parent */
  1280. if ((dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
  1281. (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
  1282. continue;
  1283. if (filter(re, dp->d_name) == 0) {
  1284. if (!cfg.blkorder)
  1285. continue;
  1286. if (fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1)
  1287. continue;
  1288. if (S_ISDIR(sb.st_mode)) {
  1289. ent_blocks = 0;
  1290. mkpath(path, dp->d_name, g_buf, PATH_MAX);
  1291. if (nftw(g_buf, sum_bsizes, open_max,
  1292. FTW_MOUNT | FTW_PHYS) == -1) {
  1293. printmsg("nftw(3) failed");
  1294. dir_blocks += sb.st_blocks;
  1295. } else
  1296. dir_blocks += ent_blocks;
  1297. } else {
  1298. if (sb.st_blocks)
  1299. dir_blocks += sb.st_blocks;
  1300. ++num_files;
  1301. }
  1302. continue;
  1303. }
  1304. if (fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
  1305. if (*dents)
  1306. free(*dents);
  1307. printerr(1, "fstatat");
  1308. }
  1309. if (n == total_dents) {
  1310. total_dents += 64;
  1311. *dents = realloc(*dents, total_dents * sizeof(**dents));
  1312. if (*dents == NULL)
  1313. printerr(1, "realloc");
  1314. }
  1315. xstrlcpy((*dents)[n].name, dp->d_name, NAME_MAX);
  1316. (*dents)[n].mode = sb.st_mode;
  1317. (*dents)[n].t = sb.st_mtime;
  1318. (*dents)[n].size = sb.st_size;
  1319. if (cfg.blkorder) {
  1320. if (S_ISDIR(sb.st_mode)) {
  1321. ent_blocks = 0;
  1322. mkpath(path, dp->d_name, g_buf, PATH_MAX);
  1323. if (nftw(g_buf, sum_bsizes, open_max,
  1324. FTW_MOUNT | FTW_PHYS) == -1) {
  1325. printmsg("nftw(3) failed");
  1326. (*dents)[n].blocks = sb.st_blocks;
  1327. } else
  1328. (*dents)[n].blocks = ent_blocks;
  1329. } else {
  1330. (*dents)[n].blocks = sb.st_blocks;
  1331. ++num_files;
  1332. }
  1333. if ((*dents)[n].blocks)
  1334. dir_blocks += (*dents)[n].blocks;
  1335. }
  1336. ++n;
  1337. }
  1338. if (cfg.blkorder) {
  1339. static struct statvfs svb;
  1340. if (statvfs(path, &svb) == -1)
  1341. fs_free = 0;
  1342. else
  1343. fs_free = svb.f_bavail << getorder(svb.f_bsize);
  1344. }
  1345. /* Should never be null */
  1346. if (closedir(dirp) == -1) {
  1347. if (*dents)
  1348. free(*dents);
  1349. printerr(1, "closedir");
  1350. }
  1351. return n;
  1352. }
  1353. static void
  1354. dentfree(struct entry *dents)
  1355. {
  1356. free(dents);
  1357. }
  1358. /* Return the position of the matching entry or 0 otherwise */
  1359. static int
  1360. dentfind(struct entry *dents, int n, char *path)
  1361. {
  1362. if (!path)
  1363. return 0;
  1364. static int i;
  1365. static char *p;
  1366. p = basename(path);
  1367. DPRINTF_S(p);
  1368. for (i = 0; i < n; ++i)
  1369. if (strcmp(p, dents[i].name) == 0)
  1370. return i;
  1371. return 0;
  1372. }
  1373. static int
  1374. populate(char *path, char *oldpath, char *fltr)
  1375. {
  1376. static regex_t re;
  1377. /* Can fail when permissions change while browsing */
  1378. if (canopendir(path) == 0)
  1379. return -1;
  1380. /* Search filter */
  1381. if (setfilter(&re, fltr) != 0)
  1382. return -1;
  1383. if (cfg.blkorder) {
  1384. printmsg("Calculating...");
  1385. refresh();
  1386. }
  1387. ndents = dentfill(path, &dents, visible, &re);
  1388. qsort(dents, ndents, sizeof(*dents), entrycmp);
  1389. /* Find cur from history */
  1390. cur = dentfind(dents, ndents, oldpath);
  1391. return 0;
  1392. }
  1393. static void
  1394. redraw(char *path)
  1395. {
  1396. static int nlines, i;
  1397. static size_t ncols;
  1398. nlines = MIN(LINES - 4, ndents);
  1399. /* Clean screen */
  1400. erase();
  1401. /* Strip trailing slashes */
  1402. for (i = strlen(path) - 1; i > 0; --i)
  1403. if (path[i] == '/')
  1404. path[i] = '\0';
  1405. else
  1406. break;
  1407. DPRINTF_D(cur);
  1408. DPRINTF_S(path);
  1409. /* No text wrapping in cwd line */
  1410. if (!realpath(path, g_buf)) {
  1411. printmsg("Cannot resolve path");
  1412. return;
  1413. }
  1414. ncols = COLS;
  1415. if (ncols > PATH_MAX)
  1416. ncols = PATH_MAX;
  1417. g_buf[ncols - strlen(CWD) - 1] = '\0';
  1418. printw(CWD "%s\n\n", g_buf);
  1419. /* Print listing */
  1420. if (cur < (nlines >> 1)) {
  1421. for (i = 0; i < nlines; ++i)
  1422. printptr(&dents[i], i == cur);
  1423. } else if (cur >= ndents - (nlines >> 1)) {
  1424. for (i = ndents - nlines; i < ndents; ++i)
  1425. printptr(&dents[i], i == cur);
  1426. } else {
  1427. static int odd;
  1428. odd = ISODD(nlines);
  1429. nlines >>= 1;
  1430. for (i = cur - nlines; i < cur + nlines + odd; ++i)
  1431. printptr(&dents[i], i == cur);
  1432. }
  1433. if (cfg.showdetail) {
  1434. if (ndents) {
  1435. static char ind[2] = "\0\0";
  1436. static char sort[17];
  1437. if (cfg.mtimeorder)
  1438. sprintf(sort, "by time ");
  1439. else if (cfg.sizeorder)
  1440. sprintf(sort, "by size ");
  1441. else
  1442. sort[0] = '\0';
  1443. if (S_ISDIR(dents[cur].mode))
  1444. ind[0] = '/';
  1445. else if (S_ISLNK(dents[cur].mode))
  1446. ind[0] = '@';
  1447. else if (S_ISSOCK(dents[cur].mode))
  1448. ind[0] = '=';
  1449. else if (S_ISFIFO(dents[cur].mode))
  1450. ind[0] = '|';
  1451. else if (dents[cur].mode & 0100)
  1452. ind[0] = '*';
  1453. else
  1454. ind[0] = '\0';
  1455. if (!cfg.blkorder)
  1456. sprintf(g_buf, "total %d %s[%s%s]", ndents, sort,
  1457. replace_escape(dents[cur].name), ind);
  1458. else {
  1459. i = sprintf(g_buf, "du: %s (%lu files) ",
  1460. coolsize(dir_blocks << 9), num_files);
  1461. sprintf(g_buf + i, "vol: %s free [%s%s]", coolsize(fs_free),
  1462. replace_escape(dents[cur].name), ind);
  1463. }
  1464. printmsg(g_buf);
  1465. } else
  1466. printmsg("0 items");
  1467. }
  1468. }
  1469. static void
  1470. browse(char *ipath, char *ifilter)
  1471. {
  1472. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  1473. char lastdir[PATH_MAX];
  1474. char fltr[LINE_MAX];
  1475. char *mime, *dir, *tmp, *run, *env;
  1476. struct stat sb;
  1477. int r, fd, presel;
  1478. enum action sel = SEL_RUNARG + 1;
  1479. xstrlcpy(path, ipath, PATH_MAX);
  1480. xstrlcpy(fltr, ifilter, LINE_MAX);
  1481. oldpath[0] = '\0';
  1482. newpath[0] = '\0';
  1483. lastdir[0] = '\0'; /* Can't move back from initial directory */
  1484. if (cfg.filtermode)
  1485. presel = FILTER;
  1486. else
  1487. presel = 0;
  1488. begin:
  1489. if (populate(path, oldpath, fltr) == -1) {
  1490. printwarn();
  1491. goto nochange;
  1492. }
  1493. for (;;) {
  1494. redraw(path);
  1495. nochange:
  1496. /* Exit if parent has exited */
  1497. if (getppid() == 1)
  1498. _exit(0);
  1499. sel = nextsel(&run, &env, &presel);
  1500. switch (sel) {
  1501. case SEL_CDQUIT:
  1502. {
  1503. char *tmpfile = "/tmp/nnn";
  1504. tmp = getenv("NNN_TMPFILE");
  1505. if (tmp)
  1506. tmpfile = tmp;
  1507. FILE *fp = fopen(tmpfile, "w");
  1508. if (fp) {
  1509. fprintf(fp, "cd \"%s\"", path);
  1510. fclose(fp);
  1511. }
  1512. /* Fall through to exit */
  1513. } // fallthrough
  1514. case SEL_QUIT:
  1515. dentfree(dents);
  1516. return;
  1517. case SEL_BACK:
  1518. /* There is no going back */
  1519. if (path[0] == '/' && path[1] == '\0') {
  1520. printmsg("You are at /");
  1521. goto nochange;
  1522. }
  1523. dir = xdirname(path);
  1524. if (canopendir(dir) == 0) {
  1525. printwarn();
  1526. goto nochange;
  1527. }
  1528. /* Save history */
  1529. xstrlcpy(oldpath, path, PATH_MAX);
  1530. /* Save last working directory */
  1531. xstrlcpy(lastdir, path, PATH_MAX);
  1532. xstrlcpy(path, dir, PATH_MAX);
  1533. /* Reset filter */
  1534. xstrlcpy(fltr, ifilter, LINE_MAX);
  1535. if (cfg.filtermode)
  1536. presel = FILTER;
  1537. goto begin;
  1538. case SEL_GOIN:
  1539. /* Cannot descend in empty directories */
  1540. if (ndents == 0)
  1541. goto begin;
  1542. mkpath(path, dents[cur].name, newpath, PATH_MAX);
  1543. DPRINTF_S(newpath);
  1544. /* Get path info */
  1545. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  1546. if (fd == -1) {
  1547. printwarn();
  1548. goto nochange;
  1549. }
  1550. r = fstat(fd, &sb);
  1551. if (r == -1) {
  1552. printwarn();
  1553. close(fd);
  1554. goto nochange;
  1555. }
  1556. close(fd);
  1557. DPRINTF_U(sb.st_mode);
  1558. switch (sb.st_mode & S_IFMT) {
  1559. case S_IFDIR:
  1560. if (canopendir(newpath) == 0) {
  1561. printwarn();
  1562. goto nochange;
  1563. }
  1564. /* Save last working directory */
  1565. xstrlcpy(lastdir, path, PATH_MAX);
  1566. xstrlcpy(path, newpath, PATH_MAX);
  1567. oldpath[0] = '\0';
  1568. /* Reset filter */
  1569. xstrlcpy(fltr, ifilter, LINE_MAX);
  1570. if (cfg.filtermode)
  1571. presel = FILTER;
  1572. goto begin;
  1573. case S_IFREG:
  1574. {
  1575. /* If NNN_USE_EDITOR is set,
  1576. * open text in EDITOR
  1577. */
  1578. if (editor) {
  1579. mime = getmime(dents[cur].name);
  1580. if (mime) {
  1581. spawn(editor, newpath, NULL,
  1582. NULL, SP_NORMAL);
  1583. continue;
  1584. }
  1585. /* Recognize and open plain
  1586. * text files with vi
  1587. */
  1588. if (get_output(g_buf, MAX_CMD_LEN,
  1589. "file", "-bi",
  1590. newpath, 0) == NULL)
  1591. continue;
  1592. if (strstr(g_buf, "text/") == g_buf) {
  1593. spawn(editor, newpath, NULL,
  1594. NULL, SP_NORMAL);
  1595. continue;
  1596. }
  1597. }
  1598. /* Invoke desktop opener as last resort */
  1599. spawn(utils[0], newpath, NULL, NULL, SP_NOTRACE);
  1600. continue;
  1601. }
  1602. default:
  1603. printmsg("Unsupported file");
  1604. goto nochange;
  1605. }
  1606. case SEL_FLTR:
  1607. presel = readln(path);
  1608. xstrlcpy(fltr, ifilter, LINE_MAX);
  1609. DPRINTF_S(fltr);
  1610. /* Save current */
  1611. if (ndents > 0)
  1612. mkpath(path, dents[cur].name, oldpath,
  1613. PATH_MAX);
  1614. goto nochange;
  1615. case SEL_MFLTR:
  1616. cfg.filtermode ^= 1;
  1617. if (cfg.filtermode)
  1618. presel = FILTER;
  1619. else
  1620. printmsg("navigate-as-you-type off");
  1621. goto nochange;
  1622. case SEL_SEARCH:
  1623. spawn(player, path, "search", NULL, SP_NORMAL);
  1624. break;
  1625. case SEL_NEXT:
  1626. if (cur < ndents - 1)
  1627. ++cur;
  1628. else if (ndents)
  1629. /* Roll over, set cursor to first entry */
  1630. cur = 0;
  1631. break;
  1632. case SEL_PREV:
  1633. if (cur > 0)
  1634. --cur;
  1635. else if (ndents)
  1636. /* Roll over, set cursor to last entry */
  1637. cur = ndents - 1;
  1638. break;
  1639. case SEL_PGDN:
  1640. if (cur < ndents - 1)
  1641. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  1642. break;
  1643. case SEL_PGUP:
  1644. if (cur > 0)
  1645. cur -= MIN((LINES - 4) / 2, cur);
  1646. break;
  1647. case SEL_HOME:
  1648. cur = 0;
  1649. break;
  1650. case SEL_END:
  1651. cur = ndents - 1;
  1652. break;
  1653. case SEL_CD:
  1654. {
  1655. static char *input;
  1656. static int truecd;
  1657. /* Save the program start dir */
  1658. tmp = getcwd(newpath, PATH_MAX);
  1659. if (tmp == NULL) {
  1660. printwarn();
  1661. goto nochange;
  1662. }
  1663. /* Switch to current path for readline(3) */
  1664. if (chdir(path) == -1) {
  1665. printwarn();
  1666. goto nochange;
  1667. }
  1668. exitcurses();
  1669. tmp = readline("chdir: ");
  1670. initcurses();
  1671. /* Change back to program start dir */
  1672. if (chdir(newpath) == -1)
  1673. printwarn();
  1674. if (tmp[0] == '\0')
  1675. break;
  1676. /* Add to readline(3) history */
  1677. add_history(tmp);
  1678. input = tmp;
  1679. tmp = strstrip(tmp);
  1680. if (tmp[0] == '\0') {
  1681. free(input);
  1682. break;
  1683. }
  1684. truecd = 0;
  1685. if (tmp[0] == '~') {
  1686. /* Expand ~ to HOME absolute path */
  1687. char *home = getenv("HOME");
  1688. if (home)
  1689. snprintf(newpath, PATH_MAX, "%s%s",
  1690. home, tmp + 1);
  1691. else {
  1692. free(input);
  1693. printmsg("HOME not set");
  1694. goto nochange;
  1695. }
  1696. } else if (tmp[0] == '-' && tmp[1] == '\0') {
  1697. if (lastdir[0] == '\0') {
  1698. free(input);
  1699. break;
  1700. }
  1701. /* Switch to last visited dir */
  1702. xstrlcpy(newpath, lastdir, PATH_MAX);
  1703. truecd = 1;
  1704. } else if ((r = all_dots(tmp))) {
  1705. if (r == 1) {
  1706. /* Always in the current dir */
  1707. free(input);
  1708. break;
  1709. }
  1710. --r;
  1711. dir = path;
  1712. for (fd = 0; fd < r; ++fd) {
  1713. /* Reached / ? */
  1714. if (path[0] == '/' && path[1] == '\0') {
  1715. /* If it's a cd .. at / */
  1716. if (fd == 0) {
  1717. printmsg("You are at /");
  1718. free(input);
  1719. goto nochange;
  1720. }
  1721. /* Can't cd beyond / anyway */
  1722. break;
  1723. }
  1724. dir = xdirname(dir);
  1725. if (canopendir(dir) == 0) {
  1726. printwarn();
  1727. free(input);
  1728. goto nochange;
  1729. }
  1730. }
  1731. truecd = 1;
  1732. /* Save the path in case of cd ..
  1733. * We mark the current dir in parent dir
  1734. */
  1735. if (r == 1) {
  1736. xstrlcpy(oldpath, path, PATH_MAX);
  1737. truecd = 2;
  1738. }
  1739. xstrlcpy(newpath, dir, PATH_MAX);
  1740. } else
  1741. mkpath(path, tmp, newpath, PATH_MAX);
  1742. free(input);
  1743. if (canopendir(newpath) == 0) {
  1744. printwarn();
  1745. break;
  1746. }
  1747. if (truecd == 0) {
  1748. /* Probable change in dir */
  1749. /* No-op if it's the same directory */
  1750. if (strcmp(path, newpath) == 0)
  1751. break;
  1752. oldpath[0] = '\0';
  1753. } else if (truecd == 1)
  1754. /* Sure change in dir */
  1755. oldpath[0] = '\0';
  1756. /* Save last working directory */
  1757. xstrlcpy(lastdir, path, PATH_MAX);
  1758. /* Save the newly opted dir in path */
  1759. xstrlcpy(path, newpath, PATH_MAX);
  1760. /* Reset filter */
  1761. xstrlcpy(fltr, ifilter, LINE_MAX);
  1762. DPRINTF_S(path);
  1763. if (cfg.filtermode)
  1764. presel = FILTER;
  1765. goto begin;
  1766. }
  1767. case SEL_CDHOME:
  1768. tmp = getenv("HOME");
  1769. if (tmp == NULL) {
  1770. clearprompt();
  1771. goto nochange;
  1772. }
  1773. if (canopendir(tmp) == 0) {
  1774. printwarn();
  1775. goto nochange;
  1776. }
  1777. if (strcmp(path, tmp) == 0)
  1778. break;
  1779. /* Save last working directory */
  1780. xstrlcpy(lastdir, path, PATH_MAX);
  1781. xstrlcpy(path, tmp, PATH_MAX);
  1782. oldpath[0] = '\0';
  1783. /* Reset filter */
  1784. xstrlcpy(fltr, ifilter, LINE_MAX);
  1785. DPRINTF_S(path);
  1786. if (cfg.filtermode)
  1787. presel = FILTER;
  1788. goto begin;
  1789. case SEL_CDBEGIN:
  1790. if (canopendir(ipath) == 0) {
  1791. printwarn();
  1792. goto nochange;
  1793. }
  1794. if (strcmp(path, ipath) == 0)
  1795. break;
  1796. /* Save last working directory */
  1797. xstrlcpy(lastdir, path, PATH_MAX);
  1798. xstrlcpy(path, ipath, PATH_MAX);
  1799. oldpath[0] = '\0';
  1800. /* Reset filter */
  1801. xstrlcpy(fltr, ifilter, LINE_MAX);
  1802. DPRINTF_S(path);
  1803. if (cfg.filtermode)
  1804. presel = FILTER;
  1805. goto begin;
  1806. case SEL_CDLAST:
  1807. if (lastdir[0] == '\0')
  1808. break;
  1809. if (canopendir(lastdir) == 0) {
  1810. printwarn();
  1811. goto nochange;
  1812. }
  1813. xstrlcpy(newpath, lastdir, PATH_MAX);
  1814. xstrlcpy(lastdir, path, PATH_MAX);
  1815. xstrlcpy(path, newpath, PATH_MAX);
  1816. oldpath[0] = '\0';
  1817. /* Reset filter */
  1818. xstrlcpy(fltr, ifilter, LINE_MAX);
  1819. DPRINTF_S(path);
  1820. if (cfg.filtermode)
  1821. presel = FILTER;
  1822. goto begin;
  1823. case SEL_CDBM:
  1824. printprompt("key: ");
  1825. tmp = readinput();
  1826. if (tmp == NULL) {
  1827. clearprompt();
  1828. goto nochange;
  1829. }
  1830. clearprompt();
  1831. for (r = 0; bookmark[r].key && r < MAX_BM; ++r) {
  1832. if (strcmp(bookmark[r].key, tmp) == 0) {
  1833. if (bookmark[r].loc[0] == '~') {
  1834. /* Expand ~ to HOME */
  1835. char *home = getenv("HOME");
  1836. if (home)
  1837. snprintf(newpath,
  1838. PATH_MAX,
  1839. "%s%s",
  1840. home,
  1841. bookmark[r].loc
  1842. + 1);
  1843. else {
  1844. printmsg("HOME not set");
  1845. goto nochange;
  1846. }
  1847. } else
  1848. mkpath(path, bookmark[r].loc,
  1849. newpath, PATH_MAX);
  1850. if (canopendir(newpath) == 0) {
  1851. printwarn();
  1852. goto nochange;
  1853. }
  1854. if (strcmp(path, newpath) == 0)
  1855. break;
  1856. oldpath[0] = '\0';
  1857. break;
  1858. }
  1859. }
  1860. if (!bookmark[r].key) {
  1861. printmsg("No matching bookmark");
  1862. goto nochange;
  1863. }
  1864. /* Save last working directory */
  1865. xstrlcpy(lastdir, path, PATH_MAX);
  1866. /* Save the newly opted dir in path */
  1867. xstrlcpy(path, newpath, PATH_MAX);
  1868. /* Reset filter */
  1869. xstrlcpy(fltr, ifilter, LINE_MAX);
  1870. DPRINTF_S(path);
  1871. if (cfg.filtermode)
  1872. presel = FILTER;
  1873. goto begin;
  1874. case SEL_TOGGLEDOT:
  1875. cfg.showhidden ^= 1;
  1876. initfilter(cfg.showhidden, &ifilter);
  1877. xstrlcpy(fltr, ifilter, LINE_MAX);
  1878. goto begin;
  1879. case SEL_DETAIL:
  1880. cfg.showdetail ^= 1;
  1881. cfg.showdetail ? (printptr = &printent_long)
  1882. : (printptr = &printent);
  1883. /* Save current */
  1884. if (ndents > 0)
  1885. mkpath(path, dents[cur].name, oldpath,
  1886. PATH_MAX);
  1887. goto begin;
  1888. case SEL_STATS:
  1889. {
  1890. struct stat sb;
  1891. if (ndents > 0) {
  1892. mkpath(path, dents[cur].name, oldpath,
  1893. PATH_MAX);
  1894. r = lstat(oldpath, &sb);
  1895. if (r == -1) {
  1896. if (dents)
  1897. dentfree(dents);
  1898. printerr(1, "lstat");
  1899. } else {
  1900. r = show_stats(oldpath, dents[cur].name,
  1901. &sb);
  1902. if (r < 0) {
  1903. printmsg(strerror(errno));
  1904. goto nochange;
  1905. }
  1906. }
  1907. }
  1908. break;
  1909. }
  1910. case SEL_MEDIA:
  1911. if (ndents > 0) {
  1912. mkpath(path, dents[cur].name, oldpath,
  1913. PATH_MAX);
  1914. if (show_mediainfo(oldpath, NULL) == -1) {
  1915. printmsg("mediainfo missing");
  1916. goto nochange;
  1917. }
  1918. }
  1919. break;
  1920. case SEL_FMEDIA:
  1921. if (ndents > 0) {
  1922. mkpath(path, dents[cur].name, oldpath,
  1923. PATH_MAX);
  1924. if (show_mediainfo(oldpath, "-f") == -1) {
  1925. printmsg("mediainfo missing");
  1926. goto nochange;
  1927. }
  1928. }
  1929. break;
  1930. case SEL_DFB:
  1931. if (!desktop_manager) {
  1932. printmsg("NNN_DE_FILE_MANAGER not set");
  1933. goto nochange;
  1934. }
  1935. spawn(desktop_manager, path, NULL, path, SP_NOTRACE | SP_NOWAIT);
  1936. break;
  1937. case SEL_FSIZE:
  1938. cfg.sizeorder ^= 1;
  1939. cfg.mtimeorder = 0;
  1940. cfg.blkorder = 0;
  1941. /* Save current */
  1942. if (ndents > 0)
  1943. mkpath(path, dents[cur].name, oldpath,
  1944. PATH_MAX);
  1945. goto begin;
  1946. case SEL_BSIZE:
  1947. cfg.blkorder ^= 1;
  1948. if (cfg.blkorder) {
  1949. cfg.showdetail = 1;
  1950. printptr = &printent_long;
  1951. }
  1952. cfg.mtimeorder = 0;
  1953. cfg.sizeorder = 0;
  1954. /* Save current */
  1955. if (ndents > 0)
  1956. mkpath(path, dents[cur].name, oldpath,
  1957. PATH_MAX);
  1958. goto begin;
  1959. case SEL_MTIME:
  1960. cfg.mtimeorder ^= 1;
  1961. cfg.sizeorder = 0;
  1962. cfg.blkorder = 0;
  1963. /* Save current */
  1964. if (ndents > 0)
  1965. mkpath(path, dents[cur].name, oldpath,
  1966. PATH_MAX);
  1967. goto begin;
  1968. case SEL_REDRAW:
  1969. /* Save current */
  1970. if (ndents > 0)
  1971. mkpath(path, dents[cur].name, oldpath,
  1972. PATH_MAX);
  1973. goto begin;
  1974. case SEL_COPY:
  1975. if (copier && ndents) {
  1976. if (strcmp(path, "/") == 0)
  1977. snprintf(newpath, PATH_MAX, "/%s",
  1978. dents[cur].name);
  1979. else
  1980. snprintf(newpath, PATH_MAX, "%s/%s",
  1981. path, dents[cur].name);
  1982. spawn(copier, newpath, NULL, NULL, SP_NONE);
  1983. printmsg(newpath);
  1984. } else if (!copier)
  1985. printmsg("NNN_COPIER is not set");
  1986. goto nochange;
  1987. case SEL_HELP:
  1988. show_help();
  1989. break;
  1990. case SEL_RUN:
  1991. run = xgetenv(env, run);
  1992. spawn(run, NULL, NULL, path, SP_NORMAL | SP_MARKER);
  1993. /* Repopulate as directory content may have changed */
  1994. goto begin;
  1995. case SEL_RUNARG:
  1996. run = xgetenv(env, run);
  1997. spawn(run, dents[cur].name, NULL, path, SP_NORMAL);
  1998. break;
  1999. }
  2000. /* Screensaver */
  2001. if (idletimeout != 0 && idle == idletimeout) {
  2002. idle = 0;
  2003. spawn(player, "", "screensaver", NULL, SP_NORMAL | SP_SIGINT);
  2004. }
  2005. }
  2006. }
  2007. static void
  2008. usage(void)
  2009. {
  2010. printf("usage: nnn [-l] [-i] [-p custom_nlay] [-S] [-v] [-h] [PATH]\n\n\
  2011. The missing terminal file browser for X.\n\n\
  2012. positional arguments:\n\
  2013. PATH directory to open [default: current dir]\n\n\
  2014. optional arguments:\n\
  2015. -l start in light mode (fewer details)\n\
  2016. -i start in navigate-as-you-type mode\n\
  2017. -p path to custom nlay\n\
  2018. -S start in disk usage analyzer mode\n\
  2019. -v show program version and exit\n\
  2020. -h show this help and exit\n\n\
  2021. Version: %s\n\
  2022. License: BSD 2-Clause\n\
  2023. Webpage: https://github.com/jarun/nnn\n", VERSION);
  2024. exit(0);
  2025. }
  2026. int
  2027. main(int argc, char *argv[])
  2028. {
  2029. char cwd[PATH_MAX], *ipath;
  2030. char *ifilter, *bmstr;
  2031. int opt;
  2032. /* Confirm we are in a terminal */
  2033. if (!isatty(0) || !isatty(1)) {
  2034. fprintf(stderr, "stdin or stdout is not a tty\n");
  2035. exit(1);
  2036. }
  2037. while ((opt = getopt(argc, argv, "dlSip:vh")) != -1) {
  2038. switch (opt) {
  2039. case 'S':
  2040. cfg.blkorder = 1;
  2041. break;
  2042. case 'l':
  2043. cfg.showdetail = 0;
  2044. printptr = &printent;
  2045. break;
  2046. case 'i':
  2047. cfg.filtermode = 1;
  2048. break;
  2049. case 'p':
  2050. player = optarg;
  2051. break;
  2052. case 'v':
  2053. printf("%s\n", VERSION);
  2054. return 0;
  2055. case 'd':
  2056. fprintf(stderr, "Option -d is deprecated and will be removed, detail view mode is default now.\n");
  2057. break;
  2058. case 'h': // fallthrough
  2059. default:
  2060. usage();
  2061. }
  2062. }
  2063. if (argc == optind) {
  2064. /* Start in the current directory */
  2065. ipath = getcwd(cwd, PATH_MAX);
  2066. if (ipath == NULL)
  2067. ipath = "/";
  2068. } else {
  2069. ipath = realpath(argv[optind], cwd);
  2070. if (!ipath) {
  2071. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  2072. exit(1);
  2073. }
  2074. }
  2075. open_max = max_openfds();
  2076. if (getuid() == 0)
  2077. cfg.showhidden = 1;
  2078. initfilter(cfg.showhidden, &ifilter);
  2079. /* Parse bookmarks string, if available */
  2080. bmstr = getenv("NNN_BMS");
  2081. if (bmstr)
  2082. parsebmstr(bmstr);
  2083. /* Edit text in EDITOR, if opted */
  2084. if (getenv("NNN_USE_EDITOR"))
  2085. editor = xgetenv("EDITOR", "vi");
  2086. /* Set player if not set already */
  2087. if (!player)
  2088. player = utils[1];
  2089. /* Get the desktop file browser, if set */
  2090. desktop_manager = getenv("NNN_DE_FILE_MANAGER");
  2091. /* Get screensaver wait time, if set; copier used as tmp var */
  2092. copier = getenv("NNN_IDLE_TIMEOUT");
  2093. if (copier)
  2094. idletimeout = abs(atoi(copier));
  2095. /* Get the default copier, if set */
  2096. copier = getenv("NNN_COPIER");
  2097. signal(SIGINT, SIG_IGN);
  2098. /* Test initial path */
  2099. if (canopendir(ipath) == 0) {
  2100. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  2101. exit(1);
  2102. }
  2103. /* Set locale */
  2104. setlocale(LC_ALL, "");
  2105. #ifdef DEBUGMODE
  2106. enabledbg();
  2107. #endif
  2108. initcurses();
  2109. browse(ipath, ifilter);
  2110. exitcurses();
  2111. #ifdef DEBUGMODE
  2112. disabledbg();
  2113. #endif
  2114. exit(0);
  2115. }