fclose out_file
authorTero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 21:13:55 +0200
changeset 98 f195b8195b5a
parent 97 84952fa708d4
child 99 fa21919d105f
fclose out_file
src/pngtile/main.c
--- a/src/pngtile/main.c	Mon Jan 25 21:05:39 2010 +0200
+++ b/src/pngtile/main.c	Mon Jan 25 21:13:55 2010 +0200
@@ -5,6 +5,7 @@
 #include <getopt.h>
 #include <string.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <stdbool.h>
 
 /**
@@ -263,7 +264,10 @@
 
             if (strcmp(out_path, "-") == 0) {
                 // use stdout
-                out_file = stdout;
+                if ((out_file = fdopen(STDOUT_FILENO, "wb")) == NULL) {
+                    log_errno("fdopen: STDOUT_FILENO");
+                    goto error;
+                }
             
             } else if (false /* tmp */) {
                 int fd;
@@ -271,17 +275,15 @@
                 // temporary file for output
                 if ((fd = mkstemp(tmp_name)) < 0) {
                     log_errno("mkstemp");
-                    
-                    continue;
+                    goto error;
                 }
 
                 out_path = tmp_name;
 
                 // open out
-                if ((out_file = fdopen(fd, "w")) == NULL) {
+                if ((out_file = fdopen(fd, "wb")) == NULL) {
                     log_errno("fdopen");
-
-                    continue;
+                    goto error;
                 }
 
             } else {
@@ -307,7 +309,10 @@
 
                 if ((err = pt_image_tile_file(image, &ti, out_file)))
                     log_errno("pt_image_tile_file: %s: %s", img_path, pt_strerror(err));
-
+                
+                // cleanup
+                if (fclose(out_file))
+                    log_warn_errno("fclose: out_file");
             }
         }