mirror of
https://github.com/ZDoom/gzdoom-gles.git
synced 2024-11-10 14:51:46 +00:00
- give tmpfileplus its own RNG.
This should not interfere with the global one which can be explicitly seeded. It also seems a bit pointless to constantly re-seed the RNG for something this inconsequential.
This commit is contained in:
parent
e78503b770
commit
f6f17fbfb4
2 changed files with 7 additions and 88 deletions
|
@ -67,23 +67,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/* ADDED IN v2.0 */
|
||||
|
||||
/*
|
||||
* NAME
|
||||
* tmpfileplus_f - create a unique temporary file with filename stored in a fixed-length buffer
|
||||
*
|
||||
* SYNOPSIS
|
||||
* FILE *tmpfileplus_f(const char *dir, const char *prefix, char *pathnamebuf, size_t pathsize, int keep);
|
||||
*
|
||||
* DESCRIPTION
|
||||
* Same as tmpfileplus() except receives filename in a fixed-length buffer. No allocated memory to free.
|
||||
|
||||
* ERRORS
|
||||
* E2BIG Resulting filename is too big for the buffer `pathnamebuf`.
|
||||
|
||||
*/
|
||||
|
||||
#include "tmpfileplus.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -112,47 +95,32 @@
|
|||
#define FDOPEN_ fdopen
|
||||
#endif
|
||||
|
||||
#include "m_random.h"
|
||||
#include "cmdlib.h"
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#define FILE_SEPARATOR "\\"
|
||||
#else
|
||||
#define FILE_SEPARATOR "/"
|
||||
#endif
|
||||
|
||||
#define RANDCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
#define NRANDCHARS (sizeof(RANDCHARS) - 1)
|
||||
|
||||
static FRandom pr_tmpfile;
|
||||
|
||||
/** Replace each byte in string s with a random character from TEMPCHARS */
|
||||
static char *set_randpart(char *s)
|
||||
{
|
||||
size_t i;
|
||||
unsigned int r;
|
||||
static unsigned int seed; /* NB static */
|
||||
|
||||
if (seed == 0)
|
||||
{ /* First time set our seed using current time and clock */
|
||||
seed = ((unsigned)time(NULL)<<8) ^ (unsigned)clock();
|
||||
}
|
||||
srand(seed++);
|
||||
|
||||
for (i = 0; i < strlen(s); i++)
|
||||
{
|
||||
r = rand() % NRANDCHARS;
|
||||
r = pr_tmpfile() % NRANDCHARS;
|
||||
s[i] = (RANDCHARS)[r];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/** Return 1 if path is a valid directory otherwise 0 */
|
||||
static int is_valid_dir(const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
if ((stat(path, &st) == 0) && (st.st_mode & S_IFDIR))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Call getenv and save a copy in buf */
|
||||
static char *getenv_save(const char *varname, char *buf, size_t bufsize)
|
||||
{
|
||||
|
@ -203,7 +171,7 @@ static FILE *mktempfile_internal(const char *tmpdir, const char *pfx, char **tmp
|
|||
pmode = S_IRUSR|S_IWUSR;
|
||||
#endif
|
||||
|
||||
if (!tmpdir || !is_valid_dir(tmpdir)) {
|
||||
if (!tmpdir || !DirExists(tmpdir)) {
|
||||
errno = ENOENT;
|
||||
return NULL;
|
||||
}
|
||||
|
@ -295,32 +263,3 @@ FILE *tmpfileplus(const char *dir, const char *prefix, char **pathname, int keep
|
|||
return fp;
|
||||
}
|
||||
|
||||
/* Same as tmpfileplus() but with fixed length buffer for output filename and no memory allocation */
|
||||
FILE *tmpfileplus_f(const char *dir, const char *prefix, char *pathnamebuf, size_t pathsize, int keep)
|
||||
{
|
||||
char *tmpbuf = NULL;
|
||||
FILE *fp;
|
||||
|
||||
/* If no buffer provided, do the normal way */
|
||||
if (!pathnamebuf || (int)pathsize <= 0) {
|
||||
return tmpfileplus(dir, prefix, NULL, keep);
|
||||
}
|
||||
/* Call with a temporary buffer */
|
||||
fp = tmpfileplus(dir, prefix, &tmpbuf, keep);
|
||||
if (fp && strlen(tmpbuf) > pathsize - 1) {
|
||||
/* Succeeded but not enough room in output buffer, so clean up and return an error */
|
||||
pathnamebuf[0] = 0;
|
||||
fclose(fp);
|
||||
if (keep) remove(tmpbuf);
|
||||
free(tmpbuf);
|
||||
errno = E2BIG;
|
||||
return NULL;
|
||||
}
|
||||
/* Copy name into buffer */
|
||||
strcpy(pathnamebuf, tmpbuf);
|
||||
free(tmpbuf);
|
||||
|
||||
return fp;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -23,10 +23,6 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Create a unique temporary file.
|
||||
@param dir (optional) directory to create file. If NULL use default TMP directory.
|
||||
@param prefix (optional) prefix for file name. If NULL use "tmp.".
|
||||
|
@ -40,22 +36,6 @@ extern "C" {
|
|||
FILE *tmpfileplus(const char *dir, const char *prefix, char **pathname, int keep);
|
||||
|
||||
|
||||
/** Create a unique temporary file with filename stored in a fixed-length buffer.
|
||||
@param dir (optional) directory to create file. If NULL use default directory.
|
||||
@param prefix (optional) prefix for file name. If NULL use "tmp.".
|
||||
@param pathnamebuf (optional) buffer to receive full pathname of temporary file. Ignored if NULL.
|
||||
@param pathsize Size of buffer to receive filename and its terminating null character.
|
||||
@param keep If `keep` is nonzero and `pathname` is not NULL, then keep the file after closing.
|
||||
Otherwise file is automatically deleted when closed.
|
||||
@return Pointer to stream opened in binary read/write (w+b) mode, or a null pointer on error.
|
||||
@exception E2BIG Resulting filename is too big for the buffer `pathnamebuf`.
|
||||
*/
|
||||
FILE *tmpfileplus_f(const char *dir, const char *prefix, char *pathnamebuf, size_t pathsize, int keep);
|
||||
|
||||
#define TMPFILE_KEEP 1
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end TMPFILEPLUS_H_ */
|
||||
|
|
Loading…
Reference in a new issue