1 --- popen.c.orig        2003-05-20 15:21:02.000000000 -0700
 
   2 +++ popen.c     2005-09-14 15:53:35.000000000 -0700
 
   3 @@ -43,7 +43,8 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/pop
 
   8 +#include <sys/socket.h>
 
   9 +#include <wchar.h>             /* fwide() */
 
  13 @@ -55,11 +56,14 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/pop
 
  14  #include "un-namespace.h"
 
  15  #include "libc_private.h"
 
  17 -extern char **environ;
 
  18 +#include <crt_externs.h>
 
  19 +#define environ (*_NSGetEnviron())
 
  21 +/* 3516149 - store file descriptor and use that to close to prevent blocking */
 
  28  static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
 
  29 @@ -77,20 +81,24 @@ popen(command, type)
 
  34 -        * Lite2 introduced two-way popen() pipes using _socketpair().
 
  35 -        * FreeBSD's pipe() is bidirectional, so we use that.
 
  37 -       if (strchr(type, '+')) {
 
  42 +       if (strcmp(type, "r+") == 0) {
 
  45 +               if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
 
  49 -               if ((*type != 'r' && *type != 'w') || type[1])
 
  50 +               if ((*type != 'r' && *type != 'w') || type[1]) {
 
  58         if ((cur = malloc(sizeof(struct pid))) == NULL) {
 
  59                 (void)_close(pdes[0]);
 
  60 @@ -104,7 +112,7 @@ popen(command, type)
 
  64 -       switch (pid = vfork()) {
 
  65 +       switch (pid = fork()) {
 
  68                 (void)_close(pdes[0]);
 
  69 @@ -138,7 +146,7 @@ popen(command, type)
 
  70                         (void)_close(pdes[1]);
 
  72                 for (p = pidlist; p; p = p->next) {
 
  73 -                       (void)_close(fileno(p->fp));
 
  74 +                       (void)_close(p->fd);
 
  76                 _execve(_PATH_BSHELL, argv, environ);
 
  78 @@ -149,9 +157,11 @@ popen(command, type)
 
  79         /* Parent; assume fdopen can't fail. */
 
  81                 iop = fdopen(pdes[0], type);
 
  83                 (void)_close(pdes[1]);
 
  85                 iop = fdopen(pdes[1], type);
 
  87                 (void)_close(pdes[0]);
 
  90 @@ -162,7 +172,7 @@ popen(command, type)
 
  95 +       fwide(iop, -1);         /* byte stream */