summary refs log tree commit diff
path: root/src/minutils/s6-hostname.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/minutils/s6-hostname.c')
-rw-r--r--src/minutils/s6-hostname.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/minutils/s6-hostname.c b/src/minutils/s6-hostname.c
new file mode 100644
index 0000000..deb525b
--- /dev/null
+++ b/src/minutils/s6-hostname.c
@@ -0,0 +1,37 @@
+/* ISC license. */
+
+#ifndef _BSD_SOURCE
+#define _BSD_SOURCE
+#endif
+
+#include <unistd.h>
+#include <skalibs/bytestr.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+
+#define USAGE "s6-hostname [ hostname ]"
+
+static int getit (void)
+{
+  stralloc sa = STRALLOC_ZERO ;
+  if (sagethostname(&sa) < 0) strerr_diefu1sys(111, "get hostname") ;
+  sa.s[sa.len++] = '\n' ;
+  if (allwrite(1, sa.s, sa.len) < sa.len)
+    strerr_diefu1sys(111, "write to stdout") ;
+  return 0 ;
+}
+
+static int setit (char const *h)
+{
+  if (sethostname(h, str_len(h)) < 0)
+    strerr_diefu1sys(111, "set hostname") ;
+  return 0 ;
+}
+
+int main (int argc, char const *const *argv)
+{
+  PROG = "s6-hostname" ;
+  return (argc < 2) ? getit() : setit(argv[1]) ;
+}