summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2014-02-24 16:43:46 +0100
committerDirk Engling <erdgeist@erdgeist.org>2014-02-24 16:43:46 +0100
commit2d13af33551b28c97dcf00a3582e186fd6b0f1ca (patch)
treed43cc58db2d510d0c2edef02750e8483ae5c4b79 /src
parentd317e46bbc8dfa0e926fac507feacad4c647f24d (diff)
Check for errors of IO operarations
Diffstat (limited to 'src')
-rw-r--r--src/export/split_version_2.c34
1 files 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 @@
7 7
8int main( int argc, char **args ) { 8int main( int argc, char **args ) {
9 char table[64], f[1024*1024]; 9 char table[64], f[1024*1024];
10 int outfiles[64], i, off, base = 0; 10 int outfiles[64], i, base = 0;
11 uint32_t fixed_columns = 0; 11 uint32_t fixed_columns = 0;
12 uint32_t *p = (uint32_t*)f;
12 13
13 if( argc > 1 ) base = atol( args[1] ); 14 if( argc > 1 ) base = atol( args[1] );
14 if( argc > 2 ) fixed_columns = atol( args[2] ); 15 if( argc > 2 ) fixed_columns = atol( args[2] );
15 16
17 /* No output file yet valid */
16 for( i=0; i<64; ++i ) outfiles[i] = -1; 18 for( i=0; i<64; ++i ) outfiles[i] = -1;
19
17 while( fgets( table, sizeof(table), stdin ) ) { 20 while( fgets( table, sizeof(table), stdin ) ) {
18 int off = ( table[strlen(table)-1] = 0 ); /* fgets sucks */
19 int f_in = open( table, O_RDONLY );
20 size_t s_in = read( f_in, f, sizeof(f));
21 uint32_t *p = (uint32_t*)f;
22 uint32_t count = p[0], columns = fixed_columns ? fixed_columns : p[1] / 4 - 1;
23 unsigned int file, strnr; 21 unsigned int file, strnr;
22 uint32_t count, columns;
23
24 table[strlen(table)-1] = 0;
25
26 {
27 int f_in = open( table, O_RDONLY );
28 if( f_in < 0 || read( f_in, f, sizeof(f)) <= 0 ) {
29 fprintf( stderr, "Can not read file %s\n", table );
30 exit(1);
31 }
32 close(f_in);
33 }
24 34
25 close(f_in); 35 count = p[0],
36 columns = fixed_columns ? fixed_columns : p[1] / 4 - 1;
26 37
27 for( file=0; file<columns; ++file ) { 38 for( file=0; file<columns; ++file ) {
39 uint32_t off;
40
28 /* Create outfile, if it is not yet there */ 41 /* Create outfile, if it is not yet there */
29 if( outfiles[file] == -1 ) { 42 if( outfiles[file] == -1 ) {
30 sprintf( table, "%02d_unknown", file+base ); 43 sprintf( table, "%02d_unknown", file+base );
31 outfiles[file] = open( table, O_WRONLY | O_APPEND | O_CREAT, 0644 ); 44 outfiles[file] = open( table, O_WRONLY | O_APPEND | O_CREAT, 0644 );
32 if ( outfiles[file] == -1 ) exit(1); 45 if ( outfiles[file] == -1 ) {
46 fprintf( stderr, "Can not create output file %s\n", table );
47 exit(1);
48 }
33 } 49 }
50
34 off = p[file+1]; 51 off = p[file+1];
35 /* Look for end of this chunk, which is <count> strings long */ 52 /* Look for end of this chunk, which is <count> strings long */
36 for( strnr=0; strnr < count; ++strnr ) { while( f[off++] ) {}; f[off-1] = '\n'; } 53 for( strnr=0; strnr < count; ++strnr ) { while( f[off++] ) {}; f[off-1] = '\n'; }
37 write( outfiles[file], f + p[file+1], off - p[file+1] ); 54 write( outfiles[file], f + p[file+1], off - p[file+1] );
38 } 55 }
39 } 56 }
57
40 for( i=0; i<64; ++i ) close( outfiles[i] ); 58 for( i=0; i<64; ++i ) close( outfiles[i] );
41 return 0; 59 return 0;
42} 60}