Re: [PATCH v2 06/13] configurator: Print test source without cat

David Gibson <[email protected]> Tue, 27 Sep 2016 15:06:31 +1000
Newsgroups org.ozlabs.lists.ccan
Message-ID <[email protected]>
On Thu, Sep 22, 2016 at 09:33:09PM -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.
> 
> Changes since v1:
> - Create fwrite_noeintr to avoid EINTR in fwrite without writing any
>   data.
> - Handle short reads from fread.  This can happen with non-conformant
>   libc or if EINTR occurs after reading some data.
> - Handle short writes from fwrite.  This can happen with non-conformant
>   libc or if EINTR occurs after writing some data.
> 
> Signed-off-by: Kevin Locke <[email protected]>

As with patch 2, I'm not sure about the need for fwrite_noeintr().

> ---
>  tools/configurator/configurator.c | 48 +++++++++++++++++++++++++++++++++++----
>  1 file changed, 43 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/configurator/configurator.c b/tools/configurator/configurator.c
> index 31e3d11..51d7ac8 100644
> --- a/tools/configurator/configurator.c
> +++ b/tools/configurator/configurator.c
> @@ -438,6 +438,42 @@ static size_t fread_noeintr(void *ptr, size_t size, size_t nitems,
>  	return ret;
>  }
>  
> +static size_t fwrite_noeintr(const void *ptr, size_t size, size_t nitems,
> +		FILE *stream)
> +{
> +	size_t ret;
> +
> +	do {
> +		errno = 0;
> +		ret = fwrite(ptr, size, nitems, stream);
> +	} while (ret == 0 && errno == EINTR);
> +
> +	return ret;
> +}
> +
> +static size_t fcopy(FILE *fsrc, FILE *fdst)
> +{
> +	char buffer[BUFSIZ];
> +	size_t copied = 0, rsize;
> +
> +	while ((rsize = fread_noeintr(buffer, 1, BUFSIZ, fsrc)) > 0) {
> +		size_t wsize, wtotal = 0;
> +
> +		while (wtotal < rsize &&
> +		       ((wsize = fwrite_noeintr(buffer + wtotal, 1,
> +						rsize - wtotal, fdst)) > 0)) {
> +			wtotal += wsize;
> +		}
> +
> +		if (wtotal < rsize)
> +			break;
> +
> +		copied += wtotal;
> +	}
> +
> +	return copied;
> +}
> +
>  static char *grab_stream(FILE *file)
>  {
>  	size_t max, ret, size = 0;
> @@ -562,7 +598,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);
>  
> @@ -593,11 +629,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

iQIcBAEBCAAGBQJX6f5WAAoJEGw4ysog2bOSX3AP/j5WDCXCPRNryTZCDWJ79utT
+wNwn4sC9YuiYw/pDXyjj9ueit1P9ESPawIUAxfQAKwNYKJoON0FiVJIWKKBY0xN
4xxj/LWaWMqOPvIiw+N3WA1gvuXqrYCj2wnf9tqOnqwZ9Fjdn2mncDlTT2Jrvifq
LSj7UdF9d5anxfXx1oyVmC4di9FSm9k55GCdLaBUsEsHv9m7BMfUsagm3ERtKfFt
boQDa7HDOs4OlcBQO+8lVUx/rBBoCOkSr/KR2+tuDJvf5XcqXNDtsc8sWeDRhpGY
ODC0Jyw4+n9o55Hl/zH97aZjIxBdBNhDHDOkY7ia4K8rABe5RVlIckmz4hCdDAb9
XGeY5CSh3mgD4ooWXJPh+T0srNmwl+omXRS9CWlllSGnvqX47lbBPqo/G0iQiGXd
5thf5UaI2heU1jEjHDH5sTl7OcdwOiaDPBwMs30Fl0Frf7hXjh1m/h3PNgxFN62h
4FscrYa68RBfYOSXMWK1Snj9lznkfqefSLgm8DDb3dSv/0hU+7RRvUEWbTO3B/Aj
LCW/VXDe688+V5Nr/3falZuMeCfoiUj03u3nPG3XnPqYYbPg44m4vlI9AkIuPpFS
1LLReRv/wd06K2BY6CFT5cFTQBAPLIf/V4qMOJ7/4jcWiNvAViPNwPcJ7lXWwp3b
B+nzi7Zcgu9n9qkuNe3j
=9u94
-----END PGP SIGNATURE-----