diff options
author | ed <ed@FreeBSD.org> | 2008-07-29 16:29:59 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2008-07-29 16:29:59 +0000 |
commit | 671e6f698dce6e5009b44a12bbb199809ea267c7 (patch) | |
tree | 881504ff11268315cb4a08c04c2a213f0506acf2 /lib/libc | |
parent | bfc8f843504c60ca9c53b2b9baa676083ce4fef8 (diff) | |
download | FreeBSD-src-671e6f698dce6e5009b44a12bbb199809ea267c7.zip FreeBSD-src-671e6f698dce6e5009b44a12bbb199809ea267c7.tar.gz |
Convert popen()'s `pidlist' to a SLIST, for consistency.
I guess the original author of the popen() code didn't want to use our
<sys/queue.h> 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)
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/gen/popen.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/libc/gen/popen.c b/lib/libc/gen/popen.c index 9e597b9..f3450b5 100644 --- a/lib/libc/gen/popen.c +++ b/lib/libc/gen/popen.c @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" #include <sys/param.h> +#include <sys/queue.h> #include <sys/wait.h> #include <signal.h> @@ -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); |