Skip to content

Instantly share code, notes, and snippets.

@huwan
Created April 14, 2019 08:12
Show Gist options
  • Save huwan/dd37932e126887449f95ef3683b7af20 to your computer and use it in GitHub Desktop.
Save huwan/dd37932e126887449f95ef3683b7af20 to your computer and use it in GitHub Desktop.
tmpname
#ifndef TMP_NAME_H
#define TMP_NAME_H 1
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define PATHLEN 20 //XXX: modify this if necessary
char str[PATHLEN];
// http://www.concentric.net/~Ttwang/tech/inthash.htm
static unsigned long mix(unsigned long a, unsigned long b, unsigned long c)
{
a=a-b; a=a-c; a=a^(c >> 13);
b=b-c; b=b-a; b=b^(a << 8);
c=c-a; c=c-b; c=c^(b >> 13);
a=a-b; a=a-c; a=a^(c >> 12);
b=b-c; b=b-a; b=b^(a << 16);
c=c-a; c=c-b; c=c^(b >> 5);
a=a-b; a=a-c; a=a^(c >> 3);
b=b-c; b=b-a; b=b^(a << 10);
c=c-a; c=c-b; c=c^(b >> 15);
return c;
}
static inline char *tmpname(void)
{
int i, len;
const char dir[] = "/mnt/pmem/"; //XXX: modify this if necessary
const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
unsigned long seed = mix((unsigned long)clock(), (unsigned long)time(NULL), (unsigned long)getpid());
srand(seed);
len = strlen(dir);
for (i = 0; i < len; i++)
str[i] = dir[i];
for (i = len; i < PATHLEN; i++) {
str[i] = charset[rand() % (int) (sizeof charset - 1)];
}
str[i] = '\0';
return str;
}
#ifdef __cplusplus
}
#endif
#endif /* tmpname.h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment