From e45117d663c721302b850dae9964354cfbcb2d77 Mon Sep 17 00:00:00 2001 From: JaiLuthra1 Date: Wed, 26 Feb 2020 18:15:54 +0530 Subject: [PATCH] foomatic-rip: minor code changes --- filter/foomatic-rip/foomaticrip.c | 42 ++++++------------------------- filter/foomatic-rip/pdf.h | 2 +- filter/foomatic-rip/postscript.c | 32 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/filter/foomatic-rip/foomaticrip.c b/filter/foomatic-rip/foomaticrip.c index fb1c253ad..eb7f0b3b0 100644 --- a/filter/foomatic-rip/foomaticrip.c +++ b/filter/foomatic-rip/foomaticrip.c @@ -711,54 +711,28 @@ int print_file(const char *filename, int convert) return EXIT_PRNERR_NORETRY_BAD_SETTINGS; } + if ((tmpfile = fdopen(fd,"r+")) == 0) { + _log("ERROR: Can't fdopen temporary file\n"); + close(fd); + return 0; + } /* Copy already read data to the tmp file */ if (write(fd,buf,n) != n) { _log("ERROR: Can't copy already read data to temporary file\n"); close(fd); } + /* Copy stdin to the tmp file */ - while ((n = read(0,buf,BUFSIZ)) > 0) { - if (write(fd,buf,n) != n) { - _log("ERROR: Can't copy stdin to temporary file\n"); - close(fd); - } - } + copy_file(tmpfile, stdin, NULL, 0); + /* Rewind tmp file to read it again */ if (lseek(fd,0,SEEK_SET) < 0) { _log("ERROR: Can't rewind temporary file\n"); close(fd); } - char gscommand[65536]; - char output[31] = ""; - int pagecount; - size_t bytes; filename = strdup(tmpfilename); - snprintf(gscommand, 65536, "%s -q -dNOPAUSE -dBATCH -sDEVICE=bbox %s 2>&1 | grep -c HiResBoundingBox", - CUPS_GHOSTSCRIPT, filename); - FILE *pd = popen(gscommand, "r"); - bytes = fread(output, 1, 31, pd); - pclose(pd); - - if (bytes <= 0 || sscanf(output, "%d", &pagecount) < 1) - pagecount = -1; - if (pagecount < 0) { - _log("Unexpected page_count\n"); - return 0; - } - - if (pagecount == 0) { - _log("No pages left, outputting empty file.\n"); - return 1; - } - - _log("File contains %d pages.\n", pagecount); - - if ((tmpfile = fdopen(fd,"rb")) == 0) { - _log("ERROR: Can't fdopen temporary file\n"); - close(fd); - } ret = print_ps(tmpfile, NULL, 0, filename); fclose(tmpfile); unlink(tmpfilename); diff --git a/filter/foomatic-rip/pdf.h b/filter/foomatic-rip/pdf.h index 07e2f32a6..be410fc43 100644 --- a/filter/foomatic-rip/pdf.h +++ b/filter/foomatic-rip/pdf.h @@ -25,7 +25,7 @@ #define pdf_h int print_pdf(FILE *s, const char *alreadyread, size_t len, const char *filename, size_t startpos); -int pdf_count_pages(const char *filemame); +int pdf_count_pages(const char *filename); #endif diff --git a/filter/foomatic-rip/postscript.c b/filter/foomatic-rip/postscript.c index f0ddf0180..906666267 100644 --- a/filter/foomatic-rip/postscript.c +++ b/filter/foomatic-rip/postscript.c @@ -168,10 +168,42 @@ int stream_next_line(dstr_t *line, stream_t *s) return cnt; } +int ps_pages(const char *filename) +{ + char gscommand[65536]; + char output[31] = ""; + int pagecount; + size_t bytes; + snprintf(gscommand, 65536, "%s -q -dNOPAUSE -dBATCH -sDEVICE=bbox %s 2>&1 | grep -c HiResBoundingBox", + CUPS_GHOSTSCRIPT, filename); + FILE *pd = popen(gscommand, "r"); + bytes = fread(output, 1, 31, pd); + pclose(pd); + + if (bytes <= 0 || sscanf(output, "%d", &pagecount) < 1) + pagecount = -1; + + return pagecount; +} + int print_ps(FILE *file, const char *alreadyread, size_t len, const char *filename) { stream_t stream; + if (file != stdin) + { + int pagecount = ps_pages(filename); + if (pagecount < 0) { + _log("Unexpected page count\n"); + return 0; + } + if (pagecount == 0) { + _log("No pages left, outputting empty file.\n"); + return 1; + } + _log("File contains %d pages.\n", pagecount); + } + if (file != stdin && (dup2(fileno(file), fileno(stdin)) < 0)) { _log("Could not dup %s to stdin.\n", filename); return 0; -- 2.47.2