Memory and fd leak fixes - ID: 3436668
[platform/upstream/flac.git] / src / metaflac / operations_shorthand_picture.c
index 8685cf5..bc51296 100644 (file)
@@ -1,5 +1,5 @@
 /* metaflac - Command-line FLAC metadata editor
- * Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
+ * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -25,7 +25,9 @@
 #include "options.h"
 #include "utils.h"
 #include "FLAC/assert.h"
-#include "share/grabbag.h" /* for grabbag__picture_parse_specification */
+#include "share/grabbag.h" /* for grabbag__picture_parse_specification() etc */
+
+#include "operations_shorthand.h"
 
 static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write);
 static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename);
@@ -42,7 +44,7 @@ FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_
        FLAC__metadata_iterator_init(iterator, chain);
 
        switch(operation->type) {
-               case OP__IMPORT_PICTURE:
+               case OP__IMPORT_PICTURE_FROM:
                        ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write);
                        if(ok) {
                                /* append PICTURE block */
@@ -150,9 +152,9 @@ FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *pictu
                return false;
        }
        if(0 == strcmp(pic_filename, "-"))
-               f = stdout;
+               f = grabbag__file_get_binary_stdout();
        else
-               f = fopen(pic_filename, "w");
+               f = fopen(pic_filename, "wb");
 
        if(0 == f) {
                fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno));
@@ -161,6 +163,8 @@ FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *pictu
 
        if(fwrite(picture->data.picture.data, 1, len, f) != len) {
                fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename);
+               if(f != stdout)
+                       fclose(f);
                return false;
        }