summaryrefslogtreecommitdiff
path: root/src/skaembutils/s6-uniquename.c
blob: 06418e55d27b8f85c7556ea04b2d62536d92682f (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
/* ISC license. */

#include <skalibs/allreadwrite.h>
#include <skalibs/sgetopt.h>
#include <skalibs/uint.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/skamisc.h>
#include <skalibs/random.h>

#define USAGE "s6-uniquename [ -n randomlen ] prefix"
#define usage() strerr_dieusage(100, USAGE)

int main (int argc, char const *const *argv)
{
  stralloc sa = STRALLOC_ZERO ;
  unsigned int n = 8 ;
  PROG = "s6-uniquename" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "n:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'n' : if (!uint0_scan(l.arg, &n)) usage() ; break ;
        default : usage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc < 1) usage() ;
  if (!stralloc_cats(&sa, argv[0])) strerr_diefu1sys(111, "stralloc_cats") ;
  if ((n ? random_sauniquename(&sa, n) : sauniquename(&sa)) < 0)
    strerr_diefu1sys(111, "make unique name") ;
  if (!stralloc_catb(&sa, "\n", 1)) strerr_diefu1sys(111, "stralloc_cats") ;
  if (allwrite(1, sa.s, sa.len) < sa.len) strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}