Index: /libmpc/trunk/mpcchap/mpcchap.c
===================================================================
--- /libmpc/trunk/mpcchap/mpcchap.c	(revision 486)
+++ /libmpc/trunk/mpcchap/mpcchap.c	(revision 487)
@@ -23,4 +23,5 @@
 #include "iniparser.h"
 
+#include <inttypes.h>
 #include <sys/stat.h>
 
@@ -83,4 +84,5 @@
 	char * tmp_buff;
 	dictionary * dict;
+    mpc_status ret = MPC_STATUS_OK;
 
 	chap_pos = (demux->chap_pos >> 3) + si->header_position;
@@ -90,7 +92,11 @@
 	stat(mpc_file, &stbuf);
 	tmp_buff = malloc(stbuf.st_size - chap_pos - chap_size);
+
 	in_file = fopen( mpc_file, "r+b" );
 	fseek(in_file, chap_pos + chap_size, SEEK_SET);
-	fread(tmp_buff, 1, stbuf.st_size - chap_pos - chap_size, in_file);
+    if (1 != fread(tmp_buff, stbuf.st_size - chap_pos - chap_size, 1, in_file)) {
+        ret = MPC_STATUS_FAIL;
+        goto error_read_in_file;
+    }
 	fseek(in_file, chap_pos, SEEK_SET);
 
@@ -105,6 +111,6 @@
 
 		if (chap_pos > si->samples - si->beg_silence)
-			fprintf(stderr, "warning : chapter %i starts @ %lli samples after the end of the stream (%lli)\n",
-			        i + 1, chap_pos, si->samples - si->beg_silence);
+            fprintf(stderr, "warning : chapter %i starts @ %" PRId64 " samples after the end of the stream (%" PRId64 ")\n",
+                    i + 1, chap_pos, si->samples - si->beg_silence);
 
 		Init_Tags();
@@ -142,11 +148,13 @@
 
 	fwrite(tmp_buff, 1, stbuf.st_size - chap_pos - chap_size, in_file);
-	ftruncate(fileno(in_file), ftell(in_file));
-
-	fclose(in_file);
-	free(tmp_buff);
-	iniparser_freedict(dict);
-
-	return MPC_STATUS_OK;
+    if (0 != ftruncate(fileno(in_file), ftell(in_file)))
+        ret = MPC_STATUS_FAIL;
+
+    iniparser_freedict(dict);
+error_read_in_file:
+    fclose(in_file);
+    free(tmp_buff);
+
+    return ret;
 }
 
@@ -159,4 +167,5 @@
 	int chap_pos, end_pos, chap_size, i;
 	char * tmp_buff;
+    mpc_status ret = MPC_STATUS_OK;
 
 	if (0 == (cd = cf_parse(chap_file, &format))) {
@@ -173,5 +182,8 @@
 	in_file = fopen( mpc_file, "r+b" );
 	fseek(in_file, chap_pos + chap_size, SEEK_SET);
-	fread(tmp_buff, 1, stbuf.st_size - chap_pos - chap_size, in_file);
+    if (1 != fread(tmp_buff, stbuf.st_size - chap_pos - chap_size, 1, in_file)) {
+        ret = MPC_STATUS_FAIL;
+        goto error_read_in_file;
+    }
 	fseek(in_file, chap_pos, SEEK_SET);
 
@@ -191,5 +203,5 @@
 
 		if (chap_pos > si->samples - si->beg_silence)
-			fprintf(stderr, "warning : chapter %i starts @ %lli samples after the end of the stream (%lli)\n",
+            fprintf(stderr, "warning : chapter %i starts @ %" PRId64 " samples after the end of the stream (%" PRId64 ")\n",
 			        i, chap_pos, si->samples - si->beg_silence);
 
@@ -225,10 +237,12 @@
 
 	fwrite(tmp_buff, 1, stbuf.st_size - chap_pos - chap_size, in_file);
-	ftruncate(fileno(in_file), ftell(in_file));
-
+    if (0 != ftruncate(fileno(in_file), ftell(in_file)))
+        ret = MPC_STATUS_FAIL;
+
+error_read_in_file:
 	fclose(in_file);
 	free(tmp_buff);
 
-	return MPC_STATUS_OK;
+    return ret;
 }
 
@@ -248,5 +262,5 @@
 	for (i = 0; i < chap_nb; i++) {
 		chap = mpc_demux_chap(demux, i);
-		fprintf(out_file, "[%lli]\ngain=%i\npeak=%i\n", chap->sample, chap->gain, chap->peak);
+        fprintf(out_file, "[%" PRId64 "]\ngain=%i\npeak=%i\n", chap->sample, chap->gain, chap->peak);
 		if (chap->tag_size > 0) {
 			int item_count, j;
Index: /libmpc/trunk/mpcenc/mpcenc.c
===================================================================
--- /libmpc/trunk/mpcenc/mpcenc.c	(revision 486)
+++ /libmpc/trunk/mpcenc/mpcenc.c	(revision 487)
@@ -1024,5 +1024,5 @@
         else if ( 0 == strcmp ( arg, "stderr") ) {                                      // Offset for threshold in quiet
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            freopen ( argv[k], "a", stderr );
+            stderr = freopen ( argv[k], "a", stderr );
         }
         else if ( 0 == strcmp ( arg, "ltq")  ||  0 == strcmp ( arg, "ath") ) {          // threshold in quiet
@@ -1209,9 +1209,10 @@
             if ( p == NULL ) {
                 stderr_printf (" Enter value for tag key '%s': ", argv[k] );
-                fgets ( buff, sizeof buff, stdin );
-                len = strlen (buff);
-                while ( len > 0  &&  (buff [len-1] == '\r'  ||  buff [len-1] == '\n') )
-                    len--;
-                addtag ( arg, strlen(arg), buff, len, NoUnicode*6, 0 );
+                if (0 != fgets ( buff, sizeof buff, stdin )) {
+                    len = strlen (buff);
+                    while ( len > 0  &&  (buff [len-1] == '\r'  ||  buff [len-1] == '\n') )
+                        len--;
+                    addtag ( arg, strlen(arg), buff, len, NoUnicode*6, 0 );
+                }
             }
             else {
Index: /libmpc/trunk/mpcgain/mpcgain.c
===================================================================
--- /libmpc/trunk/mpcgain/mpcgain.c	(revision 486)
+++ /libmpc/trunk/mpcgain/mpcgain.c	(revision 487)
@@ -116,5 +116,5 @@
 	while (1) {
 		fseek(file, next_chap_pos, SEEK_SET);
-		fread(buffer, 1, MAX_HEAD_SIZE, file);
+        if (0 == fread(buffer, 1, MAX_HEAD_SIZE, file)) break;
 		r.buff = buffer;
 		r.count = 8;
@@ -183,5 +183,5 @@
 		mpc_demux_seek_sample(demux, 0);
 		if (chap_nb > 0) {
-			mpc_chap_info * chap_info = mpc_demux_chap(demux, chap);
+            const mpc_chap_info * chap_info = mpc_demux_chap(demux, chap);
 			next_chap_sample = chap_info->sample;
 			chap_gain = malloc(sizeof(mpc_uint16_t) * 2 * chap_nb);
@@ -216,5 +216,5 @@
 				cur_sample = next_chap_sample;
 				if (chap < chap_nb) {
-					mpc_chap_info * chap_info = mpc_demux_chap(demux, chap);
+                    const mpc_chap_info * chap_info = mpc_demux_chap(demux, chap);
 					next_chap_sample = chap_info->sample;
 				} else
@@ -261,6 +261,6 @@
 		}
 		fseek(file, header_pos[j] - 4, SEEK_SET);
-		fread(buffer, 1, 16, file);
-		if (memcmp(buffer, "MPCK", 4) != 0) {
+
+        if (fread(buffer, 1, 16, file) != 16 || memcmp(buffer, "MPCK", 4) != 0) {
 			fprintf(stderr, "Unsupported file format, not a sv8 file : %s\n", argv[j + 1]);
 			fclose(file);
@@ -277,5 +277,5 @@
 			header_pos[j] += b.size + size;
 			fseek(file, header_pos[j], SEEK_SET);
-			fread(buffer, 1, 16, file);
+            if (0 == fread(buffer, 1, 16, file)) break;
 			r.buff = buffer;
 			r.count = 8;
@@ -287,5 +287,5 @@
 			continue;
 		}
-		header_pos[j] += size;
+        header_pos[j] += size;
 
 		buffer[size] = 1; // replaygain version
