Enable utf8 I/O stuff when compiling with MinGW.
[platform/upstream/flac.git] / src / flac / main.c
index 3880d09..62e54a4 100644 (file)
@@ -37,6 +37,7 @@
 #include "share/alloc.h"
 #include "share/grabbag.h"
 #include "share/compat.h"
+#include "share/safe_str.h"
 #include "analyze.h"
 #include "decode.h"
 #include "encode.h"
@@ -303,6 +304,12 @@ int main(int argc, char *argv[])
        _response(&argc, &argv);
        _wildcard(&argc, &argv);
 #endif
+#ifdef _WIN32
+       if (get_utf8_argv(&argc, &argv) != 0) {
+               fprintf(stderr, "ERROR: failed to convert command line parameters to UTF-8\n");
+               return 1;
+       }
+#endif
 
        srand((unsigned)time(0));
        setlocale(LC_ALL, "");
@@ -977,8 +984,8 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                                                return usage_error("ERROR: too many seekpoints requested\n");
                                        }
                                        else {
-                                               strcat(option_values.requested_seek_points, option_argument);
-                                               strcat(option_values.requested_seek_points, ";");
+                                               size_t len = strlen(option_values.requested_seek_points);
+                                               flac_snprintf(option_values.requested_seek_points+len, sizeof(option_values.requested_seek_points) - len, "%s;", option_argument);
                                        }
                                }
                                break;
@@ -1281,6 +1288,7 @@ void show_help(void)
        printf("      --force-wave64-format    Force decoding to Wave64 format\n");
        printf("negative options:\n");
        printf("      --no-adaptive-mid-side\n");
+       printf("      --no-cued-seekpoints\n");
        printf("      --no-decode-through-errors\n");
        printf("      --no-delete-input-file\n");
        printf("      --no-preserve-modtime\n");
@@ -1600,7 +1608,7 @@ void show_explain(void)
        printf("      --input-size=#           Size of the raw input in bytes.  If you are\n");
        printf("                               encoding raw samples from stdin, you must set\n");
        printf("                               this option in order to be able to use --skip,\n");
-       printf("                               --until, --cue-sheet, or other options that need\n");
+       printf("                               --until, --cuesheet, or other options that need\n");
        printf("                               to know the size of the input beforehand.  If\n");
        printf("                               the size given is greater than what is found in\n");
        printf("                               the input stream, the encoder will complain\n");
@@ -1625,6 +1633,7 @@ void show_explain(void)
        printf("                               auto-detected.\n");
        printf("negative options:\n");
        printf("      --no-adaptive-mid-side\n");
+       printf("      --no-cued-seekpoints\n");
        printf("      --no-decode-through-errors\n");
        printf("      --no-delete-input-file\n");
        printf("      --no-preserve-modtime\n");
@@ -1681,7 +1690,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
        }
        else {
                infilesize = grabbag__file_get_filesize(infilename);
-               if(0 == (encode_infile = fopen(infilename, "rb"))) {
+               if(0 == (encode_infile = flac_fopen(infilename, "rb"))) {
                        flac__utils_printf(stderr, 1, "ERROR: can't open input file %s: %s\n", infilename, strerror(errno));
                        return 1;
                }
@@ -1894,14 +1903,15 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
        /* if infilename and outfilename point to the same file, we need to write to a temporary file */
        if(encode_infile != stdin && grabbag__file_are_same(infilename, outfilename)) {
                static const char *tmp_suffix = ".tmp,fl-ac+en'c";
+               size_t dest_len = strlen(outfilename) + strlen(tmp_suffix) + 1;
                /*@@@@ still a remote possibility that a file with this filename exists */
-               if(0 == (internal_outfilename = safe_malloc_add_3op_(strlen(outfilename), /*+*/strlen(tmp_suffix), /*+*/1))) {
+               if(0 == (internal_outfilename = safe_malloc_(dest_len))) {
                        flac__utils_printf(stderr, 1, "ERROR allocating memory for tempfile name\n");
                        conditional_fclose(encode_infile);
                        return 1;
                }
-               strcpy(internal_outfilename, outfilename);
-               strcat(internal_outfilename, tmp_suffix);
+               safe_strncpy(internal_outfilename, outfilename, dest_len);
+               safe_strncat(internal_outfilename, tmp_suffix, dest_len);
        }
 
        if(input_format == FORMAT_RAW) {
@@ -1969,14 +1979,14 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
 
        /* rename temporary file if necessary */
        if(retval == 0 && internal_outfilename != 0) {
-               if(rename(internal_outfilename, outfilename) < 0) {
+               if(flac_rename(internal_outfilename, outfilename) < 0) {
 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
-                       /* on some flavors of windows, rename() will fail if the destination already exists, so we unlink and try again */
-                       if(unlink(outfilename) < 0) {
+                       /* on some flavors of windows, flac_rename() will fail if the destination already exists, so we unlink and try again */
+                       if(flac_unlink(outfilename) < 0) {
                                flac__utils_printf(stderr, 1, "ERROR: moving new FLAC file %s back on top of original FLAC file %s, keeping both\n", internal_outfilename, outfilename);
                                retval = 1;
                        }
-                       else if(rename(internal_outfilename, outfilename) < 0) {
+                       else if(flac_rename(internal_outfilename, outfilename) < 0) {
                                flac__utils_printf(stderr, 1, "ERROR: moving new FLAC file %s back on top of original FLAC file %s, you must do it\n", internal_outfilename, outfilename);
                                retval = 1;
                        }
@@ -1989,7 +1999,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
 
        /* handle --delete-input-file, but don't want to delete if piping from stdin, or if input filename and output filename are the same */
        if(retval == 0 && option_values.delete_input && strcmp(infilename, "-") && internal_outfilename == 0)
-               unlink(infilename);
+               flac_unlink(infilename);
 
        if(internal_outfilename != 0)
                free(internal_outfilename);
@@ -2131,7 +2141,7 @@ int decode_file(const char *infilename)
                if(option_values.preserve_modtime && strcmp(outfilename, "-"))
                        grabbag__file_copy_metadata(infilename, outfilename);
                if(option_values.delete_input && !option_values.test_only && !option_values.analyze)
-                       unlink(infilename);
+                       flac_unlink(infilename);
        }
 
        return retval;
@@ -2173,7 +2183,7 @@ const char *get_outfilename(const char *infilename, const char *suffix)
                static char buffer[4096]; /* @@@ bad MAGIC NUMBER */
 
                if(0 == strcmp(infilename, "-") || option_values.force_to_stdout) {
-                       strcpy(buffer, "-");
+                       safe_strncpy(buffer, "-", sizeof(buffer));
                }
                else {
                        char *p;