sauniquename.c (671B)
1 /* ISC license. */ 2 3 #include <unistd.h> 4 #include <skalibs/types.h> 5 #include <skalibs/djbunix.h> 6 #include <skalibs/skamisc.h> 7 #include <skalibs/stralloc.h> 8 #include <skalibs/tai.h> 9 10 int sauniquename (stralloc *sa) 11 { 12 size_t base = sa->len ; 13 int wasnull = !sa->s ; 14 15 if (!stralloc_readyplus(sa, TIMESTAMP + PID_FMT + 131)) return 0 ; 16 sa->s[base] = ':' ; 17 timestamp(sa->s + base + 1) ; 18 sa->s[base + 1 + TIMESTAMP] = ':' ; 19 sa->len = base + 2 + TIMESTAMP ; 20 sa->len += pid_fmt(sa->s + sa->len, getpid()) ; 21 sa->s[sa->len++] = ':' ; 22 if (sagethostname(sa) < 0) goto err ; 23 return 1 ; 24 25 err: 26 if (wasnull) stralloc_free(sa) ; else sa->len = base ; 27 return 0 ; 28 }