diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/sauniquename.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/sauniquename.c')
-rw-r--r-- | src/libstddjb/sauniquename.c | 28 |
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 ; +} |