From e4c69f10c8729812176ef49171e79e9dd8cd983f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 10 Feb 2014 13:14:37 +0100 Subject: Fix reading from stdin. fread sucks. --- src/pwgui/main.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/pwgui/main.cpp b/src/pwgui/main.cpp index 6e12eee..fa131b1 100644 --- a/src/pwgui/main.cpp +++ b/src/pwgui/main.cpp @@ -98,17 +98,25 @@ int main(int argc, char *argv[]) return CUPS_BACKEND_FAILED; } char buffer[BUFLEN]; - size_t bytes, ret; - while (!feof(stdin)) { - bytes = fread(buffer, BUFLEN, 1, stdin); + int bytes, ret; + int total = 0; + for (;;) { + bytes = read(STDIN_FILENO, buffer, BUFLEN); if (bytes == 0) break; + if (bytes < 0) { + fprintf(stderr, "ERROR: Could not read print job from STDIN.\n"); + remove(tmpfile); + return CUPS_BACKEND_FAILED; + } if ((ret = write(fh, buffer, bytes)) != bytes) { fprintf(stderr, "ERROR: Could not write %d bytes to %s (wrote %d)\n", (int)bytes, tmpfile, (int)ret); remove(tmpfile); return CUPS_BACKEND_FAILED; } + total += bytes; } close(fh); + fprintf(stderr, "ERROR: Read %d bytes from stdin.\n", total); // } else { // File given, check if file exists @@ -168,7 +176,7 @@ int main(int argc, char *argv[]) status = run_backend(backend, device, argv[1], creds, argv[3], argv[4], argv[5], tmpfile, pass); } while (status != CUPS_BACKEND_OK); remove(tmpfile); - fprintf(stderr, "Job submitted.\n"); + fprintf(stderr, "ERROR: Job submitted.\n"); return CUPS_BACKEND_OK; } -- cgit v1.2.3-55-g7522