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

David Gibson <[email protected]> Tue, 20 Sep 2016 15:09:06 +1000
Newsgroups org.ozlabs.lists.ccan
Message-ID <20160920050906.GP20488@umbus>
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 <[email protected]>
> ---
>  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

_______________________________________________
ccan mailing list
[email protected]
https://lists.ozlabs.org/listinfo/ccan
signature.asc (application/pgp-signature, 819 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJX4MRyAAoJEGw4ysog2bOSt1QQALe0El+6ACPbrBWI2ylNvZE7
g16A9WqSd602MuaaY2P5WIsxP5uaJjkXUm15lho2BbDQI7Vq3sKrV33t4ramhY1m
a8V+adDsGf6TCZubm7Znwv2gaSqR/CZxz9FNv0qBMXviccmdqNRYv/D5JtadmrVW
X4ti9a2hNeNLfwf6GRwS2X5M67TW7BEjLrat6Jt5Jqnso9M1aXK4mijYd0z0n0uD
hCH4G3scX5abH0h3MaXorwcIoQ0U+74Zu7V1d5wC++Sad5qwXwDvcirIuoIcbEH+
PIybPJ09OClJI/ID/xQPRPvWCdHyYRoqQoNNy4op/gATbK53qzxDJ/N3DIs/tYBY
tbPqIlbMjQ7ECTLsiqpmfiV3oNPW46dr+Z0va2mxeAy3lI0A300/wPgKBvWTk0LP
V+6mpURMseDsVvJtRXxK072EpQk6dwSO522pzHDweWz/boGSMHpbK1YbTyfjeh0e
QeNqTlvzrpWRPe8P5flOi6gImT2K81yxw2J/c+W4RFOg6SSsAme9ZhvUIifq5Cp0
LwCsbaGVpzxhVm9uqNp/1rfet8kYDHczXIOV7UQ7BJ7A4hCNq8yf8imJsKhGQ2ri
ScZAQjeRWOROM+Fue+xmyDfZJA8I/wJAs02OeLCHJ/oMCWm2PhCbMVz0CutNabbg
cFQqB9ZBfdRdByMs3gGx
=iZhy
-----END PGP SIGNATURE-----