about summary refs log tree commit diff
path: root/src/minutils/s6-hostname.c
blob: deb525b20aef94110ab1ac7edb91d25b940044f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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]) ;
}