summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..2b2cac1
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,55 @@
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#undef _GNU_SOURCE
+#include <stdlib.h>
+
+#include "utils.h"
+
+int write_int(const char *path, int val)
+{
+ FILE *f;
+
+ f = fopen(path, "w");
+ if (!f) {
+ fprintf(stderr, "failed to open '%s': %m\n", path);
+ return -1;
+ }
+
+ fprintf(f, "%d", val);
+
+ fclose(f);
+
+ return 0;
+}
+
+int read_int(const char *path, int *val)
+{
+ FILE *f;
+
+ f = fopen(path, "r");
+
+ if (!f) {
+ fprintf(stderr, "failed to open '%s': %m\n", path);
+ return -1;
+ }
+
+ fscanf(f, "%d", val);
+
+ fclose(f);
+
+ return 0;
+}
+
+int store_line(const char *line, void *data)
+{
+ FILE *f = data;
+
+ /* ignore comment line */
+ if (line[0] == '#')
+ return 0;
+
+ fprintf(f, "%s", line);
+
+ return 0;
+}