[flac-dev] [patch] patch for fskip_ahead()

nu774 honeycomb77 at gmail.com
Thu Dec 27 04:44:38 PST 2012


Hi,
This patch addresses the following issue:

[Problem]
MinGW build of flac command fails to parse piped WAV input with
WAVEFORMATEXTENSIBLE format.

[Description]
This is because MinGW fseeko() doesn't return error for the attempt to
seek on non seekable file (same behavior as MSVC).
The simplest solution would be to change #ifdef _MSC_VER to #ifdef
_WIN32 here.
Instead, this patch tests file with fstat(), and use fseeko() only when
it is a regular file.
This is confirmed to work properly both on MSVC and MinGW, can seek if
stdin is a redirected regular file, and doesn't require #ifdef.



-------------- next part --------------
diff --git a/src/flac/encode.c b/src/flac/encode.c
index 42fef00..f4b7a4c 100644
--- a/src/flac/encode.c
+++ b/src/flac/encode.c
@@ -26,6 +26,7 @@
 #include <stdio.h> /* for FILE etc. */
 #include <stdlib.h> /* for malloc */
 #include <string.h> /* for strcmp(), strerror() */
+#include <sys/stat.h>
 #include "FLAC/all.h"
 #include "share/alloc.h"
 #include "share/grabbag.h"
@@ -2799,29 +2800,18 @@ FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, const char *fn)
 FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset)
 {
 	static unsigned char dump[8192];
+	struct stat stb;
 
-#ifdef _MSC_VER
-	if(f == stdin) {
-		/* MS' stdio impl can't even seek forward on stdin, have to use pure non-fseek() version: */
-		while(offset > 0) {
-			const long need = (long)min(offset, sizeof(dump));
-			if((long)fread(dump, 1, need, f) < need)
-				return false;
-			offset -= need;
-		}
-	}
-	else
-#endif
+	if(fstat(fileno(f), &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFREG)
 	{
-		while(offset > 0) {
-			long need = (long)min(offset, LONG_MAX);
-			if(fseeko(f, need, SEEK_CUR) < 0) {
-				need = (long)min(offset, sizeof(dump));
-				if((long)fread(dump, 1, need, f) < need)
-					return false;
-			}
-			offset -= need;
-		}
+		if(fseeko(f, offset, SEEK_CUR) == 0)
+			return true;
+	}
+	while(offset > 0) {
+		const long need = (long)min(offset, sizeof(dump));
+		if((long)fread(dump, 1, need, f) < need)
+			return false;
+		offset -= need;
 	}
 	return true;
 }


More information about the flac-dev mailing list