mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 05:41:31 +00:00
Simplify show_stats()
This commit is contained in:
parent
9a124ae935
commit
69926a8eff
73
src/nnn.c
73
src/nnn.c
|
@ -4094,7 +4094,6 @@ static char *get_output(char *buf, const size_t bytes, char *file, char *arg1, c
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
/* Show in pager in child */
|
/* Show in pager in child */
|
||||||
|
@ -4111,70 +4110,58 @@ static char *get_output(char *buf, const size_t bytes, char *file, char *arg1, c
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pipetof(char *cmd, FILE *fout)
|
static void pipetof(char *cmd, FILE *fout, uint_t lines)
|
||||||
{
|
{
|
||||||
FILE *fin = popen(cmd, "r");
|
FILE *fin = popen(cmd, "r");
|
||||||
|
|
||||||
if (fin) {
|
if (fin) {
|
||||||
while (fgets(g_buf, CMD_LEN_MAX - 1, fin))
|
while (lines && fgets(g_buf, CMD_LEN_MAX - 1, fin)) {
|
||||||
fprintf(fout, "%s", g_buf);
|
fprintf(fout, "%s", g_buf);
|
||||||
|
--lines;
|
||||||
|
}
|
||||||
pclose(fin);
|
pclose(fin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void wrap_cmd(char *buf, const char *cmd, char *fpath)
|
||||||
|
{
|
||||||
|
size_t r = xstrsncpy(buf, cmd, CMD_LEN_MAX);
|
||||||
|
|
||||||
|
r += xstrsncpy(buf + r - 1, fpath, PATH_MAX);
|
||||||
|
buf[r - 2] = '\"';
|
||||||
|
buf[r - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Follows the stat(1) output closely
|
* Follows the stat(1) output closely
|
||||||
*/
|
*/
|
||||||
static bool show_stats(char *fpath, const struct stat *sb)
|
static bool show_stats(char *fpath, const struct stat *sb)
|
||||||
{
|
{
|
||||||
int fd;
|
static const char * const cmds[] = {
|
||||||
FILE *fp;
|
#ifdef FILE_MIME_OPTS
|
||||||
char *p, *begin = g_buf;
|
"file " FILE_MIME_OPTS " \"",
|
||||||
size_t r;
|
#endif
|
||||||
|
"file -b \"",
|
||||||
|
"stat \"",
|
||||||
|
};
|
||||||
|
|
||||||
fd = create_tmp_file();
|
char *p, *begin = g_buf;
|
||||||
|
size_t r = ELEMENTS(cmds);
|
||||||
|
int fd = create_tmp_file();
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
r = xstrsncpy(g_buf, "stat \"", PATH_MAX);
|
FILE *fp = fdopen(fd, "w");
|
||||||
r += xstrsncpy(g_buf + r - 1, fpath, PATH_MAX);
|
|
||||||
g_buf[r - 2] = '\"';
|
|
||||||
g_buf[r - 1] = '\0';
|
|
||||||
DPRINTF_S(g_buf);
|
|
||||||
|
|
||||||
fp = fdopen(fd, "w");
|
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
close(fd);
|
close(fd);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
pipetof(g_buf, fp);
|
while (r) {
|
||||||
|
wrap_cmd(g_buf, cmds[--r], fpath);
|
||||||
if (S_ISREG(sb->st_mode)) {
|
pipetof(g_buf, fp, (uint_t)-1);
|
||||||
/* Show file(1) output */
|
fprintf(fp, "\n");
|
||||||
p = get_output(g_buf, CMD_LEN_MAX, "file", "-b", fpath, FALSE);
|
|
||||||
if (p) {
|
|
||||||
fprintf(fp, "\n\n ");
|
|
||||||
while (*p) {
|
|
||||||
if (*p == ',') {
|
|
||||||
*p = '\0';
|
|
||||||
fprintf(fp, " %s\n", begin);
|
|
||||||
begin = p + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
++p;
|
|
||||||
}
|
|
||||||
fprintf(fp, " %s\n ", begin);
|
|
||||||
|
|
||||||
#ifdef FILE_MIME_OPTS
|
|
||||||
/* Show the file MIME type */
|
|
||||||
get_output(g_buf, CMD_LEN_MAX, "file", FILE_MIME_OPTS, fpath, FALSE);
|
|
||||||
fprintf(fp, "%s", g_buf);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fp, "\n");
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
@ -4656,9 +4643,9 @@ static void show_help(const char *path)
|
||||||
|
|
||||||
if (g_state.fortune && getutil("fortune"))
|
if (g_state.fortune && getutil("fortune"))
|
||||||
#ifndef __HAIKU__
|
#ifndef __HAIKU__
|
||||||
pipetof("fortune -s", fp);
|
pipetof("fortune -s", fp, (uint_t)-1);
|
||||||
#else
|
#else
|
||||||
pipetof("fortune", fp);
|
pipetof("fortune", fp, (uint_t)-1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
start = end = helpstr;
|
start = end = helpstr;
|
||||||
|
|
Loading…
Reference in a new issue