mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 05:41:31 +00:00
Use a global buffer for isolated functions
This commit is contained in:
parent
a279f58b91
commit
d01d689340
117
nnn.c
117
nnn.c
|
@ -151,7 +151,9 @@ static off_t blk_size;
|
||||||
static size_t fs_free;
|
static size_t fs_free;
|
||||||
static int open_max;
|
static int open_max;
|
||||||
static const double div_2_pow_10 = 1.0 / 1024.0;
|
static const double div_2_pow_10 = 1.0 / 1024.0;
|
||||||
static const char *size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
|
|
||||||
|
/* For use in functions which are isolated and don't return the buffer */
|
||||||
|
static char g_buf[MAX_CMD_LEN];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Layout:
|
* Layout:
|
||||||
|
@ -445,7 +447,6 @@ getmime(char *file)
|
||||||
static int
|
static int
|
||||||
setfilter(regex_t *regex, char *filter)
|
setfilter(regex_t *regex, char *filter)
|
||||||
{
|
{
|
||||||
static char errbuf[LINE_MAX];
|
|
||||||
static size_t len;
|
static size_t len;
|
||||||
static int r;
|
static int r;
|
||||||
|
|
||||||
|
@ -454,8 +455,8 @@ setfilter(regex_t *regex, char *filter)
|
||||||
len = COLS;
|
len = COLS;
|
||||||
if (len > LINE_MAX)
|
if (len > LINE_MAX)
|
||||||
len = LINE_MAX;
|
len = LINE_MAX;
|
||||||
regerror(r, regex, errbuf, len);
|
regerror(r, regex, g_buf, len);
|
||||||
printmsg(errbuf);
|
printmsg(g_buf);
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -813,7 +814,6 @@ static void
|
||||||
printent(struct entry *ent, int active)
|
printent(struct entry *ent, int active)
|
||||||
{
|
{
|
||||||
static int ncols;
|
static int ncols;
|
||||||
static char str[PATH_MAX + 16];
|
|
||||||
|
|
||||||
if (COLS > PATH_MAX + 16)
|
if (COLS > PATH_MAX + 16)
|
||||||
ncols = PATH_MAX + 16;
|
ncols = PATH_MAX + 16;
|
||||||
|
@ -821,25 +821,25 @@ printent(struct entry *ent, int active)
|
||||||
ncols = COLS;
|
ncols = COLS;
|
||||||
|
|
||||||
if (S_ISDIR(ent->mode))
|
if (S_ISDIR(ent->mode))
|
||||||
snprintf(str, ncols, "%s%s/", CURSYM(active),
|
snprintf(g_buf, ncols, "%s%s/", CURSYM(active),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISLNK(ent->mode))
|
else if (S_ISLNK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%s@", CURSYM(active),
|
snprintf(g_buf, ncols, "%s%s@", CURSYM(active),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISSOCK(ent->mode))
|
else if (S_ISSOCK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%s=", CURSYM(active),
|
snprintf(g_buf, ncols, "%s%s=", CURSYM(active),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISFIFO(ent->mode))
|
else if (S_ISFIFO(ent->mode))
|
||||||
snprintf(str, ncols, "%s%s|", CURSYM(active),
|
snprintf(g_buf, ncols, "%s%s|", CURSYM(active),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (ent->mode & S_IXUSR)
|
else if (ent->mode & S_IXUSR)
|
||||||
snprintf(str, ncols, "%s%s*", CURSYM(active),
|
snprintf(g_buf, ncols, "%s%s*", CURSYM(active),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else
|
else
|
||||||
snprintf(str, ncols, "%s%s", CURSYM(active),
|
snprintf(g_buf, ncols, "%s%s", CURSYM(active),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
|
|
||||||
printw("%s\n", str);
|
printw("%s\n", g_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void (*printptr)(struct entry *ent, int active) = &printent;
|
static void (*printptr)(struct entry *ent, int active) = &printent;
|
||||||
|
@ -847,6 +847,7 @@ static void (*printptr)(struct entry *ent, int active) = &printent;
|
||||||
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 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 fsize, tmp;
|
static off_t fsize, tmp;
|
||||||
|
@ -872,7 +873,6 @@ static void
|
||||||
printent_long(struct entry *ent, int active)
|
printent_long(struct entry *ent, int active)
|
||||||
{
|
{
|
||||||
static int ncols;
|
static int ncols;
|
||||||
static char str[PATH_MAX + 32];
|
|
||||||
static char buf[18];
|
static char buf[18];
|
||||||
|
|
||||||
if (COLS > PATH_MAX + 32)
|
if (COLS > PATH_MAX + 32)
|
||||||
|
@ -887,67 +887,67 @@ printent_long(struct entry *ent, int active)
|
||||||
|
|
||||||
if (!bsizeorder) {
|
if (!bsizeorder) {
|
||||||
if (S_ISDIR(ent->mode))
|
if (S_ISDIR(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s / %s/",
|
snprintf(g_buf, ncols, "%s%-16.16s / %s/",
|
||||||
CURSYM(active), buf, replace_escape(ent->name));
|
CURSYM(active), buf, replace_escape(ent->name));
|
||||||
else if (S_ISLNK(ent->mode))
|
else if (S_ISLNK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s @ %s@",
|
snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
|
||||||
CURSYM(active), buf, replace_escape(ent->name));
|
CURSYM(active), buf, replace_escape(ent->name));
|
||||||
else if (S_ISSOCK(ent->mode))
|
else if (S_ISSOCK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s = %s=",
|
snprintf(g_buf, ncols, "%s%-16.16s = %s=",
|
||||||
CURSYM(active), buf, replace_escape(ent->name));
|
CURSYM(active), buf, replace_escape(ent->name));
|
||||||
else if (S_ISFIFO(ent->mode))
|
else if (S_ISFIFO(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s | %s|",
|
snprintf(g_buf, ncols, "%s%-16.16s | %s|",
|
||||||
CURSYM(active), buf, replace_escape(ent->name));
|
CURSYM(active), buf, replace_escape(ent->name));
|
||||||
else if (S_ISBLK(ent->mode))
|
else if (S_ISBLK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s b %s",
|
snprintf(g_buf, ncols, "%s%-16.16s b %s",
|
||||||
CURSYM(active), buf, replace_escape(ent->name));
|
CURSYM(active), buf, replace_escape(ent->name));
|
||||||
else if (S_ISCHR(ent->mode))
|
else if (S_ISCHR(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s c %s",
|
snprintf(g_buf, ncols, "%s%-16.16s c %s",
|
||||||
CURSYM(active), buf, replace_escape(ent->name));
|
CURSYM(active), buf, replace_escape(ent->name));
|
||||||
else if (ent->mode & S_IXUSR)
|
else if (ent->mode & S_IXUSR)
|
||||||
snprintf(str, ncols, "%s%-16.16s %8.8s* %s*",
|
snprintf(g_buf, ncols, "%s%-16.16s %8.8s* %s*",
|
||||||
CURSYM(active), buf, coolsize(ent->size),
|
CURSYM(active), buf, coolsize(ent->size),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else
|
else
|
||||||
snprintf(str, ncols, "%s%-16.16s %8.8s %s",
|
snprintf(g_buf, ncols, "%s%-16.16s %8.8s %s",
|
||||||
CURSYM(active), buf, coolsize(ent->size),
|
CURSYM(active), 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(str, 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(active), 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(str, ncols, "%s%-16.16s @ %s@",
|
snprintf(g_buf, ncols, "%s%-16.16s @ %s@",
|
||||||
CURSYM(active), buf,
|
CURSYM(active), buf,
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISSOCK(ent->mode))
|
else if (S_ISSOCK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s = %s=",
|
snprintf(g_buf, ncols, "%s%-16.16s = %s=",
|
||||||
CURSYM(active), buf,
|
CURSYM(active), buf,
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISFIFO(ent->mode))
|
else if (S_ISFIFO(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s | %s|",
|
snprintf(g_buf, ncols, "%s%-16.16s | %s|",
|
||||||
CURSYM(active), buf,
|
CURSYM(active), buf,
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISBLK(ent->mode))
|
else if (S_ISBLK(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s b %s",
|
snprintf(g_buf, ncols, "%s%-16.16s b %s",
|
||||||
CURSYM(active), buf,
|
CURSYM(active), buf,
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (S_ISCHR(ent->mode))
|
else if (S_ISCHR(ent->mode))
|
||||||
snprintf(str, ncols, "%s%-16.16s c %s",
|
snprintf(g_buf, ncols, "%s%-16.16s c %s",
|
||||||
CURSYM(active), buf,
|
CURSYM(active), buf,
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else if (ent->mode & S_IXUSR)
|
else if (ent->mode & S_IXUSR)
|
||||||
snprintf(str, 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(active), buf, coolsize(ent->bsize << 9),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
else
|
else
|
||||||
snprintf(str, 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(active), buf, coolsize(ent->bsize << 9),
|
||||||
replace_escape(ent->name));
|
replace_escape(ent->name));
|
||||||
}
|
}
|
||||||
|
|
||||||
printw("%s\n", str);
|
printw("%s\n", g_buf);
|
||||||
|
|
||||||
if (active)
|
if (active)
|
||||||
attroff(A_REVERSE);
|
attroff(A_REVERSE);
|
||||||
|
@ -1048,9 +1048,8 @@ get_output(char *buf, size_t bytes)
|
||||||
static int
|
static int
|
||||||
show_stats(char* fpath, char* fname, struct stat *sb)
|
show_stats(char* fpath, char* fname, struct stat *sb)
|
||||||
{
|
{
|
||||||
char buf[PATH_MAX + 16];
|
char *perms = get_lsperms(sb->st_mode, g_buf);
|
||||||
char *perms = get_lsperms(sb->st_mode, buf);
|
char *p, *begin = g_buf;
|
||||||
char *p, *begin = buf;
|
|
||||||
|
|
||||||
char tmp[] = "/tmp/nnnXXXXXX";
|
char tmp[] = "/tmp/nnnXXXXXX";
|
||||||
int fd = mkstemp(tmp);
|
int fd = mkstemp(tmp);
|
||||||
|
@ -1076,17 +1075,17 @@ 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, buf);
|
sb->st_size, sb->st_blocks, sb->st_blksize, g_buf);
|
||||||
|
|
||||||
/* Show containing device, inode, hardlink count */
|
/* Show containing device, inode, hardlink count */
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
sprintf(buf, "%xh/%ud", sb->st_dev, sb->st_dev);
|
sprintf(g_buf, "%xh/%ud", sb->st_dev, sb->st_dev);
|
||||||
dprintf(fd, "\n Device: %-15s Inode: %-11llu Links: %-9hu",
|
dprintf(fd, "\n Device: %-15s Inode: %-11llu Links: %-9hu",
|
||||||
#else
|
#else
|
||||||
sprintf(buf, "%lxh/%lud", sb->st_dev, sb->st_dev);
|
sprintf(g_buf, "%lxh/%lud", sb->st_dev, sb->st_dev);
|
||||||
dprintf(fd, "\n Device: %-15s Inode: %-11lu Links: %-9lu",
|
dprintf(fd, "\n Device: %-15s Inode: %-11lu Links: %-9lu",
|
||||||
#endif
|
#endif
|
||||||
buf, sb->st_ino, sb->st_nlink);
|
g_buf, sb->st_ino, sb->st_nlink);
|
||||||
|
|
||||||
/* Show major, minor number for block or char device */
|
/* Show major, minor number for block or char device */
|
||||||
if (perms[0] == 'b' || perms[0] == 'c')
|
if (perms[0] == 'b' || perms[0] == 'c')
|
||||||
|
@ -1101,21 +1100,21 @@ show_stats(char* fpath, char* fname, struct stat *sb)
|
||||||
sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
|
sb->st_gid, (getgrgid(sb->st_gid))->gr_name);
|
||||||
|
|
||||||
/* Show last access time */
|
/* Show last access time */
|
||||||
strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
|
strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_atime));
|
||||||
dprintf(fd, "\n\n Access: %s", buf);
|
dprintf(fd, "\n\n Access: %s", g_buf);
|
||||||
|
|
||||||
/* Show last modification time */
|
/* Show last modification time */
|
||||||
strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
|
strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_mtime));
|
||||||
dprintf(fd, "\n Modify: %s", buf);
|
dprintf(fd, "\n Modify: %s", g_buf);
|
||||||
|
|
||||||
/* Show last status change time */
|
/* Show last status change time */
|
||||||
strftime(buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
|
strftime(g_buf, 40, "%a %d-%b-%Y %T %z,%Z", localtime(&sb->st_ctime));
|
||||||
dprintf(fd, "\n Change: %s", buf);
|
dprintf(fd, "\n Change: %s", g_buf);
|
||||||
|
|
||||||
if (S_ISREG(sb->st_mode)) {
|
if (S_ISREG(sb->st_mode)) {
|
||||||
/* Show file(1) output */
|
/* Show file(1) output */
|
||||||
sprintf(buf, "file -b \'%s\' 2>&1", fpath);
|
sprintf(g_buf, "file -b \'%s\' 2>&1", fpath);
|
||||||
p = get_output(buf, sizeof(buf));
|
p = get_output(g_buf, sizeof(g_buf));
|
||||||
if (p) {
|
if (p) {
|
||||||
dprintf(fd, "\n\n ");
|
dprintf(fd, "\n\n ");
|
||||||
while (*p) {
|
while (*p) {
|
||||||
|
@ -1134,8 +1133,8 @@ show_stats(char* fpath, char* fname, struct stat *sb)
|
||||||
dprintf(fd, "\n\n");
|
dprintf(fd, "\n\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
sprintf(buf, "cat %s | less", tmp);
|
sprintf(g_buf, "cat %s | less", tmp);
|
||||||
fd = system(buf);
|
fd = system(g_buf);
|
||||||
|
|
||||||
unlink(tmp);
|
unlink(tmp);
|
||||||
return fd;
|
return fd;
|
||||||
|
@ -1144,25 +1143,23 @@ show_stats(char* fpath, char* fname, struct stat *sb)
|
||||||
static int
|
static int
|
||||||
show_mediainfo(const char* fpath, int full)
|
show_mediainfo(const char* fpath, int full)
|
||||||
{
|
{
|
||||||
static char buf[MAX_CMD_LEN];
|
strcpy(g_buf, "which mediainfo");
|
||||||
|
if (get_output(g_buf, MAX_CMD_LEN) == NULL)
|
||||||
strcpy(buf, "which mediainfo");
|
|
||||||
if (get_output(buf, MAX_CMD_LEN) == NULL)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
sprintf(buf, "mediainfo \'%s\' ", fpath);
|
sprintf(g_buf, "mediainfo \'%s\' ", fpath);
|
||||||
if (full)
|
if (full)
|
||||||
strcat(buf, "-f 2>&1 | less");
|
strcat(g_buf, "-f 2>&1 | less");
|
||||||
else
|
else
|
||||||
strcat(buf, "2>&1 | less");
|
strcat(g_buf, "2>&1 | less");
|
||||||
|
|
||||||
return system(buf);
|
return system(g_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
show_help(void)
|
show_help(void)
|
||||||
{
|
{
|
||||||
char helpstr[] = ("echo \"\
|
static char helpstr[] = ("echo \"\
|
||||||
Key | Function\n\
|
Key | Function\n\
|
||||||
-+-\n\
|
-+-\n\
|
||||||
Up, k, ^P | Previous entry\n\
|
Up, k, ^P | Previous entry\n\
|
||||||
|
@ -1574,8 +1571,6 @@ nochange:
|
||||||
goto begin;
|
goto begin;
|
||||||
case S_IFREG:
|
case S_IFREG:
|
||||||
{
|
{
|
||||||
static char cmd[MAX_CMD_LEN];
|
|
||||||
|
|
||||||
/* If NNN_OPENER is set, use it */
|
/* If NNN_OPENER is set, use it */
|
||||||
if (opener) {
|
if (opener) {
|
||||||
spawn(opener, newpath, NULL, NULL, 4);
|
spawn(opener, newpath, NULL, NULL, 4);
|
||||||
|
@ -1596,11 +1591,11 @@ nochange:
|
||||||
|
|
||||||
/* If nlay doesn't handle it, open plain text
|
/* If nlay doesn't handle it, open plain text
|
||||||
files with vi, then try NNN_FALLBACK_OPENER */
|
files with vi, then try NNN_FALLBACK_OPENER */
|
||||||
sprintf(cmd, "file -bi \'%s\'", newpath);
|
sprintf(g_buf, "file -bi \'%s\'", newpath);
|
||||||
if (get_output(cmd, MAX_CMD_LEN) == NULL)
|
if (get_output(g_buf, MAX_CMD_LEN) == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strstr(cmd, "text/") == cmd) {
|
if (strstr(g_buf, "text/") == g_buf) {
|
||||||
exitcurses();
|
exitcurses();
|
||||||
run = xgetenv("EDITOR", "vi");
|
run = xgetenv("EDITOR", "vi");
|
||||||
spawn(run, newpath, NULL, NULL, 0);
|
spawn(run, newpath, NULL, NULL, 0);
|
||||||
|
|
Loading…
Reference in a new issue