(mc_mkstemps): use g_mkstemp() to generate name of temporary file.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2012-10-08 17:35:03 +04:00
parent d27a4f86ea
commit 8b3ed9bfdc

View File

@ -777,72 +777,32 @@ mc_lseek (int fd, off_t offset, int whence)
int
mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
{
static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static unsigned long value;
struct timeval tv;
char *tmpbase;
char *tmpname;
char *XXXXXX;
char *ret_path;
int count;
if (strchr (prefix, PATH_SEP) == NULL)
{
/* Add prefix first to find the position of XXXXXX */
tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL);
}
else
{
tmpbase = g_strdup (prefix);
}
tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL);
ret_path = tmpname;
XXXXXX = &tmpname[strlen (tmpbase)];
g_free (tmpbase);
/* Get some more or less random data. */
gettimeofday (&tv, NULL);
value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
for (count = 0; count < TMP_MAX; ++count)
{
unsigned long v = value;
char *p1, *p2;
int fd;
/* Fill in the random bits. */
XXXXXX[0] = letters[v % 62];
v /= 62;
XXXXXX[1] = letters[v % 62];
v /= 62;
XXXXXX[2] = letters[v % 62];
v /= 62;
XXXXXX[3] = letters[v % 62];
v /= 62;
XXXXXX[4] = letters[v % 62];
v /= 62;
XXXXXX[5] = letters[v % 62];
fd = open (tmpname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
if (fd >= 0)
if (strchr (prefix, PATH_SEP) != NULL)
p1 = g_strdup (prefix);
else
{
/* Successfully created. */
*pname_vpath = vfs_path_from_str (ret_path);
g_free (ret_path);
return fd;
/* Add prefix first to find the position of XXXXXX */
p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
}
/* This is a random value. It is only necessary that the next
TMP_MAX values generated by adding 7777 to VALUE are different
with (module 2^32). */
value += 7777;
}
p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
g_free (p1);
/* Unsuccessful. Free the filename. */
g_free (ret_path);
fd = g_mkstemp (p2);
if (fd >= 0)
*pname_vpath = vfs_path_from_str (p2);
else
{
*pname_vpath = NULL;
fd = -1;
}
return -1;
g_free (p2);
return fd;
}
/* --------------------------------------------------------------------------------------------- */