Preventing segment faults from occuring due to missing input files

This commit is contained in:
Jeremiah Orians 2017-06-14 21:56:37 -04:00
parent 2a12799d82
commit 37fc186cbc
No known key found for this signature in database
GPG Key ID: 7457821534D2ACCD
2 changed files with 14 additions and 0 deletions

View File

@ -17,6 +17,7 @@
* Current
** Added
Incorporated High level prototypes into makefile
Added logic to catch non-existent input files and report a useful error message
** Changed
Extended VPATH in makefile to shorten dependency names

View File

@ -16,6 +16,7 @@
*/
#include "vm.h"
#include <sys/stat.h>
FILE* tape_01;
FILE* tape_02;
@ -161,13 +162,25 @@ uint32_t shift_register(uint32_t source, uint32_t amount, bool left, bool zero)
void vm_FOPEN_READ(struct lilith* vm)
{
struct stat sb;
if(0x00001100 == vm->reg[0])
{
if(-1 == stat(tape_01_name, &sb))
{
fprintf(stderr, "File named %s does not exist\n", tape_01_name);
exit(EXIT_FAILURE);
}
tape_01 = fopen(tape_01_name, "r");
}
if (0x00001101 == vm->reg[0])
{
if(-1 == stat(tape_02_name, &sb))
{
fprintf(stderr, "File named %s does not exist\n", tape_02_name);
exit(EXIT_FAILURE);
}
tape_02 = fopen(tape_02_name, "r");
}
}