mutt stable branch with some hacks
1/* taken from XFCE's Xarchiver, made to work without glib for mutt */
2
3#include <sys/stat.h>
4#include <unistd.h>
5#include <errno.h>
6#include <time.h>
7#include <string.h>
8
9/* mkdtemp function for systems which don't have one */
10char *mkdtemp (char *tmpl)
11{
12 static const char LETTERS[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
13 static unsigned long value = 0;
14 unsigned long v;
15 int len;
16 int i, j;
17
18 len = strlen (tmpl);
19 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX") != 0)
20 {
21 errno = EINVAL;
22 return NULL;
23 }
24
25 value += ((unsigned long) time (NULL)) ^ getpid ();
26
27 for (i = 0; i < 7 ; ++i, value += 7777)
28 {
29 /* fill in the random bits */
30 for (j = 0, v = value; j < 6; ++j)
31 {
32 tmpl[(len - 6) + j] = LETTERS[v % 62];
33 v /= 62;
34 }
35
36 /* try to create the directory */
37 if (mkdir (tmpl, 0700) == 0)
38 return tmpl;
39 else if (errno != EEXIST)
40 return NULL;
41 }
42
43 errno = EEXIST;
44 return NULL;
45}