summaryrefslogtreecommitdiff
path: root/testing/nTox.c
diff options
context:
space:
mode:
authorcharmlesscoin <charmlesscoin@gmail.com>2013-08-03 17:57:44 -0400
committercharmlesscoin <charmlesscoin@gmail.com>2013-08-03 17:57:44 -0400
commit43f66085304190651b60a6f09c3b9dab8279e607 (patch)
tree4a1e35f33cc0997f123dd61123a4a4672c881a12 /testing/nTox.c
parent09a6d2d351840edbe8defd7c209c312c226bedef (diff)
cleaned up load_key() and added more error checking
Diffstat (limited to 'testing/nTox.c')
-rw-r--r--testing/nTox.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/testing/nTox.c b/testing/nTox.c
index 15e209a9..3aea6b32 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -361,16 +361,18 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
361void load_key(char *path) 361void load_key(char *path)
362{ 362{
363 FILE *data_file = fopen(path, "r"); 363 FILE *data_file = fopen(path, "r");
364 int size = 0;
364 365
365 if (data_file) { 366 if (data_file) {
366 //load keys 367 //load keys
367 fseek(data_file, 0, SEEK_END); 368 fseek(data_file, 0, SEEK_END);
368 int size = ftell(data_file); 369 size = ftell(data_file);
369 fseek(data_file, 0, SEEK_SET); 370 rewind(data_file);
371
370 uint8_t data[size]; 372 uint8_t data[size];
371 if (fread(data, sizeof(uint8_t), size, data_file) != size){ 373 if (fread(data, sizeof(uint8_t), size, data_file) != size){
372 printf("[i] could not read data file\n[i] exiting\n"); 374 fputs("[!] could not read data file! exiting...\n", stderr);
373 exit(1); 375 goto FILE_ERROR;
374 } 376 }
375 Messenger_load(data, size); 377 Messenger_load(data, size);
376 378
@@ -387,11 +389,15 @@ void load_key(char *path)
387 } 389 }
388 390
389 if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ 391 if (fwrite(data, sizeof(uint8_t), size, data_file) != size){
390 puts("[i] could not write data file! exiting..."); 392 fputs("[!] could not write data file! exiting...", stderr);
391 exit(1); 393 goto FILE_ERROR;
392 } 394 }
393 } 395 }
394 fclose(data_file); 396
397FILE_ERROR:
398 if(fclose(data_file) < 0)
399 perror("[!] fclose failed");
400 exit(1);
395} 401}
396 402
397void print_help(void) 403void print_help(void)