aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs6rc/s6rc_read_major.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs6rc/s6rc_read_major.c')
-rw-r--r--src/libs6rc/s6rc_read_major.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libs6rc/s6rc_read_major.c b/src/libs6rc/s6rc_read_major.c
new file mode 100644
index 0000000..978f29c
--- /dev/null
+++ b/src/libs6rc/s6rc_read_major.c
@@ -0,0 +1,30 @@
+/* ISC license. */
+
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include <skalibs/uint32.h>
+#include <skalibs/djbunix.h>
+
+#include <s6-rc/s6rc-utils.h>
+
+#include <skalibs/posixishard.h>
+
+uint32_t s6rc_read_major (char const *compiled)
+{
+ size_t clen = strlen(compiled) ;
+ ssize_t r ;
+ uint32_t res ;
+ char tmp[UINT32_FMT] ;
+ char fn[clen + 7] ;
+ memcpy(fn, compiled, clen) ;
+ memcpy(fn + clen, "/major", 7) ;
+ r = openreadnclose(fn, tmp, UINT32_FMT) ;
+ if (r == -1) return errno == ENOENT ? 6 : 0 ; /* up to 0.6.x.y doesn't have db/major */
+ if (!r) return 0 ;
+ if (tmp[r] != '\n') return (errno = EPROTO, 0) ;
+ tmp[r] = 0 ;
+ if (uint320_scan(tmp, &res) != r) return (errno = EPROTO, 0) ;
+ return res ;
+}