]> git.ozlabs.org Git - ccan/commitdiff
configurator: Print test source without cat
authorKevin Locke <kevin@kevinlocke.name>
Thu, 29 Sep 2016 00:44:44 +0000 (18:44 -0600)
committerDavid Gibson <david@gibson.dropbear.id.au>
Fri, 30 Sep 2016 03:51:27 +0000 (13:51 +1000)
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.

Changes since v2:
- Revert fwrite_noeintr and short read/write changes.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
tools/configurator/configurator.c

index a7cbfa53e724ad7119ba89025f0c58c3b22b5aca..73fdc952b7730be219d85cdcf530957ccf9f60a3 100644 (file)
@@ -407,6 +407,22 @@ static void c12r_errx(int eval, const char *fmt, ...)
        exit(eval);
 }
 
        exit(eval);
 }
 
+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;
+       }
+
+       return copied;
+}
+
 static char *grab_stream(FILE *file)
 {
        size_t max, ret, size = 0;
 static char *grab_stream(FILE *file)
 {
        size_t max, ret, size = 0;
@@ -530,7 +546,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)
                c12r_err(1, "creating %s", INPUT_FILE);
 
        if (!outf)
                c12r_err(1, "creating %s", INPUT_FILE);
 
@@ -561,11 +577,13 @@ static bool run_test(const char *cmd, struct test *test)
                abort();
 
        }
                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);
 
 
        newcmd = strdup(cmd);