if(!stream_info.test_only) {
if(0 == strcmp(outfilename, "-")) {
- stream_info.fout = stdout;
+ stream_info.fout = file__get_binary_stdout();
}
else {
if(0 == (stream_info.fout = fopen(outfilename, "wb"))) {
#ifdef FLAC__HAS_OGG
if(stream_info.is_ogg) {
if (0 == strcmp(infilename, "-")) {
- stream_info.fin = stdin;
+ stream_info.fin = file__get_binary_stdin();
} else {
if (0 == (stream_info.fin = fopen(infilename, "rb"))) {
fprintf(stderr, "%s: ERROR: can't open input file %s\n", stream_info.inbasefilename, infilename);
- if(stream_info.fout != stdout)
+ if(0 != stream_info.fout && stream_info.fout != stdout)
fclose(stream_info.fout);
return 1;
}
if(!stream_info.test_only) {
if(0 == strcmp(outfilename, "-")) {
- stream_info.fout = stdout;
+ stream_info.fout = file__get_binary_stdout();
}
else {
if(0 == (stream_info.fout = fopen(outfilename, "wb"))) {
#ifdef FLAC__HAS_OGG
if(stream_info.is_ogg) {
if (0 == strcmp(infilename, "-")) {
- stream_info.fin = stdin;
+ stream_info.fin = file__get_binary_stdin();
} else {
if (0 == (stream_info.fin = fopen(infilename, "rb"))) {
fprintf(stderr, "%s: ERROR: can't open input file %s\n", stream_info.inbasefilename, infilename);
- if(stream_info.fout != stdout)
+ if(0 != stream_info.fout && stream_info.fout != stdout)
fclose(stream_info.fout);
return 1;
}
(void)lookahead_length;
if(0 == strcmp(outfilename, "-")) {
- encoder_wrapper.fout = stdout;
+ encoder_wrapper.fout = file__get_binary_stdout();
}
else {
if(0 == (encoder_wrapper.fout = fopen(outfilename, "wb"))) {
fprintf(stderr, "%s: ERROR: can't open output file %s\n", encoder_wrapper.inbasefilename, outfilename);
- fclose(infile);
+ if(infile != stdin)
+ fclose(infile);
return 1;
}
}
fprintf(stderr, "%s: WARNING: skipping extra 'data' sub-chunk\n", encoder_wrapper.inbasefilename);
}
else if(!got_fmt_chunk) {
- fprintf(stderr, "%s: ERROR: got data sub-chunk before fmt sub-chunk\n", encoder_wrapper.inbasefilename);
+ fprintf(stderr, "%s: ERROR: got 'data' sub-chunk before 'fmt' sub-chunk\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
else {
goto wav_abort_;
}
else if(bytes_read != (*options.align_reservoir_samples) * bytes_per_wide_sample) {
- fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written);
+ fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written);
data_bytes = 0;
}
else {
#endif
if(0 == strcmp(outfilename, "-")) {
- encoder_wrapper.fout = stdout;
+ encoder_wrapper.fout = file__get_binary_stdout();
}
else {
if(0 == (encoder_wrapper.fout = fopen(outfilename, "wb"))) {
fprintf(stderr, "ERROR: can't open output file %s\n", outfilename);
- fclose(infile);
+ if(infile != stdin)
+ fclose(infile);
return 1;
}
}
{
encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
FLAC__byte b;
- FILE *f;
+ FILE *f = encoder_wrapper->fout;
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
const unsigned max_framesize = metadata->data.stream_info.max_framesize;
(void)encoder; /* silence compiler warning about unused parameter */
- if(encoder_wrapper->fout != stdout) {
+ if(f != stdout) {
fclose(encoder_wrapper->fout);
if(0 == (f = fopen(encoder_wrapper->outfilename, "r+b")))
return;
}
- else
- f = stdout;
/* all this is based on intimate knowledge of the stream header
* layout, but a change to the header format that would break this
#if defined _MSC_VER || defined __MINGW32__
#include <sys/utime.h> /* for utime() */
-#include <io.h> /* for chmod() */
+#include <io.h> /* for chmod(), _setmode() */
+#include <fcntl.h> /* for _O_BINARY */
#else
#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
#include <utime.h> /* for utime() */
#include <unistd.h> /* for chown() */
#endif
-#include <sys/stat.h> /* for stat() */
+#ifdef __CYGWIN__
+#include <io.h> /* for _setmode(), O_BINARY */
+#endif
+#include <sys/stat.h> /* for stat(), maybe chmod() */
#include <string.h> /* for strrchr() */
#include "file.h"
}
return ++p;
}
+
+FILE *file__get_binary_stdin()
+{
+ /* if something breaks here it is probably due to the presence or
+ * absence of an underscore before the identifiers 'setmode',
+ * 'fileno', and/or 'O_BINARY'; check your system header files.
+ */
+#if defined _MSC_VER || defined __MINGW32__
+ _setmode(_fileno(stdin), _O_BINARY);
+#elif defined __CYGWIN__
+ /* almost certainly not needed for any modern Cygwin, but let's be safe... */
+ setmode(_fileno(stdin), _O_BINARY);
+#endif
+
+ return stdin;
+}
+
+FILE *file__get_binary_stdout()
+{
+ /* if something breaks here it is probably due to the presence or
+ * absence of an underscore before the identifiers 'setmode',
+ * 'fileno', and/or 'O_BINARY'; check your system header files.
+ */
+#if defined _MSC_VER || defined __MINGW32__
+ _setmode(_fileno(stdout), _O_BINARY);
+#elif defined __CYGWIN__
+ /* almost certainly not needed for any modern Cygwin, but let's be safe... */
+ setmode(_fileno(stdout), _O_BINARY);
+#endif
+
+ return stdout;
+}
#define flac__file_h
#include <sys/types.h> /* for off_t */
+#include <stdio.h> /* for FILE */
void flac__file_copy_metadata(const char *srcpath, const char *destpath);
off_t flac__file_get_filesize(const char *srcpath);
const char *flac__file_get_basename(const char *srcpath);
+/* these will forcibly set stdin/stdout to binary mode (for OSes that require it) */
+FILE *file__get_binary_stdin();
+FILE *file__get_binary_stdout();
+
#endif
if(0 == strcmp(infilename, "-")) {
infilesize = -1;
- encode_infile = stdin;
+ encode_infile = file__get_binary_stdin();
}
else {
infilesize = flac__file_get_filesize(infilename);
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for strcmp() */
#include <sys/stat.h> /* for stat() */
+#if defined _MSC_VER || defined __MINGW32__
+#include <io.h> /* for _setmode() */
+#include <fcntl.h> /* for _O_BINARY */
+#elif defined __CYGWIN__
+#include <io.h> /* for _setmode(), O_BINARY */
+#endif
#include "FLAC/assert.h"
#include "protected/file_decoder.h"
#include "protected/seekable_stream_decoder.h"
*
***********************************************************************/
+static FILE *get_binary_stdin_();
static FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
decoder->private_->seekable_stream_decoder = 0;
if(0 == decoder->private_->filename)
- decoder->private_->file = stdin;
+ decoder->private_->file = get_binary_stdin_();
else
decoder->private_->file = fopen(decoder->private_->filename, "rb");
*
***********************************************************************/
+/*
+ * This will forcibly set stdin to binary mode (for OSes that require it)
+ */
+FILE *get_binary_stdin_()
+{
+ /* if something breaks here it is probably due to the presence or
+ * absence of an underscore before the identifiers 'setmode',
+ * 'fileno', and/or 'O_BINARY'; check your system header files.
+ */
+#if defined _MSC_VER || defined __MINGW32__
+ _setmode(_fileno(stdin), _O_BINARY);
+#elif defined __CYGWIN__
+ /* almost certainly not needed for any modern Cygwin, but let's be safe... */
+ setmode(_fileno(stdin), _O_BINARY);
+#endif
+
+ return stdin;
+}
+
FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
{
FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;