summaryrefslogtreecommitdiff
path: root/src/librandom/random_sauniquename.c
blob: 6d200aad11625013fc5032de21b137d59a6f03b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* ISC license. */

/* MT-unsafe */

#include <skalibs/stralloc.h>
#include <skalibs/skamisc.h>
#include <skalibs/random.h>

int random_sauniquename (stralloc *sa, unsigned int n)
{
  unsigned int base = sa->len ;
  int wasnull = !sa->s ;
  if (!sauniquename(sa)) return 0 ;
  if (!stralloc_readyplus(sa, n+1)) goto err ;
  stralloc_catb(sa, ":", 1) ;
  random_name(sa->s + sa->len, n) ;
  sa->len += n ;
  return 1 ;

err:
  if (wasnull) stralloc_free(sa) ; else sa->len = base ;
  return 0 ;
}