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
#CFLAGS = -g
CFLAGS = -O3 -march=native
CFLAGS = -O3 -march=native -fno-asynchronous-unwind-tables -fdata-sections \
-ffunction-sections -Wl,--gc-sections
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
OBJ = nnn.o strlcat.o strlcpy.o
BIN = nnn
@ -17,7 +18,9 @@ all: $(BIN)
$(BIN): $(OBJ)
$(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
strlcat.o: util.h

View file

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

115
nnn.c
View file

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