special version of sample formatting for common cases
[platform/upstream/flac.git] / src / flac / main.c
index 45b678c..ae1937e 100644 (file)
@@ -1,5 +1,5 @@
 /* flac - Command-line FLAC encoder/decoder
- * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 
 typedef enum { RAW, WAV, AIF, FLAC, OGGFLAC } FileFormat;
 
-static int do_it();
+static int do_it(void);
 
-static FLAC__bool init_options();
+static FLAC__bool init_options(void);
 static int parse_options(int argc, char *argv[]);
 static int parse_option(int short_option, const char *long_option, const char *option_argument);
-static void free_options();
+static void free_options(void);
 static void add_compression_setting_bool(compression_setting_type_t type, FLAC__bool value);
 static void add_compression_setting_string(compression_setting_type_t type, const char *value);
 static void add_compression_setting_unsigned(compression_setting_type_t type, unsigned value);
 
 static int usage_error(const char *message, ...);
-static void short_usage();
-static void show_version();
-static void show_help();
-static void show_explain();
-static void format_mistake(const char *infilename, const char *wrong, const char *right);
+static void short_usage(void);
+static void show_version(void);
+static void show_help(void);
+static void show_explain(void);
+static void format_mistake(const char *infilename, FileFormat wrong, FileFormat right);
 
 static int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file);
 static int decode_file(const char *infilename);
@@ -83,7 +83,12 @@ static const char *get_decoded_outfilename(const char *infilename);
 static const char *get_outfilename(const char *infilename, const char *suffix);
 
 static void die(const char *message);
+static int conditional_fclose(FILE *f);
 static char *local_strdup(const char *source);
+#ifdef _MSC_VER
+/* There's no strtoll() in MSVC6 so we just write a specialized one */
+static FLAC__int64 local__strtoll(const char *src, char **endptr);
+#endif
 
 
 /*
@@ -94,22 +99,23 @@ static struct share__option long_options_[] = {
        /*
         * general options
         */
-       { "help"             , share__no_argument, 0, 'h' },
-       { "explain"          , share__no_argument, 0, 'H' },
-       { "version"          , share__no_argument, 0, 'v' },
-       { "decode"           , share__no_argument, 0, 'd' },
-       { "analyze"          , share__no_argument, 0, 'a' },
-       { "test"             , share__no_argument, 0, 't' },
-       { "stdout"           , share__no_argument, 0, 'c' },
-       { "silent"           , share__no_argument, 0, 's' },
-       { "totally-silent"   , share__no_argument, 0, 0 },
-       { "force"            , share__no_argument, 0, 'f' },
-       { "delete-input-file", share__no_argument, 0, 0 },
-       { "output-prefix"    , share__required_argument, 0, 0 },
-       { "output-name"      , share__required_argument, 0, 'o' },
-       { "skip"             , share__required_argument, 0, 0 },
-       { "until"            , share__required_argument, 0, 0 },
-       { "channel-map"      , share__required_argument, 0, 0 }, /* undocumented */
+       { "help"              , share__no_argument, 0, 'h' },
+       { "explain"           , share__no_argument, 0, 'H' },
+       { "version"           , share__no_argument, 0, 'v' },
+       { "decode"            , share__no_argument, 0, 'd' },
+       { "analyze"           , share__no_argument, 0, 'a' },
+       { "test"              , share__no_argument, 0, 't' },
+       { "stdout"            , share__no_argument, 0, 'c' },
+       { "silent"            , share__no_argument, 0, 's' },
+       { "totally-silent"    , share__no_argument, 0, 0 },
+       { "warnings-as-errors", share__no_argument, 0, 'w' },
+       { "force"             , share__no_argument, 0, 'f' },
+       { "delete-input-file" , share__no_argument, 0, 0 },
+       { "output-prefix"     , share__required_argument, 0, 0 },
+       { "output-name"       , share__required_argument, 0, 'o' },
+       { "skip"              , share__required_argument, 0, 0 },
+       { "until"             , share__required_argument, 0, 0 },
+       { "channel-map"       , share__required_argument, 0, 0 }, /* undocumented */
 
        /*
         * decoding options
@@ -146,7 +152,7 @@ static struct share__option long_options_[] = {
        { "sector-align"              , share__no_argument, 0, 0 },
        { "seekpoint"                 , share__required_argument, 0, 'S' },
        { "padding"                   , share__required_argument, 0, 'P' },
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        { "ogg"                       , share__no_argument, 0, 0 },
        { "serial-number"             , share__required_argument, 0, 0 },
 #endif
@@ -183,7 +189,7 @@ static struct share__option long_options_[] = {
        { "no-replay-gain"            , share__no_argument, 0, 0 },
        { "no-sector-align"           , share__no_argument, 0, 0 },
        { "no-lax"                    , share__no_argument, 0, 0 },
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        { "no-ogg"                    , share__no_argument, 0, 0 },
 #endif
        { "no-exhaustive-model-search", share__no_argument, 0, 0 },
@@ -192,6 +198,7 @@ static struct share__option long_options_[] = {
        { "no-qlp-coeff-prec-search"  , share__no_argument, 0, 0 },
        { "no-padding"                , share__no_argument, 0, 0 },
        { "no-verify"                 , share__no_argument, 0, 0 },
+       { "no-warnings-as-errors"     , share__no_argument, 0, 0 },
        { "no-residual-gnuplot"       , share__no_argument, 0, 0 },
        { "no-residual-text"          , share__no_argument, 0, 0 },
        /*
@@ -215,6 +222,7 @@ static struct {
        FLAC__bool show_version;
        FLAC__bool mode_decode;
        FLAC__bool verify;
+       FLAC__bool treat_warnings_as_errors;
        FLAC__bool force_file_overwrite;
        FLAC__bool continue_through_decode_errors;
        replaygain_synthesis_spec_t replaygain_synthesis_spec;
@@ -284,7 +292,7 @@ int main(int argc, char *argv[])
        _wildcard(&argc, &argv);
 #endif
 
-       srand(time(0));
+       srand((unsigned)time(0));
        setlocale(LC_ALL, "");
        if(!init_options()) {
                flac__utils_printf(stderr, 1, "ERROR: allocating memory\n");
@@ -300,7 +308,7 @@ int main(int argc, char *argv[])
        return retval;
 }
 
-int do_it()
+int do_it(void)
 {
        int retval = 0;
 
@@ -425,7 +433,7 @@ int do_it()
        }
 
        flac__utils_printf(stderr, 2, "\n");
-       flac__utils_printf(stderr, 2, "flac %s, Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson\n", FLAC__VERSION_STRING);
+       flac__utils_printf(stderr, 2, "flac %s, Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson\n", FLAC__VERSION_STRING);
        flac__utils_printf(stderr, 2, "flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
        flac__utils_printf(stderr, 2, "welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
 
@@ -490,12 +498,13 @@ int do_it()
        return retval;
 }
 
-FLAC__bool init_options()
+FLAC__bool init_options(void)
 {
        option_values.show_help = false;
        option_values.show_explain = false;
        option_values.mode_decode = false;
        option_values.verify = false;
+       option_values.treat_warnings_as_errors = false;
        option_values.force_file_overwrite = false;
        option_values.continue_through_decode_errors = false;
        option_values.replaygain_synthesis_spec.apply = false;
@@ -557,7 +566,7 @@ int parse_options(int argc, char *argv[])
        int short_option;
        int option_index = 1;
        FLAC__bool had_error = false;
-       const char *short_opts = "0123456789aA:b:cdefFhHl:mMo:pP:q:r:sS:tT:vV";
+       const char *short_opts = "0123456789aA:b:cdefFhHl:mMo:pP:q:r:sS:tT:vVw";
 
        while ((short_option = share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
                switch (short_option) {
@@ -625,7 +634,7 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                                char *end;
 #ifdef _MSC_VER
                                FLAC__int64 i;
-                               i = strtol(option_argument, &end, 10); /* [2G limit] */
+                               i = local__strtoll(option_argument, &end);
 #else
                                long long i;
                                i = strtoll(option_argument, &end, 10);
@@ -634,7 +643,7 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                                        return usage_error("ERROR: --%s must be a number\n", long_option);
                                option_values.format_input_size = (off_t)i;
                                if(option_values.format_input_size != i) /* check if off_t is smaller than long long */
-                                       return usage_error("ERROR: --%s too large; this flac does not support filesizes over 2GB\n", long_option);
+                                       return usage_error("ERROR: --%s too large; this build of flac does not support filesizes over 2GB\n", long_option);
                                if(option_values.format_input_size <= 0)
                                        return usage_error("ERROR: --%s must be > 0\n", long_option);
                        }
@@ -709,7 +718,7 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                else if(0 == strcmp(long_option, "sector-align")) {
                        option_values.sector_align = true;
                }
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
                else if(0 == strcmp(long_option, "ogg")) {
                        option_values.use_ogg = true;
                }
@@ -782,7 +791,7 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                else if(0 == strcmp(long_option, "no-lax")) {
                        option_values.lax = false;
                }
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
                else if(0 == strcmp(long_option, "no-ogg")) {
                        option_values.use_ogg = false;
                }
@@ -807,6 +816,9 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                else if(0 == strcmp(long_option, "no-verify")) {
                        option_values.verify = false;
                }
+               else if(0 == strcmp(long_option, "no-warnings-as-errors")) {
+                       option_values.treat_warnings_as_errors = false;
+               }
                else if(0 == strcmp(long_option, "no-residual-gnuplot")) {
                        option_values.aopts.do_residual_gnuplot = false;
                }
@@ -882,6 +894,9 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                        case 'V':
                                option_values.verify = true;
                                break;
+                       case 'w':
+                               option_values.treat_warnings_as_errors = true;
+                               break;
                        case 'S':
                                FLAC__ASSERT(0 != option_argument);
                                if(0 == strcmp(option_argument, "-")) {
@@ -984,7 +999,7 @@ int parse_option(int short_option, const char *long_option, const char *option_a
        return 0;
 }
 
-void free_options()
+void free_options(void)
 {
        unsigned i;
        if(0 != option_values.filenames) {
@@ -1046,16 +1061,16 @@ int usage_error(const char *message, ...)
        return 1;
 }
 
-void show_version()
+void show_version(void)
 {
        printf("flac %s\n", FLAC__VERSION_STRING);
 }
 
-static void usage_header()
+static void usage_header(void)
 {
        printf("===============================================================================\n");
        printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
-       printf("Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson\n");
+       printf("Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson\n");
        printf("\n");
        printf("This program is free software; you can redistribute it and/or\n");
        printf("modify it under the terms of the GNU General Public License\n");
@@ -1073,7 +1088,7 @@ static void usage_header()
        printf("===============================================================================\n");
 }
 
-static void usage_summary()
+static void usage_summary(void)
 {
        printf("Usage:\n");
        printf("\n");
@@ -1084,7 +1099,7 @@ static void usage_summary()
        printf("\n");
 }
 
-void short_usage()
+void short_usage(void)
 {
        usage_header();
        printf("\n");
@@ -1103,7 +1118,7 @@ void short_usage()
        printf("  flac -t [INPUTFILE [...]]\n");
 }
 
-void show_help()
+void show_help(void)
 {
        usage_header();
        usage_summary();
@@ -1117,13 +1132,14 @@ void show_help()
        printf("  -c, --stdout                 Write output to stdout\n");
        printf("  -s, --silent                 Do not write runtime encode/decode statistics\n");
        printf("      --totally-silent         Do not print anything, including errors\n");
+       printf("  -w, --warnings-as-errors     Treat all warnings as errors\n");
        printf("  -f, --force                  Force overwriting of output files\n");
        printf("  -o, --output-name=FILENAME   Force the output file name\n");
        printf("      --output-prefix=STRING   Prepend STRING to output names\n");
        printf("      --delete-input-file      Deletes after a successful encode/decode\n");
        printf("      --skip={#|mm:ss.ss}      Skip the given initial samples for each input\n");
        printf("      --until={#|[+|-]mm:ss.ss}  Stop at the given sample for each input file\n");
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        printf("      --ogg                    Use Ogg as transport layer\n");
        printf("      --serial-number          Serial number to use for the FLAC stream\n");
 #endif
@@ -1144,15 +1160,15 @@ void show_help()
        printf("      --tag-from-file=FIELD=FILENAME   Like --tag but gets value from file\n");
        printf("  -S, --seekpoint={#|X|#x|#s}  Add seek point(s)\n");
        printf("  -P, --padding=#              Write a PADDING block of length #\n");
-       printf("  -0, --compression-level-0, --fast  Synonymous with -l 0 -b 1152 -r 2,2\n");
-       printf("  -1, --compression-level-1          Synonymous with -l 0 -b 1152 -M -r 2,2\n");
+       printf("  -0, --compression-level-0, --fast  Synonymous with -l 0 -b 1152 -r 3\n");
+       printf("  -1, --compression-level-1          Synonymous with -l 0 -b 1152 -M -r 3\n");
        printf("  -2, --compression-level-2          Synonymous with -l 0 -b 1152 -m -r 3\n");
-       printf("  -3, --compression-level-3          Synonymous with -l 6 -b 4608 -r 3,3\n");
-       printf("  -4, --compression-level-4          Synonymous with -l 8 -b 4608 -M -r 3,3\n");
-       printf("  -5, --compression-level-5          Synonymous with -l 8 -b 4608 -m -r 3,3\n");
-       printf("  -6, --compression-level-6          Synonymous with -l 8 -b 4608 -m -r 4\n");
-       printf("  -7, --compression-level-7          Synonymous with -l 8 -b 4608 -m -e -r 6\n");
-       printf("  -8, --compression-level-8, --best  Synonymous with -l 12 -b 4608 -m -e -r 6\n");
+       printf("  -3, --compression-level-3          Synonymous with -l 6 -b 4096 -r 4\n");
+       printf("  -4, --compression-level-4          Synonymous with -l 8 -b 4096 -M -r 4\n");
+       printf("  -5, --compression-level-5          Synonymous with -l 8 -b 4096 -m -r 5\n");
+       printf("  -6, --compression-level-6          Synonymous with -l 8 -b 4096 -m -r 6\n");
+       printf("  -7, --compression-level-7          Synonymous with -l 8 -b 4096 -m -e -r 6\n");
+       printf("  -8, --compression-level-8, --best  Synonymous with -l 12 -b 4096 -m -e -r 6\n");
        printf("  -b, --blocksize=#                  Specify blocksize in samples\n");
        printf("  -m, --mid-side                     Try mid-side coding for each frame\n");
        printf("  -M, --adaptive-mid-side            Adaptive mid-side coding for all frames\n");
@@ -1178,7 +1194,7 @@ void show_help()
        printf("      --no-exhaustive-model-search\n");
        printf("      --no-lax\n");
        printf("      --no-mid-side\n");
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        printf("      --no-ogg\n");
 #endif
        printf("      --no-padding\n");
@@ -1191,9 +1207,10 @@ void show_help()
        printf("      --no-silent\n");
        printf("      --no-force\n");
        printf("      --no-verify\n");
+       printf("      --no-warnings-as-errors\n");
 }
 
-void show_explain()
+void show_explain(void)
 {
        usage_header();
        usage_summary();
@@ -1229,6 +1246,7 @@ void show_explain()
        printf("      --totally-silent         Do not print anything of any kind, including\n");
        printf("                               warnings or errors.  The exit code will be the\n");
        printf("                               only way to determine successful completion.\n");
+       printf("  -w, --warnings-as-errors     Treat all warnings as errors\n");
        printf("  -f, --force                  Force overwriting of output files\n");
        printf("  -o, --output-name=FILENAME   Force the output file name; usually flac just\n");
        printf("                               changes the extension.  May only be used when\n");
@@ -1256,7 +1274,7 @@ void show_explain()
        printf("                               relative to the --skip point.  If a `-' sign is\n");
        printf("                               at the beginning, the --until point is relative\n");
        printf("                               to end of the audio.\n");
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        printf("      --ogg                    When encoding, generate Ogg FLAC output instead\n");
        printf("                               of native FLAC.  Ogg FLAC streams are FLAC\n");
        printf("                               streams wrapped in an Ogg transport layer.  The\n");
@@ -1268,9 +1286,10 @@ void show_explain()
        printf("                               '.ogg'.\n");
        printf("      --serial-number          Serial number to use for the FLAC stream.  When\n");
        printf("                               encoding and no serial number is given, flac\n");
-       printf("                               uses '0'.  When decoding and no number is\n");
-       printf("                               given, flac uses the serial number of the first\n");
-       printf("                               page.\n");
+       printf("                               uses a random one.  If encoding to multiple files\n");
+       printf("                               the serial number is incremented for each file.\n");
+       printf("                               When decoding and no number is given, flac uses\n");
+       printf("                               the serial number of the first page.\n");
 #endif
        printf("analysis options:\n");
        printf("      --residual-text          Include residual signal in text output.  This\n");
@@ -1324,10 +1343,13 @@ void show_explain()
        printf("                               --no-cued-seekpoints is specified.\n");
        printf("      --picture=SPECIFICATION  Import a picture and store it in a PICTURE block.\n");
        printf("                               More than one --picture command can be specified.\n");
-       printf("                               The SPECIFICATION is a string whose parts are\n");
-       printf("                               separated by | characters.  Some parts may be\n");
-       printf("                               left empty to invoke default values.  The format:\n");
-       printf("         [TYPE]|MIME-TYPE|[DESCRIPTION]|[WIDTHxHEIGHTxDEPTH[/COLORS]]|FILE\n");
+       printf("                               The SPECIFICATION can either be a simple filename\n");
+       printf("                               for the picture file, or a complete specification\n");
+       printf("                               whose parts are separated by | characters.  Some\n");
+       printf("                               parts may be left empty to invoke default values.\n");
+       printf("                               Using a filename is shorthand for \"||||FILE\".\n");
+       printf("                               The SPECIFICATION format is:\n");
+       printf("         [TYPE]|[MIME-TYPE]|[DESCRIPTION]|[WIDTHxHEIGHTxDEPTH[/COLORS]]|FILE\n");
        printf("           TYPE is optional; it is a number from one of:\n");
        printf("              0: Other\n");
        printf("              1: 32x32 pixels 'file icon' (PNG only)\n");
@@ -1352,10 +1374,11 @@ void show_explain()
        printf("             20: Publisher/Studio logotype\n");
        printf("             The default is 3 (front cover).  There may only be one picture each\n");
        printf("             of type 1 and 2 in a file.\n");
-       printf("           MIME-TYPE is mandatory; for best compatibility with players, use\n");
-       printf("             pictures with MIME type image/jpeg or image/png.  The MIME type can\n");
-       printf("             also be --> to mean that FILE is actually a URL to an image, though\n");
-       printf("             this use is discouraged.\n");
+       printf("           MIME-TYPE is optional; if left blank, it will be detected from the\n");
+       printf("             file.  For best compatibility with players, use pictures with MIME\n");
+       printf("             type image/jpeg or image/png.  The MIME type can also be --> to\n");
+       printf("             mean that FILE is actually a URL to an image, though this use is\n");
+       printf("             discouraged.\n");
        printf("           DESCRIPTION is optional; the default is an empty string\n");
        printf("           The next part specfies the resolution and color information.  If\n");
        printf("             the MIME-TYPE is image/jpeg, image/png, or image/gif, you can\n");
@@ -1406,20 +1429,20 @@ void show_explain()
        printf("                               block of 8192 bytes by default, or 65536 bytes\n");
        printf("                               if the input audio is more than 20 minutes long.\n");
        printf("  -b, --blocksize=#            Specify the blocksize in samples; the default is\n");
-       printf("                               1152 for -l 0, else 4608; must be one of 192,\n");
+       printf("                               1152 for -l 0, else 4096; must be one of 192,\n");
        printf("                               576, 1152, 2304, 4608, 256, 512, 1024, 2048,\n");
        printf("                               4096 (and 8192 or 16384 if the sample rate is\n");
        printf("                               >48kHz) for Subset streams.\n");
-       printf("  -0, --compression-level-0, --fast  Synonymous with -l 0 -b 1152 -r 2,2\n");
-       printf("  -1, --compression-level-1          Synonymous with -l 0 -b 1152 -M -r 2,2\n");
+       printf("  -0, --compression-level-0, --fast  Synonymous with -l 0 -b 1152 -r 3\n");
+       printf("  -1, --compression-level-1          Synonymous with -l 0 -b 1152 -M -r 3\n");
        printf("  -2, --compression-level-2          Synonymous with -l 0 -b 1152 -m -r 3\n");
-       printf("  -3, --compression-level-3          Synonymous with -l 6 -b 4608 -r 3,3\n");
-       printf("  -4, --compression-level-4          Synonymous with -l 8 -b 4608 -M -r 3,3\n");
-       printf("  -5, --compression-level-5          Synonymous with -l 8 -b 4608 -m -r 3,3\n");
+       printf("  -3, --compression-level-3          Synonymous with -l 6 -b 4096 -r 4\n");
+       printf("  -4, --compression-level-4          Synonymous with -l 8 -b 4096 -M -r 4\n");
+       printf("  -5, --compression-level-5          Synonymous with -l 8 -b 4096 -m -r 5\n");
        printf("                                     -5 is the default setting\n");
-       printf("  -6, --compression-level-6          Synonymous with -l 8 -b 4608 -m -r 4\n");
-       printf("  -7, --compression-level-7          Synonymous with -l 8 -b 4608 -m -e -r 6\n");
-       printf("  -8, --compression-level-8, --best  Synonymous with -l 12 -b 4608 -m -e -r 6\n");
+       printf("  -6, --compression-level-6          Synonymous with -l 8 -b 4096 -m -r 6\n");
+       printf("  -7, --compression-level-7          Synonymous with -l 8 -b 4096 -m -e -r 6\n");
+       printf("  -8, --compression-level-8, --best  Synonymous with -l 12 -b 4096 -m -e -r 6\n");
        printf("  -m, --mid-side                     Try mid-side coding for each frame\n");
        printf("                                     (stereo only)\n");
        printf("  -M, --adaptive-mid-side            Adaptive mid-side coding for all frames\n");
@@ -1479,7 +1502,7 @@ void show_explain()
        printf("      --no-exhaustive-model-search\n");
        printf("      --no-lax\n");
        printf("      --no-mid-side\n");
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        printf("      --no-ogg\n");
 #endif
        printf("      --no-padding\n");
@@ -1491,11 +1514,14 @@ void show_explain()
        printf("      --no-silent\n");
        printf("      --no-force\n");
        printf("      --no-verify\n");
+       printf("      --no-warnings-as-errors\n");
 }
 
-void format_mistake(const char *infilename, const char *wrong, const char *right)
+void format_mistake(const char *infilename, FileFormat wrong, FileFormat right)
 {
-       flac__utils_printf(stderr, 1, "WARNING: %s is not a %s file; treating as a %s file\n", infilename, wrong, right);
+       /* WATCHOUT: indexed by FileFormat */
+       static const char * const ff[] = { "raw", "WAVE", "AIFF", "FLAC", "Ogg FLAC" };
+       flac__utils_printf(stderr, 1, "WARNING: %s is not a %s file; treating as a %s file\n", infilename, ff[wrong], ff[right]);
 }
 
 int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file)
@@ -1544,12 +1570,21 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
 
                /* attempt to guess the file type based on the first 12 bytes */
                if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
-                       if(fmt != RAW)
-                               format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
+                       if(fmt != RAW) {
+                               format_mistake(infilename, fmt, RAW);
+                               if(option_values.treat_warnings_as_errors) {
+                                       conditional_fclose(encode_infile);
+                                       return 1;
+                               }
+                       }
                        fmt= RAW;
                }
                else {
-                       if(!strncmp((const char *)lookahead, "RIFF", 4) && !strncmp((const char *)lookahead+8, "WAVE", 4))
+                       if(!strncmp((const char *)lookahead, "ID3", 3)) {
+                               flac__utils_printf(stderr, 1, "ERROR: input file %s has an ID3v2 tag\n", infilename);
+                               return 1;
+                       }
+                       else if(!strncmp((const char *)lookahead, "RIFF", 4) && !strncmp((const char *)lookahead+8, "WAVE", 4))
                                fmt= WAV;
                        else if(!strncmp((const char *)lookahead, "FORM", 4) && !strncmp((const char *)lookahead+8, "AIFF", 4))
                                fmt= AIF;
@@ -1563,8 +1598,13 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                        else if(!memcmp(lookahead, "OggS", 4))
                                fmt= OGGFLAC;
                        else {
-                               if(fmt != RAW)
-                                       format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
+                               if(fmt != RAW) {
+                                       format_mistake(infilename, fmt, RAW);
+                                       if(option_values.treat_warnings_as_errors) {
+                                               conditional_fclose(encode_infile);
+                                               return 1;
+                                       }
+                               }
                                fmt= RAW;
                        }
                }
@@ -1599,12 +1639,14 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                }
                else
                        flac__utils_printf(stderr, 1, "ERROR: output file %s already exists, use -f to override\n", outfilename);
+               conditional_fclose(encode_infile);
                return 1;
        }
 
        if(option_values.format_input_size >= 0) {
                if (fmt != RAW || infilesize >= 0) {
                        flac__utils_printf(stderr, 1, "ERROR: can only use --input-size when encoding raw samples from stdin\n");
+                       conditional_fclose(encode_infile);
                        return 1;
                }
                else {
@@ -1614,36 +1656,49 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
 
        if(option_values.sector_align && (fmt == FLAC || fmt == OGGFLAC)) {
                flac__utils_printf(stderr, 1, "ERROR: can't use --sector-align when the input file is FLAC or Ogg FLAC\n");
+               conditional_fclose(encode_infile);
                return 1;
        }
        if(option_values.sector_align && fmt == RAW && infilesize < 0) {
                flac__utils_printf(stderr, 1, "ERROR: can't use --sector-align when the input size is unknown\n");
+               conditional_fclose(encode_infile);
                return 1;
        }
 
        if(fmt == RAW) {
-               if(option_values.format_is_big_endian < 0 || option_values.format_is_unsigned_samples < 0 || option_values.format_channels < 0 || option_values.format_bps < 0 || option_values.format_sample_rate < 0)
+               if(option_values.format_is_big_endian < 0 || option_values.format_is_unsigned_samples < 0 || option_values.format_channels < 0 || option_values.format_bps < 0 || option_values.format_sample_rate < 0) {
+                       conditional_fclose(encode_infile);
                        return usage_error("ERROR: for encoding a raw file you must specify a value for --endian, --sign, --channels, --bps, and --sample-rate\n");
+               }
        }
 
        if(encode_infile == stdin || option_values.force_to_stdout) {
-               if(option_values.replay_gain)
+               if(option_values.replay_gain) {
+                       conditional_fclose(encode_infile);
                        return usage_error("ERROR: --replay-gain cannot be used when encoding to stdout\n");
+               }
        }
-       if(option_values.replay_gain && option_values.use_ogg)
+       if(option_values.replay_gain && option_values.use_ogg) {
+               conditional_fclose(encode_infile);
                return usage_error("ERROR: --replay-gain cannot be used when encoding to Ogg FLAC yet\n");
+       }
 
-       if(!flac__utils_parse_skip_until_specification(option_values.skip_specification, &common_options.skip_specification) || common_options.skip_specification.is_relative)
+       if(!flac__utils_parse_skip_until_specification(option_values.skip_specification, &common_options.skip_specification) || common_options.skip_specification.is_relative) {
+               conditional_fclose(encode_infile);
                return usage_error("ERROR: invalid value for --skip\n");
+       }
 
-       if(!flac__utils_parse_skip_until_specification(option_values.until_specification, &common_options.until_specification)) /*@@@@ more checks: no + without --skip, no - unless known total_samples_to_{en,de}code */
+       if(!flac__utils_parse_skip_until_specification(option_values.until_specification, &common_options.until_specification)) { /*@@@@ more checks: no + without --skip, no - unless known total_samples_to_{en,de}code */
+               conditional_fclose(encode_infile);
                return usage_error("ERROR: invalid value for --until\n");
+       }
        /* if there is no "--until" we want to default to "--until=-0" */
        if(0 == option_values.until_specification)
                common_options.until_specification.is_relative = true;
 
        common_options.verify = option_values.verify;
-#ifdef FLAC__HAS_OGG
+       common_options.treat_warnings_as_errors = option_values.treat_warnings_as_errors;
+#if FLAC__HAS_OGG
        common_options.use_ogg = option_values.use_ogg;
        /* set a random serial number if one has not yet been specified */
        if(!option_values.has_serial_number) {
@@ -1660,6 +1715,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
        common_options.requested_seek_points = option_values.requested_seek_points;
        common_options.num_requested_seek_points = option_values.num_requested_seek_points;
        common_options.cuesheet_filename = option_values.cuesheet_filename;
+       common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
        common_options.cued_seekpoints = option_values.cued_seekpoints;
        common_options.channel_map_none = option_values.channel_map_none;
        common_options.is_first_file = is_first_file;
@@ -1682,6 +1738,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                /*@@@@ still a remote possibility that a file with this filename exists */
                if(0 == (internal_outfilename = malloc(strlen(outfilename)+strlen(tmp_suffix)+1))) {
                        flac__utils_printf(stderr, 1, "ERROR allocating memory for tempfile name\n");
+                       conditional_fclose(encode_infile);
                        return 1;
                }
                strcpy(internal_outfilename, outfilename);
@@ -1782,7 +1839,7 @@ int decode_file(const char *infilename)
         * Error if output file already exists (and -f not used).
         * Use grabbag__file_get_filesize() as a cheap way to check.
         */
-       if(!option_values.test_only && !option_values.force_file_overwrite && grabbag__file_get_filesize(outfilename) != (off_t)(-1)) {
+       if(!option_values.test_only && !option_values.force_file_overwrite && strcmp(outfilename, "-") && grabbag__file_get_filesize(outfilename) != (off_t)(-1)) {
                flac__utils_printf(stderr, 1, "ERROR: output file %s already exists, use -f to override\n", outfilename);
                return 1;
        }
@@ -1799,7 +1856,7 @@ int decode_file(const char *infilename)
        else
                treat_as_ogg = false;
 
-#ifndef FLAC__HAS_OGG
+#if !FLAC__HAS_OGG
        if(treat_as_ogg) {
                flac__utils_printf(stderr, 1, "%s: Ogg support has not been built into this copy of flac\n", infilename);
                return 1;
@@ -1823,9 +1880,10 @@ int decode_file(const char *infilename)
        else
                common_options.has_cue_specification = false;
 
+       common_options.treat_warnings_as_errors = option_values.treat_warnings_as_errors;
        common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
        common_options.replaygain_synthesis_spec = option_values.replaygain_synthesis_spec;
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        common_options.is_ogg = treat_as_ogg;
        common_options.use_first_serial_number = !option_values.has_serial_number;
        common_options.serial_number = option_values.serial_number;
@@ -1904,7 +1962,8 @@ const char *get_outfilename(const char *infilename, const char *suffix)
                                return 0;
                        if (flac__strlcat(buffer, infilename, sizeof buffer) >= sizeof buffer)
                                return 0;
-                       if(0 == (p = strrchr(buffer, '.'))) {
+                       /* the . must come after any / to avoid problems with, e.g. "some.directory/extensionless-filename" */
+                       if(0 == (p = strrchr(buffer, '.')) || strchr(p, '/')) {
                                if (flac__strlcat(buffer, suffix, sizeof buffer) >= sizeof buffer)
                                        return 0;
                        }
@@ -1927,6 +1986,14 @@ void die(const char *message)
        exit(1);
 }
 
+int conditional_fclose(FILE *f)
+{
+       if(f == 0 || f == stdin || f == stdout)
+               return 0;
+       else
+               return fclose(f);
+}
+
 char *local_strdup(const char *source)
 {
        char *ret;
@@ -1935,3 +2002,29 @@ char *local_strdup(const char *source)
                die("out of memory during strdup()");
        return ret;
 }
+
+#ifdef _MSC_VER
+/* There's no strtoll() in MSVC6 so we just write a specialized one */
+FLAC__int64 local__strtoll(const char *src, char **endptr)
+{
+       FLAC__bool neg = false;
+       FLAC__int64 ret = 0;
+       int c;
+       FLAC__ASSERT(0 != src);
+       if(*src == '-') {
+               neg = true;
+               src++;
+       }
+       while(0 != (c = *src)) {
+               c -= '0';
+               if(c >= 0 && c <= 9)
+                       ret = (ret * 10) + c;
+               else
+                       break;
+               src++;
+       }
+       if(endptr)
+               *endptr = (char*)src;
+       return neg? -ret : ret;
+}
+#endif