--- pzip-0.83/main.c.bak 2008-04-06 00:20:43.323480897 +0300 +++ pzip-0.83/main.c 2008-04-06 00:31:48.041480029 +0300 @@ -7,6 +7,9 @@ #include #include +#include +#include + #include "pzip.h" #include "config.h" #include "version.h" @@ -82,6 +85,7 @@ int main( int argc, char* argv[] ) { int encode_len; int input_len; bool encode_only = FALSE; + bool fsync_output = FALSE; bool encoding= TRUE; FILE* in_fp = NULL; FILE* out_fp = NULL; @@ -114,6 +118,10 @@ int main( int argc, char* argv[] ) { encode_only = TRUE; break; + case 'f': + fsync_output = TRUE; + break; + case 'v': ++verbose; break; @@ -226,11 +234,41 @@ int main( int argc, char* argv[] ) { } if (out_fp) { - if (encoding) fwrite( encode_buf, 1, encode_len, out_fp ); - else fwrite( decode_buf, 1, input_len, out_fp ); + size_t fret; + + if (encoding) { + fret = fwrite( encode_buf, 1, encode_len, out_fp ); + if (fret != encode_len) { + fprintf(stderr, + "***** failed to write output file: %s\n", strerror(errno)); + exit( 1 ); + } + } else { + fret = fwrite( decode_buf, 1, input_len, out_fp ); + if (fret != input_len) { + fprintf(stderr, + "***** failed to write output file: %s\n", strerror(errno)); + exit( 1 ); + } + } - fclose( out_fp ); - out_fp = NULL; + if (fflush( out_fp ) == EOF) { + fprintf(stderr, + "***** failed to write output file: %s\n", strerror(errno)); + fclose( out_fp ); + exit( 1 ); + } + if (fsync_output && (fsync( fileno( out_fp ) ) == -1 && (errno != EINVAL))) { + fprintf(stderr, + "***** failed to fsync output file: %s\n", strerror(errno)); + fclose( out_fp ); + exit( 1 ); + } + if (fclose( out_fp ) == EOF) { + fprintf(stderr, + "***** failed to write output file: %s\n", strerror(errno)); + exit( 1 ); + } } exit( 0 );