summaryrefslogtreecommitdiff
path: root/src/libstddjb/sauniquename.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb/sauniquename.c')
-rw-r--r--src/libstddjb/sauniquename.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/libstddjb/sauniquename.c b/src/libstddjb/sauniquename.c
new file mode 100644
index 0000000..5a6ad83
--- /dev/null
+++ b/src/libstddjb/sauniquename.c
@@ -0,0 +1,28 @@
+/* ISC license. */
+
+#include <unistd.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/skamisc.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/tai.h>
+#include <skalibs/uint.h>
+
+int sauniquename (stralloc *sa)
+{
+ unsigned int base = sa->len ;
+ int wasnull = !sa->s ;
+
+ if (!stralloc_readyplus(sa, TIMESTAMP + UINT_FMT + 131)) return -1 ;
+ sa->s[base] = ':' ;
+ timestamp(sa->s + base + 1) ;
+ sa->s[base + 1 + TIMESTAMP] = ':' ;
+ sa->len = base + 2 + TIMESTAMP ;
+ sa->len += uint_fmt(sa->s + sa->len, getpid()) ;
+ sa->s[sa->len++] = ':' ;
+ if (sagethostname(sa) == -1) goto err ;
+ return 0 ;
+
+err:
+ if (wasnull) stralloc_free(sa) ; else sa->len = base ;
+ return -1 ;
+}