summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libugly/popen.c
blob: e0e441495286304adab8a8f35550499ec514e7e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "dietstdio.h"
#include <unistd.h>

extern char **environ;

FILE *popen(const char *command, const char *type) {
  int pfd[2];
  int fd0;
  pid_t pid;
  if (pipe(pfd)<0) return 0;
  fd0=(*type=='r');
  if ((pid=vfork())<0) {
    close(pfd[0]);
    close(pfd[1]);
    return 0;
  }
  if (!pid) {	/* child */
    char *argv[]={"sh","-c",0,0};
    close(pfd[!fd0]); close(fd0);
    dup2(pfd[fd0],fd0); close(pfd[fd0]);
    argv[2]=(char*)command;
    execve("/bin/sh",argv,environ);
    _exit(255);
  }
  close(pfd[fd0]);
  {
    register FILE* f;
    if ((f=fdopen(pfd[!fd0],type)))
      f->popen_kludge=pid;
    return f;
  }
}