From 2d13af33551b28c97dcf00a3582e186fd6b0f1ca Mon Sep 17 00:00:00 2001
From: Dirk Engling <erdgeist@erdgeist.org>
Date: Mon, 24 Feb 2014 16:43:46 +0100
Subject: Check for errors of IO operarations

---
 src/export/split_version_2.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/export/split_version_2.c b/src/export/split_version_2.c
index 2bc48c7..ab6c8cf 100644
--- a/src/export/split_version_2.c
+++ b/src/export/split_version_2.c
@@ -7,36 +7,54 @@
 
 int main( int argc, char **args ) {
   char table[64], f[1024*1024];
-  int  outfiles[64], i, off, base = 0;
+  int  outfiles[64], i, base = 0;
   uint32_t fixed_columns = 0;
+  uint32_t *p = (uint32_t*)f;
 
   if( argc > 1 ) base = atol( args[1] );
   if( argc > 2 ) fixed_columns = atol( args[2] );
 
+  /* No output file yet valid */
   for( i=0; i<64; ++i ) outfiles[i] = -1;
+
   while( fgets( table, sizeof(table), stdin ) ) {
-    int off = ( table[strlen(table)-1] = 0 ); /* fgets sucks */
-    int f_in = open( table, O_RDONLY );
-    size_t s_in = read( f_in, f, sizeof(f));
-    uint32_t *p = (uint32_t*)f;
-    uint32_t count = p[0], columns = fixed_columns ? fixed_columns : p[1] / 4 - 1;
     unsigned int file, strnr;
+    uint32_t count, columns;
+
+    table[strlen(table)-1] = 0;
+
+    {
+      int f_in = open( table, O_RDONLY );
+      if( f_in < 0  || read( f_in, f, sizeof(f)) <= 0 ) {
+        fprintf( stderr, "Can not read file %s\n", table );
+        exit(1);
+      }
+      close(f_in);
+    }
 
-    close(f_in);
+    count = p[0],
+    columns = fixed_columns ? fixed_columns : p[1] / 4 - 1;
 
     for( file=0; file<columns; ++file ) {
+      uint32_t off;
+
       /* Create outfile, if it is not yet there */
       if( outfiles[file] == -1 ) {
         sprintf( table, "%02d_unknown", file+base );
         outfiles[file] = open( table, O_WRONLY | O_APPEND | O_CREAT, 0644 );
-        if ( outfiles[file] == -1 ) exit(1);
+        if ( outfiles[file] == -1 ) {
+          fprintf( stderr, "Can not create output file %s\n", table );
+          exit(1);
+        }
       }
+
       off = p[file+1];
       /* Look for end of this chunk, which is <count> strings long */
       for( strnr=0; strnr < count; ++strnr ) { while( f[off++] ) {}; f[off-1] = '\n'; }
       write( outfiles[file], f + p[file+1], off - p[file+1] );
     }
   }
+
   for( i=0; i<64; ++i ) close( outfiles[i] );
   return 0;
 }
-- 
cgit v1.2.3