[ccan] [PATCH 2/9] configurator: Reimplement run using popen

Kevin Locke kevin at kevinlocke.name
Tue Sep 20 16:22:41 AEST 2016


On 09/19/2016 11:00 PM, David Gibson wrote:
 > On Sun, Sep 18, 2016 at 06:51:59PM -0600, Kevin Locke wrote:
 >> Rather than using fork+pipe+system+waitpid, most of which are only
 >> available on POSIX-like systems, use popen which is also available on
 >> Windows (under the name _popen).
 >
 > Concept looks good, one little wart.
 >
 >> [...]
 >>
 >> -static char *grab_fd(int fd)
 >> +static char *grab_stream(FILE *file)
 >>  {
 >> -	int ret;
 >> -	size_t max, size = 0;
 >> +	size_t max, ret, size = 0;
 >>  	char *buffer;
 >>
 >> -	max = 16384;
 >> -	buffer = malloc(max+1);
 >> -	while ((ret = read(fd, buffer + size, max - size)) > 0) {
 >> +	max = BUFSIZ;
 >> +	buffer = malloc(max);
 >> +	while ((ret = fread(buffer+size, 1, max - size, file)) == max - 
size) {
 >
 > This assumes that fread() will never return a short read except on EOF
 > or error.  I expect that will be true in practice for regular files,
 > but I'm not sure if it's a good idea to rely on it.

Interesting.  I was under the impression that the fread couldn't short 
read.  POSIX describes the return value as "the number of elements 
successfully read which is less than nitems only if a read error or 
end-of-file is encountered."[1]  Now that I think about it, I suppose 
EINTR could be an issue for non-glibc systems.  Is that what you had in 
mind, or are there other issues I'm overlooking?  (Also for fwrite in 6/9?)

Kevin

1.  http://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html


More information about the ccan mailing list