Remove redundant buffers

This commit is contained in:
Arun Prakash Jana 2018-11-09 14:56:11 +05:30
parent 7078f22a0e
commit db807095f1
No known key found for this signature in database
GPG Key ID: A75979F35C080412
1 changed files with 42 additions and 37 deletions

79
nnn.c
View File

@ -201,7 +201,7 @@ disabledbg()
#define clearprompt() printmsg("") #define clearprompt() printmsg("")
#define printwarn() printmsg(strerror(errno)) #define printwarn() printmsg(strerror(errno))
#define istopdir(path) ((path)[1] == '\0' && (path)[0] == '/') #define istopdir(path) ((path)[1] == '\0' && (path)[0] == '/')
#define copycurname() xstrlcpy(oldname, dents[cur].name, NAME_MAX + 1) #define copycurname() xstrlcpy(lastname, dents[cur].name, NAME_MAX + 1)
#define settimeout() timeout(1000) #define settimeout() timeout(1000)
#define cleartimeout() timeout(-1) #define cleartimeout() timeout(-1)
#define errexit() printerr(__LINE__) #define errexit() printerr(__LINE__)
@ -264,12 +264,12 @@ typedef struct {
/* Contexts or workspaces */ /* Contexts or workspaces */
typedef struct { typedef struct {
char c_name[NAME_MAX + 1]; char c_path[PATH_MAX]; /* Current dir */
char c_path[PATH_MAX]; char c_init[PATH_MAX]; /* Initial dir */
char c_init[PATH_MAX]; char c_last[PATH_MAX]; /* Last visited dir */
char c_last[PATH_MAX]; char c_name[NAME_MAX + 1]; /* Current file name */
settings c_cfg; settings c_cfg; /* Current configuration */
char c_fltr[DOT_FILTER_LEN]; char c_fltr[DOT_FILTER_LEN]; /* Hidden filter */
} context; } context;
/* GLOBALS */ /* GLOBALS */
@ -2281,7 +2281,7 @@ static int dentfind(struct entry *dents, const char *fname, int n)
return 0; return 0;
} }
static int populate(char *path, char *oldname, char *fltr) static int populate(char *path, char *lastname, char *fltr)
{ {
static regex_t re; static regex_t re;
@ -2319,7 +2319,7 @@ static int populate(char *path, char *oldname, char *fltr)
#endif #endif
/* Find cur from history */ /* Find cur from history */
cur = dentfind(dents, oldname, ndents); cur = dentfind(dents, lastname, ndents);
return 0; return 0;
} }
@ -2471,11 +2471,9 @@ static void redraw(char *path)
static void browse(char *ipath, char *hfilter) static void browse(char *ipath, char *hfilter)
{ {
static char path[PATH_MAX] __attribute__ ((aligned)); static char *path, *lastdir, *lastname;
static char newpath[PATH_MAX] __attribute__ ((aligned)); static char newpath[PATH_MAX] __attribute__ ((aligned));
static char lastdir[PATH_MAX] __attribute__ ((aligned));
static char mark[PATH_MAX] __attribute__ ((aligned)); static char mark[PATH_MAX] __attribute__ ((aligned));
static char oldname[NAME_MAX + 1] __attribute__ ((aligned));
char *dir, *tmp, *run = NULL, *env = NULL; char *dir, *tmp, *run = NULL, *env = NULL;
struct stat sb; struct stat sb;
int r, fd, presel, ncp = 0, copystartid = 0, copyendid = 0; int r, fd, presel, ncp = 0, copystartid = 0, copyendid = 0;
@ -2483,11 +2481,15 @@ static void browse(char *ipath, char *hfilter)
bool dir_changed = FALSE; bool dir_changed = FALSE;
/* setup first context */ /* setup first context */
xstrlcpy(g_ctx[0].c_init, ipath, PATH_MAX); xstrlcpy(g_ctx[0].c_path, ipath, PATH_MAX); /* current directory */
g_ctx[0].c_cfg = cfg; path = g_ctx[0].c_path;
xstrlcpy(g_ctx[0].c_init, ipath, PATH_MAX); /* start directory */
lastdir = g_ctx[0].c_last; /* last visited directory */
lastname = g_ctx[0].c_name; /* last visited filename */
g_ctx[0].c_cfg = cfg; /* current configuration */
xstrlcpy(path, ipath, PATH_MAX); xstrlcpy(path, ipath, PATH_MAX);
oldname[0] = newpath[0] = lastdir[0] = mark[0] = '\0'; lastname[0] = newpath[0] = lastdir[0] = mark[0] = '\0';
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;
@ -2522,7 +2524,7 @@ begin:
} }
#endif #endif
if (populate(path, oldname, hfilter) == -1) { if (populate(path, lastname, hfilter) == -1) {
printwarn(); printwarn();
goto nochange; goto nochange;
} }
@ -2569,7 +2571,7 @@ nochange:
} }
/* Save history */ /* Save history */
xstrlcpy(oldname, xbasename(path), NAME_MAX + 1); xstrlcpy(lastname, xbasename(path), NAME_MAX + 1);
/* Save last working directory */ /* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX); xstrlcpy(lastdir, path, PATH_MAX);
@ -2613,7 +2615,7 @@ nochange:
dir_changed = TRUE; dir_changed = TRUE;
xstrlcpy(path, newpath, PATH_MAX); xstrlcpy(path, newpath, PATH_MAX);
oldname[0] = '\0'; lastname[0] = '\0';
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;
goto begin; goto begin;
@ -2697,7 +2699,7 @@ nochange:
dir_changed = TRUE; dir_changed = TRUE;
xstrlcpy(path, dir, PATH_MAX); xstrlcpy(path, dir, PATH_MAX);
oldname[0] = '\0'; lastname[0] = '\0';
DPRINTF_S(path); DPRINTF_S(path);
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;
@ -2724,7 +2726,7 @@ nochange:
xstrlcpy(lastdir, path, PATH_MAX); xstrlcpy(lastdir, path, PATH_MAX);
dir_changed = TRUE; dir_changed = TRUE;
xstrlcpy(path, newpath, PATH_MAX); xstrlcpy(path, newpath, PATH_MAX);
oldname[0] = '\0'; lastname[0] = '\0';
DPRINTF_S(path); DPRINTF_S(path);
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;
@ -2768,31 +2770,31 @@ nochange:
/* Save current context */ /* Save current context */
xstrlcpy(g_ctx[cfg.curctx].c_name, dents[cur].name, NAME_MAX + 1); xstrlcpy(g_ctx[cfg.curctx].c_name, dents[cur].name, NAME_MAX + 1);
xstrlcpy(g_ctx[cfg.curctx].c_path, path, PATH_MAX);
xstrlcpy(g_ctx[cfg.curctx].c_last, lastdir, PATH_MAX);
g_ctx[cfg.curctx].c_cfg = cfg; g_ctx[cfg.curctx].c_cfg = cfg;
xstrlcpy(g_ctx[cfg.curctx].c_fltr, hfilter, DOT_FILTER_LEN); xstrlcpy(g_ctx[cfg.curctx].c_fltr, hfilter, DOT_FILTER_LEN);
if (!g_ctx[r].c_cfg.ctxactive) { if (!g_ctx[r].c_cfg.ctxactive) {
/* Setup a new context from current context */
g_ctx[r].c_cfg.ctxactive = 1; g_ctx[r].c_cfg.ctxactive = 1;
xstrlcpy(g_ctx[r].c_name, dents[cur].name, NAME_MAX + 1);
/* Setup a new context from current context */
xstrlcpy(g_ctx[r].c_path, path, PATH_MAX); xstrlcpy(g_ctx[r].c_path, path, PATH_MAX);
xstrlcpy(g_ctx[r].c_init, path, PATH_MAX); xstrlcpy(g_ctx[r].c_init, path, PATH_MAX);
ipath = g_ctx[r].c_init; g_ctx[r].c_last[0] = '\0';
g_ctx[r].c_last[0] = lastdir[0] = '\0'; xstrlcpy(g_ctx[r].c_name, dents[cur].name, NAME_MAX + 1);
g_ctx[r].c_cfg = cfg; g_ctx[r].c_cfg = cfg;
xstrlcpy(g_ctx[r].c_fltr, hfilter, DOT_FILTER_LEN); xstrlcpy(g_ctx[r].c_fltr, hfilter, DOT_FILTER_LEN);
} else { } else {
/* Switch to saved context */ /* Switch to saved context */
xstrlcpy(oldname, g_ctx[r].c_name, NAME_MAX + 1);
xstrlcpy(path, g_ctx[r].c_path, PATH_MAX);
ipath = g_ctx[r].c_init;
xstrlcpy(lastdir, g_ctx[r].c_last, PATH_MAX);
cfg = g_ctx[r].c_cfg; cfg = g_ctx[r].c_cfg;
xstrlcpy(hfilter, g_ctx[r].c_fltr, DOT_FILTER_LEN); xstrlcpy(hfilter, g_ctx[r].c_fltr, DOT_FILTER_LEN);
} }
/* Reset the pointers */
path = g_ctx[r].c_path;
ipath = g_ctx[r].c_init;
lastdir = g_ctx[r].c_last;
lastname = g_ctx[r].c_name;
cfg.curctx = r; cfg.curctx = r;
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;
@ -2814,7 +2816,7 @@ nochange:
if (strcmp(path, newpath) == 0) if (strcmp(path, newpath) == 0)
break; break;
oldname[0] = '\0'; lastname[0] = '\0';
/* Save last working directory */ /* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX); xstrlcpy(lastdir, path, PATH_MAX);
@ -2853,6 +2855,9 @@ nochange:
case SEL_TOGGLEDOT: case SEL_TOGGLEDOT:
cfg.showhidden ^= 1; cfg.showhidden ^= 1;
initfilter(cfg.showhidden, hfilter); initfilter(cfg.showhidden, hfilter);
/* Save current */
if (ndents)
copycurname();
goto begin; goto begin;
case SEL_DETAIL: case SEL_DETAIL:
cfg.showdetail ^= 1; cfg.showdetail ^= 1;
@ -3178,7 +3183,7 @@ nochange:
} }
close(fd); close(fd);
xstrlcpy(oldname, tmp, NAME_MAX + 1); xstrlcpy(lastname, tmp, NAME_MAX + 1);
goto begin; goto begin;
case SEL_RENAME: case SEL_RENAME:
if (!ndents) if (!ndents)
@ -3226,7 +3231,7 @@ nochange:
} }
close(fd); close(fd);
xstrlcpy(oldname, tmp, NAME_MAX + 1); xstrlcpy(lastname, tmp, NAME_MAX + 1);
goto begin; goto begin;
case SEL_RENAMEALL: case SEL_RENAMEALL:
if (!get_output(g_buf, MAX_CMD_LEN, "which", utils[VIDIR], NULL, 0)) { if (!get_output(g_buf, MAX_CMD_LEN, "which", utils[VIDIR], NULL, 0)) {
@ -3305,12 +3310,12 @@ nochange:
g_ctx[cfg.curctx].c_cfg.ctxactive = 0; g_ctx[cfg.curctx].c_cfg.ctxactive = 0;
/* Switch to next active context */ /* Switch to next active context */
xstrlcpy(oldname, g_ctx[r].c_name, NAME_MAX + 1); path = g_ctx[r].c_path;
xstrlcpy(hfilter, g_ctx[r].c_fltr, NAME_MAX + 1);
xstrlcpy(path, g_ctx[r].c_path, PATH_MAX);
ipath = g_ctx[r].c_init; ipath = g_ctx[r].c_init;
xstrlcpy(lastdir, g_ctx[r].c_last, PATH_MAX); lastdir = g_ctx[r].c_last;
lastname = g_ctx[r].c_name;
cfg = g_ctx[r].c_cfg; cfg = g_ctx[r].c_cfg;
xstrlcpy(hfilter, g_ctx[r].c_fltr, NAME_MAX + 1);
cfg.curctx = r; cfg.curctx = r;
if (cfg.filtermode) if (cfg.filtermode)