From 37fc186cbc380f1108cba0bc3c1c60883b0211f2 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Wed, 14 Jun 2017 21:56:37 -0400 Subject: [PATCH] Preventing segment faults from occuring due to missing input files --- CHANGELOG.org | 1 + vm_instructions.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/CHANGELOG.org b/CHANGELOG.org index ffbbfe5..6128419 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -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 diff --git a/vm_instructions.c b/vm_instructions.c index 5b15ba7..9aafae1 100644 --- a/vm_instructions.c +++ b/vm_instructions.c @@ -16,6 +16,7 @@ */ #include "vm.h" +#include 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"); } }