From af6d5f438bf18c92730a8ae273efa54cd11f2906 Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Sun, 5 Nov 2017 15:42:31 +0100 Subject: ripe-atlas-fw: imported version 4780 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- libbb/atlas_name_macro.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 libbb/atlas_name_macro.c (limited to 'libbb/atlas_name_macro.c') diff --git a/libbb/atlas_name_macro.c b/libbb/atlas_name_macro.c new file mode 100644 index 0000000..0674fd0 --- /dev/null +++ b/libbb/atlas_name_macro.c @@ -0,0 +1,87 @@ +#include "libbb.h" + +#define URANDOM_DEV "/dev/urandom" + +static char hex_chars[]= "0123456789abcdef"; + +char *atlas_name_macro(char *str) +{ + unsigned char c; + int i, fd; + size_t len; + char *p, *in, *out; + char buf[256]; + unsigned char random_buf[8]; + + p= strchr(str, '$'); + if (p == NULL) + return strdup(str); + + in= str; + out= buf; + + while (*in) + { + p= strchr(in, '$'); + if (p == NULL) + { + strlcpy(out, in, buf+sizeof(buf)-out); + break; + } + if (p != in) + { + len= p-in; + + if (len+1 > buf+sizeof(buf)-out) + return NULL; + memcpy(out, in, len); + out[len]= '\0'; + + out += len; + } + + switch(p[1]) + { + case 'p': + snprintf(out, buf+sizeof(buf)-out, "%d", + get_probe_id()); + break; + case 't': + snprintf(out, buf+sizeof(buf)-out, "%ld", + (long)time(NULL)); + break; + case 'r': + /* We need to hex digits per byte in random_buf */ + if (sizeof(random_buf)*2+1 > buf+sizeof(buf)-out) + return NULL; + + fd= open(URANDOM_DEV, O_RDONLY); + + /* Best effort, just ignore errors */ + if (fd != -1) + { + read(fd, random_buf, sizeof(random_buf)); + close(fd); + } + + for (i= 0; i> 4) & 0xf]; + out[1]= hex_chars[c & 0xf]; + out += 2; + } + + out[0]= '\0'; + break; + + default: + return NULL; + } + in= p+2; + out += strlen(out); + } + + return strdup(buf); +} -- cgit v1.2.3