--- libelwix/src/pio.c 2013/12/05 15:04:20 1.1.2.2 +++ libelwix/src/pio.c 2013/12/05 15:38:14 1.1.2.4 @@ -26,6 +26,9 @@ e_popen(const char *command, const char *type, pid_t * int pdes[2], pid, twoway, cloexec; char *argv[4]; + if (!command || !type) + return NULL; + cloexec = strchr(type, 'e') != NULL; /* * Lite2 introduced two-way popen() pipes using _socketpair(). @@ -140,6 +143,9 @@ e_pclose(FILE *iop) int pstat; pid_t pid; + if (!iop) + return -1; + /* * Find the appropriate file pointer and remove it from the list. */ @@ -168,4 +174,60 @@ e_pclose(FILE *iop) e_free(cur); return (pid == -1 ? -1 : pstat); +} + +/* + * pio_pgetpid() - Get tagPIOPID structure from file handle + * + * @iop = popen handle + * return: NULL error or !=NULL tagPIOPID structure + */ +struct tagPIOPID * +pio_pgetpid(FILE * __restrict iop) +{ + struct tagPIOPID *p; + + if (!iop) + return NULL; + + THREAD_LOCK(); + SLIST_FOREACH(p, &pio_pidlist, next) + if (p->fp == iop) + break; + THREAD_UNLOCK(); + + return p; +} + +/* + * pio_pchkpid() - Check exit status of child programs + * + * @pids = return tagPIOPID structures of exited programs, + * if !=NULL must call array_Destroy() + * return: -1 error or >-1 exited programs + */ +int +pio_pchkpid(array_t ** __restrict pids) +{ + register int ret = 0; + struct tagPIOPID *p; + array_t *pa; + + if (pids) { + if (!(pa = array_Init(0))) + return -1; + else + *pids = pa; + } + + THREAD_LOCK(); + SLIST_FOREACH(p, &pio_pidlist, next) + if (p->fp && waitpid(p->pid, &p->stat, WNOHANG) > 0) { + if (pids) + array_Push(pa, p, 0); + ret++; + } + THREAD_UNLOCK(); + + return ret; }