valgrinds rule depends on all
[brainfuck.git] / bf.c
diff --git a/bf.c b/bf.c
index 41882a8cd92a08995a34e018fb7a940d72aea832..6a9e0506fb7d56ba10ac643c1995a095e2445ed3 100644 (file)
--- a/bf.c
+++ b/bf.c
@@ -2,7 +2,7 @@
 /* cflags: */
 /* linker: debug.o */
 
-#include <assert.h>
+#include <errno.h>
 #include <getopt.h>
 #include <malloc.h>
 #include <stdio.h>
@@ -248,6 +248,7 @@ int main (int argc, char *argv[])
         fid = fopen (output, "w");
         if (fid == NULL) {
             VERBOSE (ERROR, fprintf (stderr, "%s: can't open file '%s' for writing\n", progname, output));
+            if (buffer) free (buffer);
             return 1;
         }
     } else {
@@ -262,6 +263,11 @@ int main (int argc, char *argv[])
         fclose (fid);
     }
 
+    /* free buffer */
+    if (buffer) {
+        free (buffer);
+    }
+
     VERBOSE (INFO, fprintf (stdout, "\nmemory:"); int _i; for (_i = 0; _i < MEMSIZE; _i++) fprintf (stdout," %d", mem[_i]); fprintf (stdout,"\n"));
 
     return rc;
@@ -292,7 +298,7 @@ int main (int argc, char *argv[])
 // test: bf.exe -e '+++' -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
 // test: bf.exe -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
 // test: bf.exe -m "51 50 49" -e '.>.>.' |grep -q "321"
-// test: bf.exe -m "51 50 49" -e '.>.>.' -o test.log | grep -q "321" test.log; rc=$?; rm test.log; test $rc -eq 0
+// test: bf.exe -m "51 50 49" -e '.>.>.' -o test.log && grep -q "321" test.log; rc=$?; rm test.log; test $rc -eq 0
 // test: echo '+++>++>>-<--' > test.b | bf.exe -i test.b | grep -q "memory: 3 2 -2 -1 0"; rc=$?; rm test.b; test $rc -eq 0
 
 // test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe -v1 | grep -q "Hello World!"