msgdump tool
[vg.git] / src / msgdump.c
1 // Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved
2
3 #define VG_TOOLS
4 #include "vg/vg.h"
5 #include "vg/vg_msg.h"
6
7 #define CHUNK_SIZE 4096
8
9 int main( int argc, char *argv[] ){
10 assert(argc);
11
12 if( argc < 2 ){
13 vg_error( "Usage: %s <file>\n", argv[0] );
14 return 0;
15 }
16
17 FILE *f = fopen( argv[1], "rb" );
18 if( !f ){
19 vg_error( "vg_disk_open_read: %s\n", strerror(errno) );
20 }
21
22 void *buffer = NULL;
23 u64 current = 0;
24
25 /* read in chunks */
26 for( u32 i=0; 1; i++ ){
27 buffer = realloc( buffer, current + CHUNK_SIZE );
28 u64 l = fread( buffer + current, 1, CHUNK_SIZE, f );
29 current += l;
30
31 if( l != CHUNK_SIZE ){
32 if( feof( f ) ){
33 break;
34 }
35 else{
36 if( ferror( f ) ){
37 fclose(f);
38 vg_fatal_error( "read error" );
39 }
40 else{
41 fclose(f);
42 vg_fatal_error( "unknown error codition" );
43 }
44 }
45 }
46 }
47
48 fclose( f );
49
50 vg_msg msg;
51 vg_msg_init( &msg, buffer, current );
52 vg_msg_print( &msg, current );
53
54 free( buffer );
55 }