/* Gets only a single line, that's what we need for now */
static char *
-get_output(char *buf, size_t bytes)
+get_output(char *buf, size_t bytes, char *file, char *arg1, char *arg2)
{
- char *ret;
- FILE *pf = popen(buf, "r");
- if (pf) {
+ pid_t pid = 0;
+ int pipefd[2];
+ FILE* pf;
+ int status;
+ char *ret = NULL;
+
+ 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);
+ }
+
+ /* In parent */
+ close(pipefd[1]);
+ if ((pf = fdopen(pipefd[0], "r"))) {
ret = fgets(buf, bytes, pf);
- pclose(pf);
- return ret;
+ close(pipefd[0]);
}
- return NULL;
+ waitpid(pid, &status, 0);
+
+ return ret;
}
/*
if (S_ISREG(sb->st_mode)) {
/* Show file(1) output */
- sprintf(g_buf, "file -b \'%s\' 2>&1", fpath);
- p = get_output(g_buf, sizeof(g_buf));
+ p = get_output(g_buf, MAX_CMD_LEN, "file", "-b", fpath);
if (p) {
dprintf(fd, "\n\n ");
while (*p) {
show_mediainfo(const char* fpath, int full)
{
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;
sprintf(g_buf, "mediainfo \'%s\' ", fpath);
/* If nlay doesn't handle it, open plain text
files with vi, then try NNN_FALLBACK_OPENER */
- sprintf(g_buf, "file -bi \'%s\'", newpath);
- if (get_output(g_buf, MAX_CMD_LEN) == NULL)
+ if (get_output(g_buf, MAX_CMD_LEN, "file", "-bi", newpath) == NULL)
continue;
if (strstr(g_buf, "text/") == g_buf) {