Optimizations

This commit is contained in:
Arun Prakash Jana 2017-04-01 14:41:07 +05:30
parent f3d95b6d0d
commit 7be7a6d215
No known key found for this signature in database
GPG key ID: A75979F35C080412
3 changed files with 98 additions and 116 deletions

View file

@ -5,10 +5,11 @@ MANPREFIX = $(PREFIX)/man
#CPPFLAGS = -DDEBUG #CPPFLAGS = -DDEBUG
#CFLAGS = -g #CFLAGS = -g
CFLAGS = -O3 -march=native CFLAGS = -O3 -march=native -fno-asynchronous-unwind-tables -fdata-sections \
-ffunction-sections -Wl,--gc-sections
LDLIBS = -lcurses LDLIBS = -lcurses
DISTFILES = nnn.c strlcat.c strlcpy.c util.h config.def.h\ DISTFILES = nnn.c strlcat.c strlcpy.c util.h config.def.h \
nnn.1 Makefile README.md LICENSE nnn.1 Makefile README.md LICENSE
OBJ = nnn.o strlcat.o strlcpy.o OBJ = nnn.o strlcat.o strlcpy.o
BIN = nnn BIN = nnn
@ -17,7 +18,9 @@ all: $(BIN)
$(BIN): $(OBJ) $(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(LDLIBS) $(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(LDLIBS)
strip $(BIN) strip -S --strip-unneeded --remove-section=.note.gnu.gold-version \
--remove-section=.comment --remove-section=.note \
--remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag $(BIN)
nnn.o: util.h config.h nnn.o: util.h config.h
strlcat.o: util.h strlcat.o: util.h

View file

@ -3,12 +3,12 @@
#define CURSR " > " #define CURSR " > "
#define EMPTY " " #define EMPTY " "
int mtimeorder = 0; /* Set to 1 to sort by time modified */ static int mtimeorder = 0; /* Set to 1 to sort by time modified */
int sizeorder = 0; /* Set to 1 to sort by file size */ static int sizeorder = 0; /* Set to 1 to sort by file size */
int idletimeout = 0; /* Screensaver timeout in seconds, 0 to disable */ static int idletimeout = 0; /* Screensaver timeout in seconds, 0 to disable */
int showhidden = 0; /* Set to 1 to show hidden files by default */ static int showhidden = 0; /* Set to 1 to show hidden files by default */
int showdetail = 0; /* Set to show additional file info */ static int showdetail = 0; /* Set to show additional file info */
char *idlecmd = "rain"; /* The screensaver program */ static char *idlecmd = "rain"; /* The screensaver program */
struct assoc assocs[] = { struct assoc assocs[] = {
//{ "\\.(avi|mp4|mkv|mp3|ogg|flac|mov)$", "mpv" }, //{ "\\.(avi|mp4|mkv|mp3|ogg|flac|mov)$", "mpv" },
@ -23,60 +23,60 @@ struct assoc assocs[] = {
struct key bindings[] = { struct key bindings[] = {
/* Quit */ /* Quit */
{ 'q', SEL_QUIT }, { 'q', SEL_QUIT, "\0", "\0" },
/* Back */ /* Back */
{ KEY_BACKSPACE, SEL_BACK }, { KEY_BACKSPACE, SEL_BACK, "\0", "\0" },
{ KEY_LEFT, SEL_BACK }, { KEY_LEFT, SEL_BACK, "\0", "\0" },
{ 'h', SEL_BACK }, { 'h', SEL_BACK, "\0", "\0" },
{ CONTROL('H'), SEL_BACK }, { CONTROL('H'), SEL_BACK, "\0", "\0" },
/* Inside */ /* Inside */
{ KEY_ENTER, SEL_GOIN }, { KEY_ENTER, SEL_GOIN, "\0", "\0" },
{ '\r', SEL_GOIN }, { '\r', SEL_GOIN, "\0", "\0" },
{ KEY_RIGHT, SEL_GOIN }, { KEY_RIGHT, SEL_GOIN, "\0", "\0" },
{ 'l', SEL_GOIN }, { 'l', SEL_GOIN, "\0", "\0" },
/* Filter */ /* Filter */
{ '/', SEL_FLTR }, { '/', SEL_FLTR, "\0", "\0" },
{ '&', SEL_FLTR }, { '&', SEL_FLTR, "\0", "\0" },
/* Next */ /* Next */
{ 'j', SEL_NEXT }, { 'j', SEL_NEXT, "\0", "\0" },
{ KEY_DOWN, SEL_NEXT }, { KEY_DOWN, SEL_NEXT, "\0", "\0" },
{ CONTROL('N'), SEL_NEXT }, { CONTROL('N'), SEL_NEXT, "\0", "\0" },
/* Previous */ /* Previous */
{ 'k', SEL_PREV }, { 'k', SEL_PREV, "\0", "\0" },
{ KEY_UP, SEL_PREV }, { KEY_UP, SEL_PREV, "\0", "\0" },
{ CONTROL('P'), SEL_PREV }, { CONTROL('P'), SEL_PREV, "\0", "\0" },
/* Page down */ /* Page down */
{ KEY_NPAGE, SEL_PGDN }, { KEY_NPAGE, SEL_PGDN, "\0", "\0" },
{ CONTROL('D'), SEL_PGDN }, { CONTROL('D'), SEL_PGDN, "\0", "\0" },
/* Page up */ /* Page up */
{ KEY_PPAGE, SEL_PGUP }, { KEY_PPAGE, SEL_PGUP, "\0", "\0" },
{ CONTROL('U'), SEL_PGUP }, { CONTROL('U'), SEL_PGUP, "\0", "\0" },
/* Home */ /* Home */
{ KEY_HOME, SEL_HOME }, { KEY_HOME, SEL_HOME, "\0", "\0" },
{ CONTROL('A'), SEL_HOME }, { CONTROL('A'), SEL_HOME, "\0", "\0" },
{ '^', SEL_HOME }, { '^', SEL_HOME, "\0", "\0" },
/* End */ /* End */
{ KEY_END, SEL_END }, { KEY_END, SEL_END, "\0", "\0" },
{ CONTROL('E'), SEL_END }, { CONTROL('E'), SEL_END, "\0", "\0" },
{ '$', SEL_END }, { '$', SEL_END, "\0", "\0" },
/* Change dir */ /* Change dir */
{ 'c', SEL_CD }, { 'c', SEL_CD, "\0", "\0" },
{ '~', SEL_CDHOME }, { '~', SEL_CDHOME, "\0", "\0" },
/* Toggle hide .dot files */ /* Toggle hide .dot files */
{ '.', SEL_TOGGLEDOT }, { '.', SEL_TOGGLEDOT, "\0", "\0" },
/* Detailed listing */ /* Detailed listing */
{ 'd', SEL_DETAIL }, { 'd', SEL_DETAIL, "\0", "\0" },
/* Toggle sort by size */ /* Toggle sort by size */
{ 's', SEL_FSIZE }, { 's', SEL_FSIZE, "\0", "\0" },
/* Toggle sort by time */ /* Toggle sort by time */
{ 't', SEL_MTIME }, { 't', SEL_MTIME, "\0", "\0" },
{ CONTROL('L'), SEL_REDRAW }, { CONTROL('L'), SEL_REDRAW, "\0", "\0" },
/* Copy currently selected file path */ /* Copy currently selected file path */
{ CONTROL('K'), SEL_COPY }, { CONTROL('K'), SEL_COPY, "\0", "\0" },
/* Run command */ /* Run command */
{ 'z', SEL_RUN, "top" }, { 'z', SEL_RUN, "top", "\0" },
{ '!', SEL_RUN, "sh", "SHELL" }, { '!', SEL_RUN, "sh", "SHELL" },
/* Run command with argument */ /* Run command with argument */
{ 'e', SEL_RUNARG, "vi", "EDITOR" }, { 'e', SEL_RUNARG, "vi", "EDITOR" },
{ 'p', SEL_RUNARG, "less", "PAGER" }, { 'p', SEL_RUNARG, "less", "PAGER" },
}; };

115
nnn.c
View file

@ -90,13 +90,13 @@ typedef struct entry {
} *pEntry; } *pEntry;
/* Global context */ /* Global context */
struct entry *dents; static struct entry *dents;
int ndents, cur; static int ndents, cur;
int idle; static int idle;
char *opener = NULL; static char *opener = NULL;
char *fallback_opener = NULL; static char *fallback_opener = NULL;
char *copier = NULL; static char *copier = NULL;
const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"}; static const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
/* /*
* Layout: * Layout:
@ -115,12 +115,12 @@ const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
* '------ * '------
*/ */
void printmsg(char *); static void printmsg(char *);
void printwarn(void); static void printwarn(void);
void printerr(int, char *); static void printerr(int, char *);
#undef dprintf #undef dprintf
int static int
dprintf(int fd, const char *fmt, ...) dprintf(int fd, const char *fmt, ...)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
@ -135,18 +135,7 @@ dprintf(int fd, const char *fmt, ...)
return r; return r;
} }
void * static void *
xmalloc(size_t size)
{
void *p;
p = malloc(size);
if (p == NULL)
printerr(1, "malloc");
return p;
}
void *
xrealloc(void *p, size_t size) xrealloc(void *p, size_t size)
{ {
p = realloc(p, size); p = realloc(p, size);
@ -155,20 +144,9 @@ xrealloc(void *p, size_t size)
return p; return p;
} }
char *
xstrdup(const char *s)
{
char *p;
p = strdup(s);
if (p == NULL)
printerr(1, "strdup");
return p;
}
/* Some implementations of dirname(3) may modify `path' and some /* Some implementations of dirname(3) may modify `path' and some
* return a pointer inside `path'. */ * return a pointer inside `path'. */
char * static char *
xdirname(const char *path) xdirname(const char *path)
{ {
static char out[PATH_MAX]; static char out[PATH_MAX];
@ -182,7 +160,7 @@ xdirname(const char *path)
return out; return out;
} }
void static void
spawn(char *file, char *arg, char *dir) spawn(char *file, char *arg, char *dir)
{ {
pid_t pid; pid_t pid;
@ -202,7 +180,7 @@ spawn(char *file, char *arg, char *dir)
} }
} }
char * static char *
xgetenv(char *name, char *fallback) xgetenv(char *name, char *fallback)
{ {
char *value; char *value;
@ -213,7 +191,7 @@ xgetenv(char *name, char *fallback)
return value && value[0] ? value : fallback; return value && value[0] ? value : fallback;
} }
int static int
xstricmp(const char *s1, const char *s2) xstricmp(const char *s1, const char *s2)
{ {
while (*s2 != 0 && TOUPPER(*s1) == TOUPPER(*s2)) while (*s2 != 0 && TOUPPER(*s1) == TOUPPER(*s2))
@ -226,12 +204,12 @@ xstricmp(const char *s1, const char *s2)
return (int) (TOUPPER(*s1) - TOUPPER(*s2)); return (int) (TOUPPER(*s1) - TOUPPER(*s2));
} }
char * static char *
openwith(char *file) openwith(char *file)
{ {
regex_t regex; regex_t regex;
char *bin = NULL; char *bin = NULL;
int i; unsigned int i;
for (i = 0; i < LEN(assocs); i++) { for (i = 0; i < LEN(assocs); i++) {
if (regcomp(&regex, assocs[i].regex, if (regcomp(&regex, assocs[i].regex,
@ -246,7 +224,7 @@ openwith(char *file)
return bin; return bin;
} }
int static int
setfilter(regex_t *regex, char *filter) setfilter(regex_t *regex, char *filter)
{ {
char errbuf[LINE_MAX]; char errbuf[LINE_MAX];
@ -264,19 +242,19 @@ setfilter(regex_t *regex, char *filter)
return r; return r;
} }
void static void
initfilter(int dot, char **ifilter) initfilter(int dot, char **ifilter)
{ {
*ifilter = dot ? "." : "^[^.]"; *ifilter = dot ? "." : "^[^.]";
} }
int static int
visible(regex_t *regex, char *file) visible(regex_t *regex, char *file)
{ {
return regexec(regex, file, 0, NULL, 0) == 0; return regexec(regex, file, 0, NULL, 0) == 0;
} }
int static int
entrycmp(const void *va, const void *vb) entrycmp(const void *va, const void *vb)
{ {
if (mtimeorder) if (mtimeorder)
@ -288,7 +266,7 @@ entrycmp(const void *va, const void *vb)
return xstricmp(((pEntry)va)->name, ((pEntry)vb)->name); return xstricmp(((pEntry)va)->name, ((pEntry)vb)->name);
} }
void static void
initcurses(void) initcurses(void)
{ {
if (initscr() == NULL) { if (initscr() == NULL) {
@ -308,14 +286,14 @@ initcurses(void)
timeout(1000); /* One second */ timeout(1000); /* One second */
} }
void static void
exitcurses(void) exitcurses(void)
{ {
endwin(); /* Restore terminal */ endwin(); /* Restore terminal */
} }
/* Messages show up at the bottom */ /* Messages show up at the bottom */
void static void
printmsg(char *msg) printmsg(char *msg)
{ {
move(LINES - 1, 0); move(LINES - 1, 0);
@ -323,14 +301,14 @@ printmsg(char *msg)
} }
/* Display warning as a message */ /* Display warning as a message */
void static void
printwarn(void) printwarn(void)
{ {
printmsg(strerror(errno)); printmsg(strerror(errno));
} }
/* Kill curses and display error before exiting */ /* Kill curses and display error before exiting */
void static void
printerr(int ret, char *prefix) printerr(int ret, char *prefix)
{ {
exitcurses(); exitcurses();
@ -339,14 +317,14 @@ printerr(int ret, char *prefix)
} }
/* Clear the last line */ /* Clear the last line */
void static void
clearprompt(void) clearprompt(void)
{ {
printmsg(""); printmsg("");
} }
/* Print prompt on the last line */ /* Print prompt on the last line */
void static void
printprompt(char *str) printprompt(char *str)
{ {
clearprompt(); clearprompt();
@ -355,10 +333,11 @@ printprompt(char *str)
/* Returns SEL_* if key is bound and 0 otherwise. /* Returns SEL_* if key is bound and 0 otherwise.
* Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */ * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
int static int
nextsel(char **run, char **env) nextsel(char **run, char **env)
{ {
int c, i; int c;
unsigned int i;
c = getch(); c = getch();
if (c == -1) if (c == -1)
@ -375,7 +354,7 @@ nextsel(char **run, char **env)
return 0; return 0;
} }
char * static char *
readln(void) readln(void)
{ {
static char ln[LINE_MAX]; static char ln[LINE_MAX];
@ -391,7 +370,7 @@ readln(void)
return ln[0] ? ln : NULL; return ln[0] ? ln : NULL;
} }
int static int
canopendir(char *path) canopendir(char *path)
{ {
DIR *dirp; DIR *dirp;
@ -403,7 +382,7 @@ canopendir(char *path)
return 1; return 1;
} }
char * static char *
mkpath(char *dir, char *name, char *out, size_t n) mkpath(char *dir, char *name, char *out, size_t n)
{ {
/* Handle absolute path */ /* Handle absolute path */
@ -419,7 +398,7 @@ mkpath(char *dir, char *name, char *out, size_t n)
return out; return out;
} }
void static void
printent(struct entry *ent, int active) printent(struct entry *ent, int active)
{ {
if (S_ISDIR(ent->mode)) if (S_ISDIR(ent->mode))
@ -436,9 +415,9 @@ printent(struct entry *ent, int active)
printw("%s%s\n", active ? CURSR : EMPTY, ent->name); printw("%s%s\n", active ? CURSR : EMPTY, ent->name);
} }
void (*printptr)(struct entry *ent, int active) = &printent; static void (*printptr)(struct entry *ent, int active) = &printent;
char* static char*
coolsize(off_t size) coolsize(off_t size)
{ {
static char size_buf[12]; /* Buffer to hold human readable size */ static char size_buf[12]; /* Buffer to hold human readable size */
@ -454,11 +433,11 @@ coolsize(off_t size)
return size_buf; return size_buf;
} }
void static void
printent_long(struct entry *ent, int active) printent_long(struct entry *ent, int active)
{ {
static char buf[18]; static char buf[18];
const static struct tm *p; static const struct tm *p;
p = localtime(&ent->t); p = localtime(&ent->t);
strftime(buf, 18, "%b %d %H:%M %Y", p); strftime(buf, 18, "%b %d %H:%M %Y", p);
@ -495,7 +474,7 @@ printent_long(struct entry *ent, int active)
attroff(A_REVERSE); attroff(A_REVERSE);
} }
int static int
dentfill(char *path, struct entry **dents, dentfill(char *path, struct entry **dents,
int (*filter)(regex_t *, char *), regex_t *re) int (*filter)(regex_t *, char *), regex_t *re)
{ {
@ -536,14 +515,14 @@ dentfill(char *path, struct entry **dents,
return n; return n;
} }
void static void
dentfree(struct entry *dents) dentfree(struct entry *dents)
{ {
free(dents); free(dents);
} }
/* Return the position of the matching entry or 0 otherwise */ /* Return the position of the matching entry or 0 otherwise */
int static int
dentfind(struct entry *dents, int n, char *cwd, char *path) dentfind(struct entry *dents, int n, char *cwd, char *path)
{ {
char tmp[PATH_MAX]; char tmp[PATH_MAX];
@ -561,7 +540,7 @@ dentfind(struct entry *dents, int n, char *cwd, char *path)
return 0; return 0;
} }
int static int
populate(char *path, char *oldpath, char *fltr) populate(char *path, char *oldpath, char *fltr)
{ {
regex_t re; regex_t re;
@ -590,7 +569,7 @@ populate(char *path, char *oldpath, char *fltr)
return 0; return 0;
} }
void static void
redraw(char *path) redraw(char *path)
{ {
static char cwd[PATH_MAX]; static char cwd[PATH_MAX];
@ -661,7 +640,7 @@ redraw(char *path)
} }
} }
void static void
browse(char *ipath, char *ifilter) browse(char *ipath, char *ifilter)
{ {
static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX]; static char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
@ -945,7 +924,7 @@ nochange:
} }
} }
void static void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: nnn [-d] [dir]\n"); fprintf(stderr, "usage: nnn [-d] [dir]\n");