]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
foomatic-rip: minor code changes 211/head
authorJaiLuthra1 <luthrajaiji@gmail.com>
Wed, 26 Feb 2020 12:45:54 +0000 (18:15 +0530)
committerJaiLuthra1 <luthrajaiji@gmail.com>
Wed, 26 Feb 2020 12:45:54 +0000 (18:15 +0530)
filter/foomatic-rip/foomaticrip.c
filter/foomatic-rip/pdf.h
filter/foomatic-rip/postscript.c

index fb1c253ad8f1f6dc5f47da93693eeed5832e9584..eb7f0b3b023545e3ae40a3809f39842617338ff2 100644 (file)
@@ -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);
index 07e2f32a627e8546c541d29f659a3b6db926ad74..be410fc43c482a0336a87a67feb5aeea26c062b4 100644 (file)
@@ -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
 
index f0ddf0180b4ca6e7aa0200eb44e63bb2380acd07..906666267c778b87dbf15eaf6b47b4662aee5482 100644 (file)
@@ -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;