about summary refs log tree commit diff
path: root/src/minutils/s6-hostname.c
blob: 9d71cbe1dedd53f060a57560b4d86d587edea967 (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 <string.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, strlen(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]) ;
}