From: Thomas Veerman Date: Wed, 27 Feb 2013 16:50:04 +0000 (+0000) Subject: libc: drop Minix popen and use NetBSD's X-Git-Tag: v3.3.0~1116 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=refs%2Fchanges%2F72%2F372%2F2;p=minix.git libc: drop Minix popen and use NetBSD's Change-Id: I56f253d855671a6e1c8d53c7383107565164ab8d --- diff --git a/lib/libc/gen/minix/popen.c b/lib/libc/gen/minix/popen.c deleted file mode 100644 index 3bd48fc46..000000000 --- a/lib/libc/gen/minix/popen.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * popen - open a pipe - */ -/* $Header$ */ -#include -#include "namespace.h" - -#ifdef __weak_alias -__weak_alias(popen, _popen) -__weak_alias(pclose, _pclose) -#endif - -#include -#include -#include -#include -#include - -#if defined(__BSD4_2) -union wait { - int w_status; -}; -typedef union wait wait_arg; -#else -typedef int wait_arg; -#endif /* __BSD4_2 */ - -int _close(int d); -int _dup2(int oldd, int newd); /* not present in System 5 */ -int _execl(const char *name, const char *_arg, ... ); -pid_t _fork(void); -int _pipe(int fildes[2]); -pid_t _wait(wait_arg *status); -void _exit(int status); - -static int pids[OPEN_MAX]; - -FILE * -popen(command, type) -const char *command; -const char *type; -{ - int piped[2]; - int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2; - int pid; - - if (Xtype == 2 || - _pipe(piped) < 0 || - (pid = _fork()) < 0) return 0; - - if (pid == 0) { - /* child */ - register int *p; - - for (p = pids; p < &pids[OPEN_MAX]; p++) { - if (*p) _close((int)(p - pids)); - } - _close(piped[Xtype]); - _dup2(piped[!Xtype], !Xtype); - _close(piped[!Xtype]); - _execl("/bin/sh", "sh", "-c", command, (char *) 0); - _exit(127); /* like system() ??? */ - } - - pids[piped[Xtype]] = pid; - _close(piped[!Xtype]); - return fdopen(piped[Xtype], type); -} - -#if defined(__BSD4_2) -#define ret_val status.w_status -#else -#define ret_val status -#endif - -int -pclose(stream) -FILE *stream; -{ - int fd = fileno(stream); - wait_arg status; - int wret; - - void (*intsave)(int) = signal(SIGINT, SIG_IGN); - void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN); - fclose(stream); - while ((wret = _wait(&status)) != -1) { - if (wret == pids[fd]) break; - } - if (wret == -1) ret_val = -1; - signal(SIGINT, intsave); - signal(SIGQUIT, quitsave); - pids[fd] = 0; - return ret_val; -} - -#if defined(__USG) -int _dup(int fildes); - -static int -_dup2(oldd, newd) -int oldd, newd; -{ - int i = 0, fd, tmp; - int fdbuf[_NFILES]; - - /* ignore the error on the close() */ - tmp = errno; (void) _close(newd); errno = tmp; - while ((fd = _dup(oldd)) != newd) { - if (fd == -1) break; - fdbuf[i++] = fd; - } - tmp = errno; - while (--i >= 0) { - _close(fdbuf[i]); - } - errno = tmp; - return -(fd == -1); -} -#endif /* __USG */ diff --git a/lib/libc/gen/popen.c b/lib/libc/gen/popen.c index 593e34631..cd8cb5a6c 100644 --- a/lib/libc/gen/popen.c +++ b/lib/libc/gen/popen.c @@ -111,13 +111,21 @@ popen(const char *command, const char *type) return (NULL); } +#if defined(__minix) + rwlock_rdlock(&pidlist_lock); +#else (void)rwlock_rdlock(&pidlist_lock); +#endif (void)__readlockenv(); switch (pid = vfork()) { case -1: /* Error. */ serrno = errno; (void)__unlockenv(); +#if defined(__minix) + rwlock_unlock(&pidlist_lock); +#else (void)rwlock_unlock(&pidlist_lock); +#endif free(cur); (void)close(pdes[0]); (void)close(pdes[1]); @@ -177,7 +185,11 @@ popen(const char *command, const char *type) cur->pid = pid; cur->next = pidlist; pidlist = cur; +#if defined(__minix) + rwlock_unlock(&pidlist_lock); +#else (void)rwlock_unlock(&pidlist_lock); +#endif return (iop); } @@ -203,7 +215,11 @@ pclose(FILE *iop) if (cur->fp == iop) break; if (cur == NULL) { +#if defined(__minix) + rwlock_unlock(&pidlist_lock); +#else (void)rwlock_unlock(&pidlist_lock); +#endif return (-1); } @@ -215,7 +231,11 @@ pclose(FILE *iop) else last->next = cur->next; +#if defined(__minix) + rwlock_unlock(&pidlist_lock); +#else (void)rwlock_unlock(&pidlist_lock); +#endif do { pid = waitpid(cur->pid, &pstat, 0);