summaryrefslogtreecommitdiff
path: root/src/utilities/s6-dns-hosts-compile.c
blob: 554a56915e72b09b728a07ed879f6525e1a957ca (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ISC license. */

#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>

#include <skalibs/sgetopt.h>
#include <skalibs/strerr.h>
#include <skalibs/djbunix.h>

#include <s6-dns/hosts.h>

#define USAGE "s6-dns-hosts-compile [ -i ifile ] [ -o ofile ]"
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char const *const *argv)
{
  char const *ifile = "/etc/hosts" ;
  char const *ofile = "/etc/hosts.cdb" ;
  PROG = "s6-dns-hosts-compile" ;
  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "i:o:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'i' : ifile = l.arg ; break ;
        case 'o' : ofile = l.arg ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  {
    int fdr, fdw ;
    size_t len = strlen(ofile) ;
    char tmp[len + 29] ;
    memcpy(tmp, ofile, len) ;
    memcpy(tmp + len, ":s6-dns-hosts-compile:XXXXXX", 29) ;
    fdr = openc_read(ifile) ;
    if (fdr == -1) strerr_diefu2sys(111, "open ", ifile) ;
    fdw = mkstemp(tmp) ;
    if (fdw == -1) strerr_diefu2sys(111, "create ", tmp) ;
    if (!s6dns_hosts_compile(fdr, fdw))
      strerr_diefu4sys(111, "compile ", ifile, " to ", tmp) ;
    if (fsync(fdw) == -1) strerr_diefu2sys(111, "fsync ", tmp) ;
    if (fchmod(fdw, 0644) == -1) strerr_diefu2sys(111, "fchmod ", tmp) ;
    if (rename(tmp, ofile) == -1) strerr_diefu4sys(111, "rename ", tmp, " to ", ofile) ;
  }
  return 0 ;
}