summaryrefslogtreecommitdiffstats
path: root/usr/kinit/getintfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/kinit/getintfile.c')
-rw-r--r--usr/kinit/getintfile.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/usr/kinit/getintfile.c b/usr/kinit/getintfile.c
new file mode 100644
index 0000000..41ba475
--- /dev/null
+++ b/usr/kinit/getintfile.c
@@ -0,0 +1,30 @@
+/*
+ * Open a file and read it, assuming it contains a single long value.
+ * Return 0 if we read a valid value, otherwise -1.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "kinit.h"
+
+int getintfile(const char *path, long *val)
+{
+ char buffer[64];
+ char *ep;
+ FILE *f;
+
+ f = fopen(path, "r");
+ if (!f)
+ return -1;
+
+ ep = buffer + fread(buffer, 1, sizeof buffer - 1, f);
+ fclose(f);
+ *ep = '\0';
+
+ *val = strtol(buffer, &ep, 0);
+ if (*ep && *ep != '\n')
+ return -1;
+ else
+ return 0;
+}