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

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