summaryrefslogtreecommitdiff
path: root/src/libstddjb/sauniquename.c
blob: ff102fae0bf4210401f597d8605cb0be895c3405 (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
/* ISC license. */

#include <unistd.h>
#include <skalibs/types.h>
#include <skalibs/djbunix.h>
#include <skalibs/skamisc.h>
#include <skalibs/stralloc.h>
#include <skalibs/tai.h>

int sauniquename (stralloc *sa)
{
  unsigned int base = sa->len ;
  int wasnull = !sa->s ;

  if (!stralloc_readyplus(sa, TIMESTAMP + UINT_FMT + 131)) return 0 ;
  sa->s[base] = ':' ;
  timestamp(sa->s + base + 1) ;
  sa->s[base + 1 + TIMESTAMP] = ':' ;
  sa->len = base + 2 + TIMESTAMP ;
  sa->len += pid_fmt(sa->s + sa->len, getpid()) ;
  sa->s[sa->len++] = ':' ;
  if (sagethostname(sa) == -1) goto err ;
  return 1 ;

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