From 7e7f7ca3c607e2e150d63692e9cc320bbbe2e7d8 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Tue, 29 Jul 2008 16:29:59 +0000 Subject: [PATCH] Convert popen()'s `pidlist' to a SLIST, for consistency. I guess the original author of the popen() code didn't want to use our macro's, because the single linked list macro's didn't offer O(1) deletion. Because I introduced SLIST_REMOVE_NEXT() some time ago, we can now use the macro's here. By converting the code to an SLIST, it is more consistent with other parts of the C library and the operating system. Reviewed by: csjp Approved by: philip (mentor, implicit) --- lib/libc/gen/popen.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/libc/gen/popen.c b/lib/libc/gen/popen.c index 9e597b95e5d2..f3450b5e1cc4 100644 --- a/lib/libc/gen/popen.c +++ b/lib/libc/gen/popen.c @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" #include +#include #include #include @@ -53,11 +54,12 @@ __FBSDID("$FreeBSD$"); extern char **environ; -static struct pid { - struct pid *next; +struct pid { + SLIST_ENTRY(pid) next; FILE *fp; pid_t pid; -} *pidlist; +}; +static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist); static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER; #define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex) @@ -133,9 +135,8 @@ popen(command, type) } (void)_close(pdes[1]); } - for (p = pidlist; p; p = p->next) { + SLIST_FOREACH(p, &pidlist, next) (void)_close(fileno(p->fp)); - } _execve(_PATH_BSHELL, argv, environ); _exit(127); /* NOTREACHED */ @@ -155,8 +156,7 @@ popen(command, type) cur->fp = iop; cur->pid = pid; THREAD_LOCK(); - cur->next = pidlist; - pidlist = cur; + SLIST_INSERT_HEAD(&pidlist, cur, next); THREAD_UNLOCK(); return (iop); @@ -171,7 +171,7 @@ int pclose(iop) FILE *iop; { - struct pid *cur, *last; + struct pid *cur, *last = NULL; int pstat; pid_t pid; @@ -179,17 +179,19 @@ pclose(iop) * Find the appropriate file pointer and remove it from the list. */ THREAD_LOCK(); - for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) + SLIST_FOREACH(cur, &pidlist, next) { if (cur->fp == iop) break; + last = cur; + } if (cur == NULL) { THREAD_UNLOCK(); return (-1); } if (last == NULL) - pidlist = cur->next; + SLIST_REMOVE_HEAD(&pidlist, next); else - last->next = cur->next; + SLIST_REMOVE_NEXT(&pidlist, last, next); THREAD_UNLOCK(); (void)fclose(iop);