aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorMango0x45 <thomasvoss@live.com>2021-01-18 21:51:42 +0000
committerMango0x45 <thomasvoss@live.com>2021-01-18 21:51:42 +0000
commitf3de92ce323542b98d0142adfdefbf499b32b3a6 (patch)
tree5df835d00d5185f5c9569980c3a329786ba5d842 /utils
parentf472887ca69ca82c31f97dfac8f84a3d841564b0 (diff)
downloadsteve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.tar
steve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.tar.gz
steve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.tar.bz2
steve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.tar.lz
steve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.tar.xz
steve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.tar.zst
steve-bot-f3de92ce323542b98d0142adfdefbf499b32b3a6.zip
Add math program
Diffstat (limited to 'utils')
-rw-r--r--utils/math.c63
-rw-r--r--utils/math.h29
2 files changed, 92 insertions, 0 deletions
diff --git a/utils/math.c b/utils/math.c
new file mode 100644
index 0000000..25ae645
--- /dev/null
+++ b/utils/math.c
@@ -0,0 +1,63 @@
+/*
+ * This is a simple program that calls BC and terminates it if it takes longer
+ * than five seconds to complete. There is no real reason for this to be done
+ * in C instead of Python, however there are some key advantages:
+ *
+ * - Mango gets to play with C making him happy
+ * - C is compiled instead of interpreted so this is technically more efficient
+ * - Ok mayyyybe is slower because it uses a seperate process?
+ * - It makes us look smarter?
+ */
+#define _POSIX_SOURCE
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "math.h"
+
+bool timeout = false, child_done = false;
+
+void timeout_hander(int UNUSED(sig))
+{
+ timeout = true;
+}
+
+void child_handler(int UNUSED(sig))
+{
+ child_done = true;
+}
+
+int main(void)
+{
+ pid_t pid = fork();
+ switch (pid) {
+ case 0:
+ execl("/bin/bc", "", "-q", "bc_funcs/lib.bc", "bc_funcs/init.bc",
+ FILE_NAME, "bc_funcs/exit.bc", NULL);
+ fallthrough;
+ case -1:
+ perror("calc");
+ return EXIT_FAILURE;
+ default:
+ /* Setup signal handlers after fork so the child doesnt inherit them */
+ signal(SIGALRM, timeout_hander);
+ signal(SIGCHLD, child_handler);
+ alarm(TIMEOUT);
+ pause();
+
+ /*
+ * Wait for either the child process to terminate, or for 5 seconds to
+ * have passed
+ */
+ if (timeout) {
+ puts("Error: 5 second timeout reached");
+ kill(pid, 9);
+ }
+
+ break;
+ }
+
+ return EXIT_SUCCESS;
+} \ No newline at end of file
diff --git a/utils/math.h b/utils/math.h
new file mode 100644
index 0000000..edfb9ee
--- /dev/null
+++ b/utils/math.h
@@ -0,0 +1,29 @@
+#ifndef __MATH_H_
+#define __MATH_H_
+
+/* Suppress unused parameter warnings */
+#ifdef __GNUC__
+# define UNUSED(x) UNUSED_##x __attribute__((__unused__))
+#else
+# define UNUSED(x) UNUSED_##x
+#endif
+
+/* Fallthrough pseudo-keyword macro */
+#if __has_attribute(__fallthrough__)
+# define fallthrough __attribute__((__fallthrough__))
+#else
+# define fallthrough \
+ do { \
+ } while (0) /* fallthrough */
+#endif
+
+#define EXIT_FAILURE 1
+#define EXIT_SUCCESS 0
+#define FILE_NAME "bc_input.bc"
+#define TIMEOUT 5
+
+/* Signal handlers */
+void timeout_hander(int sig);
+void child_handler(int sig);
+
+#endif \ No newline at end of file