Browse Source

Replace a large buf with global g_buf

master
Arun Prakash Jana 6 years ago
parent
commit
cc1891d351
No known key found for this signature in database GPG Key ID: A75979F35C080412
1 changed files with 34 additions and 22 deletions
  1. +34
    -22
      nnn.c

+ 34
- 22
nnn.c View File

@@ -454,8 +454,7 @@ xmemrchr(uchar *s, uchar ch, size_t n)
static char * static char *
xdirname(const char *path) xdirname(const char *path)
{ {
static char *buf = g_buf;
static char *last_slash, *runp;
static char *buf = g_buf, *last_slash, *runp;


xstrlcpy(buf, path, PATH_MAX); xstrlcpy(buf, path, PATH_MAX);


@@ -513,11 +512,11 @@ xdirname(const char *path)
static int static int
all_dots(const char *path) all_dots(const char *path)
{ {
int count = 0;

if (!path) if (!path)
return FALSE; return FALSE;


int count = 0;

while (*path == '.') while (*path == '.')
++count, ++path; ++count, ++path;


@@ -561,9 +560,9 @@ initcurses(void)
static void static void
spawn(char *file, char *arg1, char *arg2, char *dir, uchar flag) spawn(char *file, char *arg1, char *arg2, char *dir, uchar flag)
{ {
pid_t pid;
int status;
char *shlvl;
static char *shlvl;
static pid_t pid;
static int status;


if (flag & F_NORMAL) if (flag & F_NORMAL)
exitcurses(); exitcurses();
@@ -1176,21 +1175,30 @@ resetdircolor(mode_t mode)
/* /*
* Replace escape characters in a string with '?' * Replace escape characters in a string with '?'
* Adjust string length to maxcols if > 0; * Adjust string length to maxcols if > 0;
*
* Interestingly, note that buffer points to g_buf. What happens if
* str also points to g_buf? In this case we assume that the caller
* acknowledges that it's OK to lose the data in g_buf after this
* call to unescape().
* The API, on its part, first converts str to multibyte (after which
* it doesn't touch str anymore). Only after that it starts modifying
* buffer. This works like a phased operation.
*/ */
static char * static char *
unescape(const char *str, uint maxcols) unescape(const char *str, uint maxcols)
{ {
static char buffer[PATH_MAX];
static wchar_t wbuf[PATH_MAX]; static wchar_t wbuf[PATH_MAX];
static char *buffer;
static wchar_t *buf; static wchar_t *buf;
static size_t len; static size_t len;


buffer[0] = '\0';
buf = wbuf;

/* Convert multi-byte to wide char */ /* Convert multi-byte to wide char */
len = mbstowcs(wbuf, str, PATH_MAX); len = mbstowcs(wbuf, str, PATH_MAX);


buffer = g_buf;
buffer[0] = '\0';
buf = wbuf;

if (maxcols && len > maxcols) { if (maxcols && len > maxcols) {
len = wcswidth(wbuf, len); len = wcswidth(wbuf, len);


@@ -1368,7 +1376,7 @@ static char *
get_lsperms(mode_t mode, char *desc) get_lsperms(mode_t mode, char *desc)
{ {
static const char * const rwx[] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"}; static const char * const rwx[] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
static char bits[11];
static char bits[11] = {'\0'};


bits[0] = get_fileind(mode, desc); bits[0] = get_fileind(mode, desc);
strcpy(&bits[1], rwx[(mode >> 6) & 7]); strcpy(&bits[1], rwx[(mode >> 6) & 7]);
@@ -1382,8 +1390,6 @@ get_lsperms(mode_t mode, char *desc)
if (mode & S_ISVTX) if (mode & S_ISVTX)
bits[9] = (mode & 0001) ? 't' : 'T'; /* others executable */ bits[9] = (mode & 0001) ? 't' : 'T'; /* others executable */


bits[10] = '\0';

return bits; return bits;
} }


@@ -1463,7 +1469,8 @@ get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2, int page
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 desc[32];
char *perms = get_lsperms(sb->st_mode, desc);
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);
@@ -1479,8 +1486,12 @@ show_stats(char *fpath, char *fname, struct stat *sb)
if (len != -1) { if (len != -1) {
g_buf[len] = '\0'; g_buf[len] = '\0';
dprintf(fd, " File: '%s' -> ", unescape(fname, 0)); dprintf(fd, " File: '%s' -> ", unescape(fname, 0));

/*
* We pass g_buf but unescape() operates on g_buf too!
* Read the API notes for information on how this works.
*/
dprintf(fd, "'%s'", unescape(g_buf, 0)); dprintf(fd, "'%s'", unescape(g_buf, 0));
xstrlcpy(g_buf, "symbolic link", MAX_CMD_LEN);
} }
} else } else
dprintf(fd, " File: '%s'", unescape(fname, 0)); dprintf(fd, " File: '%s'", unescape(fname, 0));
@@ -1491,7 +1502,7 @@ show_stats(char *fpath, char *fname, struct stat *sb)
#else #else
dprintf(fd, "\n Size: %-15ld Blocks: %-10ld IO Block: %-6ld %s", dprintf(fd, "\n Size: %-15ld Blocks: %-10ld IO Block: %-6ld %s",
#endif #endif
sb->st_size, sb->st_blocks, sb->st_blksize, g_buf);
sb->st_size, sb->st_blocks, sb->st_blksize, desc);


/* Show containing device, inode, hardlink count */ /* Show containing device, inode, hardlink count */
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
@@ -1984,8 +1995,9 @@ populate(char *path, char *oldpath, char *fltr)
static void static void
redraw(char *path) redraw(char *path)
{ {
static int nlines, i;
static char buf[(NAME_MAX + 1) << 1];
static size_t ncols; static size_t ncols;
static int nlines, i;
static bool mode_changed; static bool mode_changed;


mode_changed = FALSE; mode_changed = FALSE;
@@ -2091,13 +2103,13 @@ redraw(char *path)


/* We need to show filename as it may be truncated in directory listing */ /* We need to show filename as it may be truncated in directory listing */
if (!cfg.blkorder) if (!cfg.blkorder)
sprintf(g_buf, "%d/%d %s[%s%s]", cur + 1, ndents, sort, unescape(dents[cur].name, 0), ind);
sprintf(buf, "%d/%d %s[%s%s]", cur + 1, ndents, sort, unescape(dents[cur].name, 0), ind);
else { else {
i = sprintf(g_buf, "%d/%d du: %s (%lu files) ", cur + 1, ndents, coolsize(dir_blocks << 9), num_files);
sprintf(g_buf + i, "vol: %s free [%s%s]", coolsize(get_fs_free(path)), unescape(dents[cur].name, 0), ind);
i = sprintf(buf, "%d/%d du: %s (%lu files) ", cur + 1, ndents, coolsize(dir_blocks << 9), num_files);
sprintf(buf + i, "vol: %s free [%s%s]", coolsize(get_fs_free(path)), unescape(dents[cur].name, 0), ind);
} }


printmsg(g_buf);
printmsg(buf);
} else } else
printmsg("0 items"); printmsg("0 items");
} }


Loading…
Cancel
Save