[ccan] [PATCH 6/9] configurator: Print test source without cat

David Gibson david at gibson.dropbear.id.au
Tue Sep 20 15:09:06 AEST 2016


On Sun, Sep 18, 2016 at 06:52:03PM -0600, Kevin Locke wrote:
> Windows does not provide cat.  Instead, copy the test source to stdout
> using the file stream to which it was written.
> 
> Signed-off-by: Kevin Locke <kevin at kevinlocke.name>
> ---
>  tools/configurator/configurator.c | 28 +++++++++++++++++++++++-----
>  1 file changed, 23 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/configurator/configurator.c b/tools/configurator/configurator.c
> index 0307635..0386930 100644
> --- a/tools/configurator/configurator.c
> +++ b/tools/configurator/configurator.c
> @@ -422,6 +422,22 @@ static void errx(int status, const char *fmt, ...)
>  	va_end(ap);
>  }
>  
> +static size_t fcopy(FILE *fsrc, FILE *fdst)
> +{
> +	char buffer[BUFSIZ];
> +	size_t rsize, wsize;
> +	size_t copied = 0;
> +
> +	while ((rsize = fread(buffer, 1, BUFSIZ, fsrc)) > 0) {
> +		wsize = fwrite(buffer, 1, rsize, fdst);
> +		copied += wsize;
> +		if (wsize != rsize)
> +			break;

Similar to 2/9 you're assuming you'll never get a short fwrite() here.
Since the output stream is very likely to be a terminal in this case,
that seems a lot more likely to break than just assuming no short
reads on fread().  I think this one really does need to handle short
writes here.

> +	}
> +
> +	return copied;
> +}
> +
>  static char *grab_stream(FILE *file)
>  {
>  	size_t max, ret, size = 0;
> @@ -545,7 +561,7 @@ static bool run_test(const char *cmd, struct test *test)
>  		}
>  	}
>  
> -	outf = fopen(INPUT_FILE, "w");
> +	outf = fopen(INPUT_FILE, verbose > 1 ? "w+" : "w");
>  	if (!outf)
>  		err(1, "creating %s", INPUT_FILE);
>  
> @@ -576,11 +592,13 @@ static bool run_test(const char *cmd, struct test *test)
>  		abort();
>  
>  	}
> -	fclose(outf);
>  
> -	if (verbose > 1)
> -		if (system("cat " INPUT_FILE) == -1)
> -			;
> +	if (verbose > 1) {
> +		fseek(outf, 0, SEEK_SET);
> +		fcopy(outf, stdout);
> +	}
> +
> +	fclose(outf);
>  
>  	newcmd = strdup(cmd);
>  

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20160920/afa9f0df/attachment.sig>


More information about the ccan mailing list