Fix reasonable checkpatch reports

This commit is contained in:
Arun Prakash Jana 2017-06-15 19:34:56 +05:30
parent 8404c0fd04
commit 6d20178881
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 196 additions and 147 deletions

View file

@ -3,12 +3,12 @@
#define CURSR " > " #define CURSR " > "
#define EMPTY " " #define EMPTY " "
static int filtermode = 0; /* Set to 1 to enter filter mode */ static int filtermode; /* Set to 1 to enter filter mode */
static int mtimeorder = 0; /* Set to 1 to sort by time modified */ static int mtimeorder; /* Set to 1 to sort by time modified */
static int sizeorder = 0; /* Set to 1 to sort by file size */ static int sizeorder; /* Set to 1 to sort by file size */
static int bsizeorder = 0; /* Set to 1 to sort by blocks used including content */ static int bsizeorder; /* Set to 1 to sort by blocks used (disk usage) */
static int idletimeout = 0; /* Screensaver timeout in seconds, 0 to disable */ static int idletimeout; /* Idle timeout in seconds, 0 to disable */
static int showhidden = 0; /* Set to 1 to show hidden files by default */ static int showhidden; /* Set to 1 to show hidden files by default */
static int showdetail = 1; /* Set to 0 to show fewer file info */ static int showdetail = 1; /* Set to 0 to show fewer file info */
static struct assoc assocs[] = { static struct assoc assocs[] = {

293
nnn.c
View file

@ -147,12 +147,12 @@ typedef unsigned long ulong;
/* Externs */ /* Externs */
#ifdef __APPLE__ #ifdef __APPLE__
extern int add_history(const char *); extern int add_history(const char *string);
#else #else
extern void add_history(const char *string); extern void add_history(const char *string);
#endif #endif
extern int wget_wch(WINDOW *, wint_t *); extern int wget_wch(WINDOW *win, wint_t *wch);
/* Global context */ /* Global context */
static struct entry *dents; static struct entry *dents;
@ -207,9 +207,8 @@ static rlim_t
max_openfds() max_openfds()
{ {
struct rlimit rl; struct rlimit rl;
rlim_t limit; rlim_t limit = getrlimit(RLIMIT_NOFILE, &rl);
limit = getrlimit(RLIMIT_NOFILE, &rl);
if (limit != 0) if (limit != 0)
return 32; return 32;
@ -229,7 +228,8 @@ max_openfds()
static void static void
xstrlcpy(char *dest, const char *src, size_t n) xstrlcpy(char *dest, const char *src, size_t n)
{ {
while (--n && (*dest++ = *src++)); while (--n && (*dest++ = *src++))
;
if (!n) if (!n)
*dest = '\0'; *dest = '\0';
} }
@ -296,10 +296,11 @@ xdirname(const char *path)
/* Terminate the buffer. */ /* Terminate the buffer. */
if (runp == buf) { if (runp == buf) {
/* The last slash is the first character in the string. /* The last slash is the first character in the string.
We have to return "/". As a special case we have to * We have to return "/". As a special case we have to
return "//" if there are exactly two slashes at the * return "//" if there are exactly two slashes at the
beginning of the string. See XBD 4.10 Path Name * beginning of the string. See XBD 4.10 Path Name
Resolution for more information. */ * Resolution for more information.
*/
if (last_slash == buf + 1) if (last_slash == buf + 1)
++last_slash; ++last_slash;
else else
@ -310,8 +311,9 @@ xdirname(const char *path)
last_slash[0] = '\0'; last_slash[0] = '\0';
} else { } else {
/* This assignment is ill-designed but the XPG specs require to /* This assignment is ill-designed but the XPG specs require to
return a string containing "." in any case no directory part * return a string containing "." in any case no directory part
is found and so a static and constant string is required. */ * is found and so a static and constant string is required.
*/
buf[0] = '.'; buf[0] = '.';
buf[1] = '\0'; buf[1] = '\0';
} }
@ -323,12 +325,13 @@ xdirname(const char *path)
* Return number of dots of all chars in a string are dots, else 0 * Return number of dots of all chars in a string are dots, else 0
*/ */
static int static int
all_dots(const char* ptr) all_dots(const char *ptr)
{ {
if (!ptr) if (!ptr)
return FALSE; return FALSE;
int count = 0; int count = 0;
while (*ptr == '.') { while (*ptr == '.') {
count++; count++;
ptr++; ptr++;
@ -361,23 +364,24 @@ spawn(char *file, char *arg1, char *arg2, char *dir, unsigned char flag)
status = chdir(dir); status = chdir(dir);
/* Show a marker (to indicate nnn spawned shell) */ /* Show a marker (to indicate nnn spawned shell) */
if (flag & 0b1) if (flag & 0x01)
fprintf(stdout, "\n +-++-++-+\n | n n n |\n +-++-++-+\n\n"); printf("\n +-++-++-+\n | n n n |\n +-++-++-+\n\n");
/* Suppress stdout and stderr */ /* Suppress stdout and stderr */
if (flag & 0b100) { if (flag & 0x04) {
int fd = open("/dev/null", O_WRONLY, S_IWUSR); int fd = open("/dev/null", O_WRONLY, 0200);
dup2(fd, 1); dup2(fd, 1);
dup2(fd, 2); dup2(fd, 2);
close(fd); close(fd);
} }
if (flag & 0b1000) if (flag & 0x08)
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
execlp(file, file, arg1, arg2, NULL); execlp(file, file, arg1, arg2, NULL);
_exit(1); _exit(1);
} else { } else {
if (!(flag & 0b10)) if (!(flag & 0x02))
/* Ignore interruptions */ /* Ignore interruptions */
while (waitpid(pid, &status, 0) == -1) while (waitpid(pid, &status, 0) == -1)
DPRINTF_D(status); DPRINTF_D(status);
@ -422,7 +426,7 @@ xstricmp(char *s1, char *s2)
c2++; c2++;
if (*c2 == '-' || *c2 == '+') if (*c2 == '-' || *c2 == '+')
c2++; c2++;
while(*c2 >= '0' && *c2 <= '9') while (*c2 >= '0' && *c2 <= '9')
c2++; c2++;
if (*c1 == '\0' && *c2 == '\0') { if (*c1 == '\0' && *c2 == '\0') {
@ -445,7 +449,8 @@ xstricmp(char *s1, char *s2)
s1++, s2++; s1++, s2++;
/* In case of alphabetically same names, make sure /* In case of alphabetically same names, make sure
lower case one comes before upper case one */ * lower case one comes before upper case one
*/
if (!*s1 && !*s2) if (!*s1 && !*s2)
return 1; return 1;
@ -557,6 +562,7 @@ initcurses(void)
{ {
if (initscr() == NULL) { if (initscr() == NULL) {
char *term = getenv("TERM"); char *term = getenv("TERM");
if (term != NULL) if (term != NULL)
fprintf(stderr, "error opening terminal: %s\n", term); fprintf(stderr, "error opening terminal: %s\n", term);
else else
@ -665,21 +671,24 @@ fill(struct entry **dents,
static struct entry _dent; static struct entry _dent;
/* Copy count to tmp */ /* Copy count to tmp */
xstrlcpy(_dent.name, (*dents)[count].name, NAME_MAX); xstrlcpy(_dent.name, (*dents)[count].name,
NAME_MAX);
_dent.mode = (*dents)[count].mode; _dent.mode = (*dents)[count].mode;
_dent.t = (*dents)[count].t; _dent.t = (*dents)[count].t;
_dent.size = (*dents)[count].size; _dent.size = (*dents)[count].size;
_dent.bsize = (*dents)[count].bsize; _dent.bsize = (*dents)[count].bsize;
/* Copy ndents - 1 to count */ /* Copy ndents - 1 to count */
xstrlcpy((*dents)[count].name, (*dents)[ndents].name, NAME_MAX); xstrlcpy((*dents)[count].name,
(*dents)[ndents].name, NAME_MAX);
(*dents)[count].mode = (*dents)[ndents].mode; (*dents)[count].mode = (*dents)[ndents].mode;
(*dents)[count].t = (*dents)[ndents].t; (*dents)[count].t = (*dents)[ndents].t;
(*dents)[count].size = (*dents)[ndents].size; (*dents)[count].size = (*dents)[ndents].size;
(*dents)[count].bsize = (*dents)[ndents].bsize; (*dents)[count].bsize = (*dents)[ndents].bsize;
/* Copy tmp to ndents - 1 */ /* Copy tmp to ndents - 1 */
xstrlcpy((*dents)[ndents].name, _dent.name, NAME_MAX); xstrlcpy((*dents)[ndents].name, _dent.name,
NAME_MAX);
(*dents)[ndents].mode = _dent.mode; (*dents)[ndents].mode = _dent.mode;
(*dents)[ndents].t = _dent.t; (*dents)[ndents].t = _dent.t;
(*dents)[ndents].size = _dent.size; (*dents)[ndents].size = _dent.size;
@ -732,7 +741,7 @@ readln(char *path)
while ((r = wget_wch(stdscr, ch)) != ERR) { while ((r = wget_wch(stdscr, ch)) != ERR) {
if (r == OK) { if (r == OK) {
switch(*ch) { switch (*ch) {
case '\r': // with nonl(), this is ENTER key value case '\r': // with nonl(), this is ENTER key value
if (len == 1) { if (len == 1) {
cur = oldcur; cur = oldcur;
@ -780,7 +789,7 @@ readln(char *path)
printprompt(ln); printprompt(ln);
} }
} else { } else {
switch(*ch) { switch (*ch) {
case KEY_DC: // fallthrough case KEY_DC: // fallthrough
case KEY_BACKSPACE: case KEY_BACKSPACE:
if (len == 1) { if (len == 1) {
@ -909,6 +918,7 @@ replace_escape(const char *str)
static char buffer[PATH_MAX]; static char buffer[PATH_MAX];
static wchar_t wbuf[PATH_MAX]; static wchar_t wbuf[PATH_MAX];
static wchar_t *buf; static wchar_t *buf;
buffer[0] = '\0'; buffer[0] = '\0';
buf = wbuf; buf = wbuf;
@ -928,32 +938,32 @@ replace_escape(const char *str)
} }
static void static void
printent(struct entry *ent, int active) printent(struct entry *ent, int sel)
{ {
static int ncols; static int ncols;
if (COLS > PATH_MAX + 16) if (PATH_MAX + 16 < COLS)
ncols = PATH_MAX + 16; ncols = PATH_MAX + 16;
else else
ncols = COLS; ncols = COLS;
if (S_ISDIR(ent->mode)) if (S_ISDIR(ent->mode))
snprintf(g_buf, ncols, "%s%s/", CURSYM(active), snprintf(g_buf, ncols, "%s%s/", CURSYM(sel),
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISLNK(ent->mode)) else if (S_ISLNK(ent->mode))
snprintf(g_buf, ncols, "%s%s@", CURSYM(active), snprintf(g_buf, ncols, "%s%s@", CURSYM(sel),
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISSOCK(ent->mode)) else if (S_ISSOCK(ent->mode))
snprintf(g_buf, ncols, "%s%s=", CURSYM(active), snprintf(g_buf, ncols, "%s%s=", CURSYM(sel),
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISFIFO(ent->mode)) else if (S_ISFIFO(ent->mode))
snprintf(g_buf, ncols, "%s%s|", CURSYM(active), snprintf(g_buf, ncols, "%s%s|", CURSYM(sel),
replace_escape(ent->name)); replace_escape(ent->name));
else if (ent->mode & S_IXUSR) else if (ent->mode & 0100)
snprintf(g_buf, ncols, "%s%s*", CURSYM(active), snprintf(g_buf, ncols, "%s%s*", CURSYM(sel),
replace_escape(ent->name)); replace_escape(ent->name));
else else
snprintf(g_buf, ncols, "%s%s", CURSYM(active), snprintf(g_buf, ncols, "%s%s", CURSYM(sel),
replace_escape(ent->name)); replace_escape(ent->name));
printw("%s\n", g_buf); printw("%s\n", g_buf);
@ -962,7 +972,8 @@ printent(struct entry *ent, int active)
static char* static char*
coolsize(off_t size) coolsize(off_t size)
{ {
static const char *size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"}; static const char * const U[]
= {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
static char size_buf[12]; /* Buffer to hold human readable size */ static char size_buf[12]; /* Buffer to hold human readable size */
static int i; static int i;
static off_t tmp; static off_t tmp;
@ -978,95 +989,95 @@ coolsize(off_t size)
i++; i++;
} }
snprintf(size_buf, 12, "%.*Lf%s", i, size + rem * div_2_pow_10, size_units[i]); snprintf(size_buf, 12, "%.*Lf%s", i, size + rem * div_2_pow_10, U[i]);
return size_buf; return size_buf;
} }
static void static void
printent_long(struct entry *ent, int active) printent_long(struct entry *ent, int sel)
{ {
static int ncols; static int ncols;
static char buf[18]; static char buf[18];
if (COLS > PATH_MAX + 32) if (PATH_MAX + 32 < COLS)
ncols = PATH_MAX + 32; ncols = PATH_MAX + 32;
else else
ncols = COLS; ncols = COLS;
strftime(buf, 18, "%d %m %Y %H:%M", localtime(&ent->t)); strftime(buf, 18, "%d %m %Y %H:%M", localtime(&ent->t));
if (active) if (sel)
attron(A_REVERSE); attron(A_REVERSE);
if (!bsizeorder) { if (!bsizeorder) {
if (S_ISDIR(ent->mode)) if (S_ISDIR(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s / %s/", snprintf(g_buf, ncols, "%s%-16.16s / %s/",
CURSYM(active), buf, replace_escape(ent->name)); CURSYM(sel), buf, replace_escape(ent->name));
else if (S_ISLNK(ent->mode)) else if (S_ISLNK(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s @ %s@", snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
CURSYM(active), buf, replace_escape(ent->name)); CURSYM(sel), buf, replace_escape(ent->name));
else if (S_ISSOCK(ent->mode)) else if (S_ISSOCK(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s = %s=", snprintf(g_buf, ncols, "%s%-16.16s = %s=",
CURSYM(active), buf, replace_escape(ent->name)); CURSYM(sel), buf, replace_escape(ent->name));
else if (S_ISFIFO(ent->mode)) else if (S_ISFIFO(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s | %s|", snprintf(g_buf, ncols, "%s%-16.16s | %s|",
CURSYM(active), buf, replace_escape(ent->name)); CURSYM(sel), buf, replace_escape(ent->name));
else if (S_ISBLK(ent->mode)) else if (S_ISBLK(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s b %s", snprintf(g_buf, ncols, "%s%-16.16s b %s",
CURSYM(active), buf, replace_escape(ent->name)); CURSYM(sel), buf, replace_escape(ent->name));
else if (S_ISCHR(ent->mode)) else if (S_ISCHR(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s c %s", snprintf(g_buf, ncols, "%s%-16.16s c %s",
CURSYM(active), buf, replace_escape(ent->name)); CURSYM(sel), buf, replace_escape(ent->name));
else if (ent->mode & S_IXUSR) else if (ent->mode & 0100)
snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*", snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
CURSYM(active), buf, coolsize(ent->size), CURSYM(sel), buf, coolsize(ent->size),
replace_escape(ent->name)); replace_escape(ent->name));
else else
snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s", snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
CURSYM(active), buf, coolsize(ent->size), CURSYM(sel), buf, coolsize(ent->size),
replace_escape(ent->name)); replace_escape(ent->name));
} else { } else {
if (S_ISDIR(ent->mode)) if (S_ISDIR(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s %8.8s/ %s/", snprintf(g_buf, ncols, "%s%-16.16s %8.8s/ %s/",
CURSYM(active), buf, coolsize(ent->bsize << 9), CURSYM(sel), buf, coolsize(ent->bsize << 9),
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISLNK(ent->mode)) else if (S_ISLNK(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s @ %s@", snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
CURSYM(active), buf, CURSYM(sel), buf,
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISSOCK(ent->mode)) else if (S_ISSOCK(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s = %s=", snprintf(g_buf, ncols, "%s%-16.16s = %s=",
CURSYM(active), buf, CURSYM(sel), buf,
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISFIFO(ent->mode)) else if (S_ISFIFO(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s | %s|", snprintf(g_buf, ncols, "%s%-16.16s | %s|",
CURSYM(active), buf, CURSYM(sel), buf,
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISBLK(ent->mode)) else if (S_ISBLK(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s b %s", snprintf(g_buf, ncols, "%s%-16.16s b %s",
CURSYM(active), buf, CURSYM(sel), buf,
replace_escape(ent->name)); replace_escape(ent->name));
else if (S_ISCHR(ent->mode)) else if (S_ISCHR(ent->mode))
snprintf(g_buf, ncols, "%s%-16.16s c %s", snprintf(g_buf, ncols, "%s%-16.16s c %s",
CURSYM(active), buf, CURSYM(sel), buf,
replace_escape(ent->name)); replace_escape(ent->name));
else if (ent->mode & S_IXUSR) else if (ent->mode & 0100)
snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*", snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
CURSYM(active), buf, coolsize(ent->bsize << 9), CURSYM(sel), buf, coolsize(ent->bsize << 9),
replace_escape(ent->name)); replace_escape(ent->name));
else else
snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s", snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
CURSYM(active), buf, coolsize(ent->bsize << 9), CURSYM(sel), buf, coolsize(ent->bsize << 9),
replace_escape(ent->name)); replace_escape(ent->name));
} }
printw("%s\n", g_buf); printw("%s\n", g_buf);
if (active) if (sel)
attroff(A_REVERSE); attroff(A_REVERSE);
} }
static void (*printptr)(struct entry *ent, int active) = &printent_long; static void (*printptr)(struct entry *ent, int sel) = &printent_long;
static char static char
get_fileind(mode_t mode, char *desc) get_fileind(mode_t mode, char *desc)
@ -1076,7 +1087,7 @@ get_fileind(mode_t mode, char *desc)
if (S_ISREG(mode)) { if (S_ISREG(mode)) {
c = '-'; c = '-';
sprintf(desc, "%s", "regular file"); sprintf(desc, "%s", "regular file");
if (mode & S_IXUSR) if (mode & 0100)
strcat(desc, ", executable"); strcat(desc, ", executable");
} else if (S_ISDIR(mode)) { } else if (S_ISDIR(mode)) {
c = 'd'; c = 'd';
@ -1114,14 +1125,14 @@ get_fileind(mode_t mode, char *desc)
desc[0] = '\0'; desc[0] = '\0';
} }
return(c); return c;
} }
/* Convert a mode field into "ls -l" type perms field. */ /* Convert a mode field into "ls -l" type perms field. */
static char * static char *
get_lsperms(mode_t mode, char *desc) get_lsperms(mode_t mode, char *desc)
{ {
static const char *rwx[] = {"---", "--x", "-w-", "-wx", static const char * const rwx[] = {"---", "--x", "-w-", "-wx",
"r--", "r-x", "rw-", "rwx"}; "r--", "r-x", "rw-", "rwx"};
static char bits[11]; static char bits[11];
@ -1131,15 +1142,15 @@ get_lsperms(mode_t mode, char *desc)
strcpy(&bits[7], rwx[(mode & 7)]); strcpy(&bits[7], rwx[(mode & 7)]);
if (mode & S_ISUID) if (mode & S_ISUID)
bits[3] = (mode & S_IXUSR) ? 's' : 'S'; bits[3] = (mode & 0100) ? 's' : 'S'; /* user executable */
if (mode & S_ISGID) if (mode & S_ISGID)
bits[6] = (mode & S_IXGRP) ? 's' : 'l'; bits[6] = (mode & 0010) ? 's' : 'l'; /* group executable */
if (mode & S_ISVTX) if (mode & S_ISVTX)
bits[9] = (mode & S_IXOTH) ? 't' : 'T'; bits[9] = (mode & 0001) ? 't' : 'T'; /* others executable */
bits[10] = '\0'; bits[10] = '\0';
return(bits); return bits;
} }
/* /*
@ -1149,11 +1160,12 @@ get_lsperms(mode_t mode, char *desc)
* If pager is valid, returns NULL * If pager is valid, returns NULL
*/ */
static char * static char *
get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2, int pager) get_output(char *buf, size_t bytes, char *file,
char *arg1, char *arg2, int pager)
{ {
pid_t pid; pid_t pid;
int pipefd[2]; int pipefd[2];
FILE* pf; FILE *pf;
int status; int status;
char *ret = NULL; char *ret = NULL;
@ -1174,7 +1186,8 @@ get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2, int page
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
close(pipefd[1]); close(pipefd[1]);
if (!pager) { if (!pager) {
if ((pf = fdopen(pipefd[0], "r"))) { pf = fdopen(pipefd[0], "r");
if (pf) {
ret = fgets(buf, bytes, pf); ret = fgets(buf, bytes, pf);
close(pipefd[0]); close(pipefd[0]);
} }
@ -1201,13 +1214,13 @@ get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2, int page
* Follows the stat(1) output closely * Follows the stat(1) output closely
*/ */
static int static int
show_stats(char* fpath, char* fname, struct stat *sb) show_stats(char *fpath, char *fname, struct stat *sb)
{ {
char *perms = get_lsperms(sb->st_mode, g_buf); char *perms = get_lsperms(sb->st_mode, g_buf);
char *p, *begin = g_buf; char *p, *begin = g_buf;
char tmp[] = "/tmp/nnnXXXXXX"; char tmp[] = "/tmp/nnnXXXXXX";
int fd = mkstemp(tmp); int fd = mkstemp(tmp);
if (fd == -1) if (fd == -1)
return -1; return -1;
@ -1215,6 +1228,7 @@ show_stats(char* fpath, char* fname, struct stat *sb)
if (perms[0] == 'l') { if (perms[0] == 'l') {
/* Note that MAX_CMD_LEN > PATH_MAX */ /* Note that MAX_CMD_LEN > PATH_MAX */
ssize_t len = readlink(fpath, g_buf, MAX_CMD_LEN); ssize_t len = readlink(fpath, g_buf, MAX_CMD_LEN);
if (len != -1) { if (len != -1) {
g_buf[len] = '\0'; g_buf[len] = '\0';
dprintf(fd, " File: '%s' -> ", dprintf(fd, " File: '%s' -> ",
@ -1296,9 +1310,9 @@ show_stats(char* fpath, char* fname, struct stat *sb)
} }
static int static int
show_mediainfo(char* fpath, char *arg) show_mediainfo(char *fpath, char *arg)
{ {
if (get_output(g_buf, MAX_CMD_LEN, "which", "mediainfo", NULL, 0) == NULL) if (!get_output(g_buf, MAX_CMD_LEN, "which", "mediainfo", NULL, 0))
return -1; return -1;
exitcurses(); exitcurses();
@ -1312,10 +1326,6 @@ static int
show_help(void) show_help(void)
{ {
char tmp[] = "/tmp/nnnXXXXXX"; char tmp[] = "/tmp/nnnXXXXXX";
int i = 0, fd = mkstemp(tmp);
if (fd == -1)
return -1;
static char helpstr[] = ("\ static char helpstr[] = ("\
Key | Function\n\ Key | Function\n\
-+-\n\ -+-\n\
@ -1353,13 +1363,19 @@ show_help(void)
Q | Quit and change directory\n\ Q | Quit and change directory\n\
q, ^Q | Quit\n\n\n"); q, ^Q | Quit\n\n\n");
int i = 0, fd = mkstemp(tmp);
if (fd == -1)
return -1;
dprintf(fd, "%s", helpstr); dprintf(fd, "%s", helpstr);
if (getenv("NNN_BMS")) { if (getenv("NNN_BMS")) {
dprintf(fd, "BOOKMARKS\n"); dprintf(fd, "BOOKMARKS\n");
for (; i < MAX_BM; i++) for (; i < MAX_BM; i++)
if (bookmark[i].key) if (bookmark[i].key)
dprintf(fd, " %s: %s\n", bookmark[i].key, bookmark[i].loc); dprintf(fd, " %s: %s\n",
bookmark[i].key, bookmark[i].loc);
else else
break; break;
dprintf(fd, "\n"); dprintf(fd, "\n");
@ -1386,7 +1402,8 @@ show_help(void)
} }
static int static int
sum_bsizes(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) sum_bsizes(const char *fpath, const struct stat *sb,
int typeflag, struct FTW *ftwbuf)
{ {
if (typeflag == FTW_F || typeflag == FTW_D) if (typeflag == FTW_F || typeflag == FTW_D)
blk_size += sb->st_blocks; blk_size += sb->st_blocks;
@ -1436,6 +1453,7 @@ dentfill(char *path, struct entry **dents,
static struct dirent *dp; static struct dirent *dp;
static struct stat sb; static struct stat sb;
static int n; static int n;
n = 0; n = 0;
dirp = opendir(path); dirp = opendir(path);
@ -1473,7 +1491,8 @@ dentfill(char *path, struct entry **dents,
if (bsizeorder) { if (bsizeorder) {
if (S_ISDIR(sb.st_mode)) { if (S_ISDIR(sb.st_mode)) {
blk_size = 0; blk_size = 0;
if (nftw(newpath, sum_bsizes, open_max, FTW_MOUNT | FTW_PHYS) == -1) { if (nftw(newpath, sum_bsizes, open_max,
FTW_MOUNT | FTW_PHYS) == -1) {
printmsg("nftw(3) failed"); printmsg("nftw(3) failed");
(*dents)[n].bsize = sb.st_blocks; (*dents)[n].bsize = sb.st_blocks;
} else } else
@ -1487,6 +1506,7 @@ dentfill(char *path, struct entry **dents,
if (bsizeorder) { if (bsizeorder) {
static struct statvfs svb; static struct statvfs svb;
if (statvfs(path, &svb) == -1) if (statvfs(path, &svb) == -1)
fs_free = 0; fs_free = 0;
else else
@ -1594,6 +1614,7 @@ redraw(char *path)
printptr(&dents[i], i == cur); printptr(&dents[i], i == cur);
} else { } else {
static int odd; static int odd;
odd = ISODD(nlines); odd = ISODD(nlines);
nlines >>= 1; nlines >>= 1;
for (i = cur - nlines; i < cur + nlines + odd; i++) for (i = cur - nlines; i < cur + nlines + odd; i++)
@ -1620,7 +1641,7 @@ redraw(char *path)
ind[0] = '='; ind[0] = '=';
else if (S_ISFIFO(dents[cur].mode)) else if (S_ISFIFO(dents[cur].mode))
ind[0] = '|'; ind[0] = '|';
else if (dents[cur].mode & S_IXUSR) else if (dents[cur].mode & 0100)
ind[0] = '*'; ind[0] = '*';
else else
ind[0] = '\0'; ind[0] = '\0';
@ -1629,7 +1650,8 @@ redraw(char *path)
sprintf(cwd, "total %d %s[%s%s]", ndents, sort, sprintf(cwd, "total %d %s[%s%s]", ndents, sort,
replace_escape(dents[cur].name), ind); replace_escape(dents[cur].name), ind);
else else
sprintf(cwd, "total %d by disk usage, %s free [%s%s]", sprintf(cwd,
"total %d by disk usage, %s free [%s%s]",
ndents, coolsize(fs_free), ndents, coolsize(fs_free),
replace_escape(dents[cur].name), ind); replace_escape(dents[cur].name), ind);
@ -1680,10 +1702,13 @@ nochange:
case SEL_CDQUIT: case SEL_CDQUIT:
{ {
char *tmpfile = "/tmp/nnn"; char *tmpfile = "/tmp/nnn";
if ((tmp = getenv("NNN_TMPFILE")) != NULL)
tmp = getenv("NNN_TMPFILE");
if (tmp)
tmpfile = tmp; tmpfile = tmp;
FILE *fp = fopen(tmpfile, "w"); FILE *fp = fopen(tmpfile, "w");
if (fp) { if (fp) {
fprintf(fp, "cd \"%s\"", path); fprintf(fp, "cd \"%s\"", path);
fclose(fp); fclose(fp);
@ -1760,24 +1785,31 @@ nochange:
goto begin; goto begin;
case S_IFREG: case S_IFREG:
{ {
/* If NNN_USE_EDITOR is set, open text in EDITOR */ /* If NNN_USE_EDITOR is set,
* open text in EDITOR
*/
if (editor) { if (editor) {
mime = getmime(dents[cur].name); mime = getmime(dents[cur].name);
if (mime) { if (mime) {
exitcurses(); exitcurses();
spawn(editor, newpath, NULL, NULL, 0); spawn(editor, newpath, NULL,
NULL, 0);
initcurses(); initcurses();
continue; continue;
} }
/* Recognize and open plain text files with vi */ /* Recognize and open plain
if (get_output(g_buf, MAX_CMD_LEN, "file", "-bi", * text files with vi
*/
if (get_output(g_buf, MAX_CMD_LEN,
"file", "-bi",
newpath, 0) == NULL) newpath, 0) == NULL)
continue; continue;
if (strstr(g_buf, "text/") == g_buf) { if (strstr(g_buf, "text/") == g_buf) {
exitcurses(); exitcurses();
spawn(editor, newpath, NULL, NULL, 0); spawn(editor, newpath, NULL,
NULL, 0);
initcurses(); initcurses();
continue; continue;
} }
@ -1797,7 +1829,8 @@ nochange:
DPRINTF_S(fltr); DPRINTF_S(fltr);
/* Save current */ /* Save current */
if (ndents > 0) if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
goto nochange; goto nochange;
case SEL_MFLTR: case SEL_MFLTR:
filtermode = !filtermode; filtermode = !filtermode;
@ -1867,7 +1900,7 @@ nochange:
if (tmp[0] == '\0') if (tmp[0] == '\0')
break; break;
else
/* Add to readline(3) history */ /* Add to readline(3) history */
add_history(tmp); add_history(tmp);
@ -1883,8 +1916,10 @@ nochange:
if (tmp[0] == '~') { if (tmp[0] == '~') {
/* Expand ~ to HOME absolute path */ /* Expand ~ to HOME absolute path */
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home) if (home)
snprintf(newpath, PATH_MAX, "%s%s", home, tmp + 1); snprintf(newpath, PATH_MAX, "%s%s",
home, tmp + 1);
else { else {
free(input); free(input);
printmsg("HOME not set"); printmsg("HOME not set");
@ -1911,8 +1946,7 @@ nochange:
for (fd = 0; fd < r; fd++) { for (fd = 0; fd < r; fd++) {
/* Reached / ? */ /* Reached / ? */
if (strcmp(path, "/") == 0 || if (path[0] == '/' && path[1] == '\0') {
strchr(path, '/') == NULL) {
/* If it's a cd .. at / */ /* If it's a cd .. at / */
if (fd == 0) { if (fd == 0) {
printmsg("You are at /"); printmsg("You are at /");
@ -1922,7 +1956,8 @@ nochange:
/* Can't cd beyond / anyway */ /* Can't cd beyond / anyway */
break; break;
} else { }
dir = xdirname(dir); dir = xdirname(dir);
if (canopendir(dir) == 0) { if (canopendir(dir) == 0) {
printwarn(); printwarn();
@ -1930,12 +1965,12 @@ nochange:
goto nochange; goto nochange;
} }
} }
}
truecd = 1; truecd = 1;
/* Save the path in case of cd .. /* Save the path in case of cd ..
We mark the current dir in parent dir */ * We mark the current dir in parent dir
*/
if (r == 1) { if (r == 1) {
xstrlcpy(oldpath, path, PATH_MAX); xstrlcpy(oldpath, path, PATH_MAX);
truecd = 2; truecd = 2;
@ -1945,19 +1980,18 @@ nochange:
} else } else
mkpath(path, tmp, newpath, PATH_MAX); mkpath(path, tmp, newpath, PATH_MAX);
free(input);
if (canopendir(newpath) == 0) { if (canopendir(newpath) == 0) {
printwarn(); printwarn();
free(input);
break; break;
} }
if (truecd == 0) { if (truecd == 0) {
/* Probable change in dir */ /* Probable change in dir */
/* No-op if it's the same directory */ /* No-op if it's the same directory */
if (strcmp(path, newpath) == 0) { if (strcmp(path, newpath) == 0)
free(input);
break; break;
}
oldpath[0] = '\0'; oldpath[0] = '\0';
} else if (truecd == 1) } else if (truecd == 1)
@ -1973,7 +2007,6 @@ nochange:
/* Reset filter */ /* Reset filter */
xstrlcpy(fltr, ifilter, LINE_MAX); xstrlcpy(fltr, ifilter, LINE_MAX);
DPRINTF_S(path); DPRINTF_S(path);
free(input);
if (filtermode) if (filtermode)
presel = FILTER; presel = FILTER;
goto begin; goto begin;
@ -2056,16 +2089,23 @@ nochange:
for (r = 0; bookmark[r].key && r < MAX_BM; r++) { for (r = 0; bookmark[r].key && r < MAX_BM; r++) {
if (strcmp(bookmark[r].key, tmp) == 0) { if (strcmp(bookmark[r].key, tmp) == 0) {
if (bookmark[r].loc[0] == '~') { if (bookmark[r].loc[0] == '~') {
/* Expand ~ to HOME absolute path */ /* Expand ~ to HOME */
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home) if (home)
snprintf(newpath, PATH_MAX, "%s%s", home, bookmark[r].loc + 1); snprintf(newpath,
PATH_MAX,
"%s%s",
home,
bookmark[r].loc
+ 1);
else { else {
printmsg("HOME not set"); printmsg("HOME not set");
goto nochange; goto nochange;
} }
} else } else
mkpath(path, bookmark[r].loc, newpath, PATH_MAX); mkpath(path, bookmark[r].loc,
newpath, PATH_MAX);
if (canopendir(newpath) == 0) { if (canopendir(newpath) == 0) {
printwarn(); printwarn();
@ -2109,14 +2149,16 @@ nochange:
: (printptr = &printent); : (printptr = &printent);
/* Save current */ /* Save current */
if (ndents > 0) if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
goto begin; goto begin;
case SEL_STATS: case SEL_STATS:
{ {
struct stat sb; struct stat sb;
if (ndents > 0) { if (ndents > 0) {
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
r = lstat(oldpath, &sb); r = lstat(oldpath, &sb);
if (r == -1) { if (r == -1) {
@ -2125,7 +2167,8 @@ nochange:
printerr(1, "lstat"); printerr(1, "lstat");
} else { } else {
exitcurses(); exitcurses();
r = show_stats(oldpath, dents[cur].name, &sb); r = show_stats(oldpath, dents[cur].name,
&sb);
initcurses(); initcurses();
if (r < 0) { if (r < 0) {
printmsg(strerror(errno)); printmsg(strerror(errno));
@ -2138,9 +2181,10 @@ nochange:
} }
case SEL_MEDIA: case SEL_MEDIA:
if (ndents > 0) { if (ndents > 0) {
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
if(show_mediainfo(oldpath, NULL) == -1) { if (show_mediainfo(oldpath, NULL) == -1) {
printmsg("mediainfo missing"); printmsg("mediainfo missing");
goto nochange; goto nochange;
} }
@ -2148,9 +2192,10 @@ nochange:
break; break;
case SEL_FMEDIA: case SEL_FMEDIA:
if (ndents > 0) { if (ndents > 0) {
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
if(show_mediainfo(oldpath, "-f") == -1) { if (show_mediainfo(oldpath, "-f") == -1) {
printmsg("mediainfo missing"); printmsg("mediainfo missing");
goto nochange; goto nochange;
} }
@ -2162,7 +2207,7 @@ nochange:
goto nochange; goto nochange;
} }
spawn(desktop_manager, path, NULL, path, 0b110); spawn(desktop_manager, path, NULL, path, 0x06);
break; break;
case SEL_FSIZE: case SEL_FSIZE:
sizeorder = !sizeorder; sizeorder = !sizeorder;
@ -2170,7 +2215,8 @@ nochange:
bsizeorder = 0; bsizeorder = 0;
/* Save current */ /* Save current */
if (ndents > 0) if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
goto begin; goto begin;
case SEL_BSIZE: case SEL_BSIZE:
bsizeorder = !bsizeorder; bsizeorder = !bsizeorder;
@ -2182,7 +2228,8 @@ nochange:
sizeorder = 0; sizeorder = 0;
/* Save current */ /* Save current */
if (ndents > 0) if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
goto begin; goto begin;
case SEL_MTIME: case SEL_MTIME:
mtimeorder = !mtimeorder; mtimeorder = !mtimeorder;
@ -2190,12 +2237,14 @@ nochange:
bsizeorder = 0; bsizeorder = 0;
/* Save current */ /* Save current */
if (ndents > 0) if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
goto begin; goto begin;
case SEL_REDRAW: case SEL_REDRAW:
/* Save current */ /* Save current */
if (ndents > 0) if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, PATH_MAX); mkpath(path, dents[cur].name, oldpath,
PATH_MAX);
goto begin; goto begin;
case SEL_COPY: case SEL_COPY:
if (copier && ndents) { if (copier && ndents) {
@ -2242,7 +2291,7 @@ nochange:
static void static void
usage(void) usage(void)
{ {
fprintf(stdout, "usage: nnn [-l] [-i] [-p custom_nlay] [-S] [-v] [-h] [PATH]\n\n\ printf("usage: nnn [-l] [-i] [-p custom_nlay] [-S] [-v] [-h] [PATH]\n\n\
The missing terminal file browser for X.\n\n\ The missing terminal file browser for X.\n\n\
positional arguments:\n\ positional arguments:\n\
PATH directory to open [default: current dir]\n\n\ PATH directory to open [default: current dir]\n\n\
@ -2264,7 +2313,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char cwd[PATH_MAX], *ipath; char cwd[PATH_MAX], *ipath;
char *ifilter; char *ifilter, *bmstr;
int opt = 0; int opt = 0;
/* Confirm we are in a terminal */ /* Confirm we are in a terminal */
@ -2289,7 +2338,7 @@ main(int argc, char *argv[])
player = optarg; player = optarg;
break; break;
case 'v': case 'v':
fprintf(stdout, "%s\n", VERSION); printf("%s\n", VERSION);
return 0; return 0;
case 'd': case 'd':
fprintf(stderr, "Option -d is deprecated and will be removed, detail view mode is default now.\n"); fprintf(stderr, "Option -d is deprecated and will be removed, detail view mode is default now.\n");
@ -2320,9 +2369,9 @@ main(int argc, char *argv[])
initfilter(showhidden, &ifilter); initfilter(showhidden, &ifilter);
/* Parse bookmarks string, if available */ /* Parse bookmarks string, if available */
char *bms = getenv("NNN_BMS"); bmstr = getenv("NNN_BMS");
if (bms) if (bmstr)
parsebmstr(bms); parsebmstr(bmstr);
/* Edit text in EDITOR, if opted */ /* Edit text in EDITOR, if opted */
if (getenv("NNN_USE_EDITOR")) if (getenv("NNN_USE_EDITOR"))