Simplify show_stats()

This commit is contained in:
Arun Prakash Jana 2021-06-12 21:33:26 +05:30
parent 9a124ae935
commit 69926a8eff
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -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 */
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"); 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;