Use execlp(3) instead of popen(3)

This commit is contained in:
Arun Prakash Jana 2017-05-15 09:54:30 +05:30
parent d01d689340
commit 20216a995b
No known key found for this signature in database
GPG key ID: A75979F35C080412

45
nnn.c
View file

@ -1029,17 +1029,38 @@ get_lsperms(mode_t mode, char *desc)
/* Gets only a single line, that's what we need for now */ /* Gets only a single line, that's what we need for now */
static char * static char *
get_output(char *buf, size_t bytes) get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2)
{ {
char *ret; pid_t pid = 0;
FILE *pf = popen(buf, "r"); int pipefd[2];
if (pf) { FILE* pf;
ret = fgets(buf, bytes, pf); int status;
pclose(pf); char *ret = NULL;
return ret;
if (pipe(pipefd) == -1)
printerr(1, "pipe(2)");
pid = fork();
if (pid == 0) {
/* In child */
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
execlp(file, file, arg1, arg2, NULL);
_exit(1);
} }
return NULL; /* In parent */
close(pipefd[1]);
if ((pf = fdopen(pipefd[0], "r"))) {
ret = fgets(buf, bytes, pf);
close(pipefd[0]);
}
waitpid(pid, &status, 0);
return ret;
} }
/* /*
@ -1113,8 +1134,7 @@ show_stats(char* fpath, char* fname, struct stat *sb)
if (S_ISREG(sb->st_mode)) { if (S_ISREG(sb->st_mode)) {
/* Show file(1) output */ /* Show file(1) output */
sprintf(g_buf, "file -b \'%s\' 2>&1", fpath); p = get_output(g_buf, MAX_CMD_LEN, "file", "-b", fpath);
p = get_output(g_buf, sizeof(g_buf));
if (p) { if (p) {
dprintf(fd, "\n\n "); dprintf(fd, "\n\n ");
while (*p) { while (*p) {
@ -1144,7 +1164,7 @@ static int
show_mediainfo(const char* fpath, int full) show_mediainfo(const char* fpath, int full)
{ {
strcpy(g_buf, "which mediainfo"); strcpy(g_buf, "which mediainfo");
if (get_output(g_buf, MAX_CMD_LEN) == NULL) if (get_output(g_buf, MAX_CMD_LEN, "which", "mediainfo", NULL) == NULL)
return -1; return -1;
sprintf(g_buf, "mediainfo \'%s\' ", fpath); sprintf(g_buf, "mediainfo \'%s\' ", fpath);
@ -1591,8 +1611,7 @@ 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(g_buf, "file -bi \'%s\'", newpath); if (get_output(g_buf, MAX_CMD_LEN, "file", "-bi", newpath) == NULL)
if (get_output(g_buf, MAX_CMD_LEN) == NULL)
continue; continue;
if (strstr(g_buf, "text/") == g_buf) { if (strstr(g_buf, "text/") == g_buf) {