about summary refs log tree commit diff
path: root/collect.c
diff options
context:
space:
mode:
Diffstat (limited to 'collect.c')
-rw-r--r--collect.c44
1 files changed, 25 insertions, 19 deletions
diff --git a/collect.c b/collect.c
index b380f71..cf32283 100644
--- a/collect.c
+++ b/collect.c
@@ -1,6 +1,5 @@
 /*
  * Live variable collector
- * Copyright (C) 2021, 2023  Gregory James Duck
  * Copyright (C) 2024-2025  Nguyễn Gia Phong
  *
  * This file is part of taosc.
@@ -21,32 +20,39 @@
 
 #include "stdlib.c"
 
-FILE *fout = NULL;
+static uint64_t stack_size;
+static int output_file = -1;
+
+/*
+ * Get an environment variable and parse as a number.
+ * Return 0 on error.
+ */
+uint64_t getenvull(const char *name)
+{
+	const char *const s = getenv(name);
+	if (s == NULL)
+		return 0ULL;
+	errno = 0;
+	const uint64_t u = strtoull(s, NULL, 0);
+	if (errno)
+		return 0ULL;
+	return u;
+}
 
 void init(int argc, const char *const *argv, char **envp)
 {
 	environ = envp;
+	stack_size = getenvull("TAOSC_STACK_SIZE");
 	const char *const path = getenv("TAOSC_OUTPUT");
 	if (path != NULL)
-		fout = fopen(path, "w");
-	if (fout == NULL)
-		fout = stderr;
-	setvbuf(fout, NULL, _IOFBF, 0);
+		output_file = open(path, O_WRONLY | O_CREAT, 0644);
+	if (output_file == -1)
+		output_file = 2; /* stderr */
 }
 
-
 void log(const struct STATE *state)
 {
-	static mutex_t mutex = MUTEX_INITIALIZER;
-	if (mutex_lock(&mutex) < 0)
-		return;
-	clearerr(fout);
-	fprintf(fout, "[begin]\n");
-	const int64_t *const env = (const int64_t *) state;
-	for (unsigned char i = 1; i < sizeof(*state) / sizeof(*env); ++i)
-		fprintf(fout, "%hhu %lld\n", i, env[i]);
-	fprintf(fout, "[end]\n");
-	fflush(fout);
-	fclose(fout); /* FIXME: reopen */
-	mutex_unlock(&mutex);
+	write(output_file, (const char *)state, sizeof(struct STATE));
+	write(output_file, (const char *)state->rsp, stack_size);
+	fsync(output_file);
 }