1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-10 08:29:02 +00:00

Add tempfile function.

This commit is contained in:
david
2013-09-26 07:17:03 +00:00
parent 31bc2847bd
commit e4af8d90d8
3 changed files with 40 additions and 0 deletions

View File

@@ -267,3 +267,5 @@ extern void set_lf_mode(void);
extern int setenv_portable(const char *name, const char *value);
extern void setup_environment(struct fdinfo *fdinfo);
extern char *tempfile(const char *dir, const char *prefix);

View File

@@ -436,3 +436,36 @@ int setenv_portable(const char *name, const char *value)
{
return setenv(name, value, 1);
}
/* Create a name temporary file. This function aims to emulate tempnam, with the
exception that it creates the file like mkstemp does.
The directories tried are, in order,
* $TMPDIR
* dir
* /tmp
*/
char *tempfile(const char *dir, const char *prefix)
{
const char *SUFFIX = "XXXXXXXXXX";
const char *tmpdir;
char *namebuf;
size_t namelen;
int n;
tmpdir = NULL;
if ((getuid() == geteuid()) && (getgid() == getegid()))
tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = dir;
if (tmpdir == NULL)
tmpdir = "/tmp";
namelen = strlen(tmpdir) + 1 + strlen(prefix) + strlen(SUFFIX);
namebuf = malloc(namelen + 1);
n = Snprintf(namebuf, namelen + 1, "%s/%s%s", tmpdir, prefix, SUFFIX);
ncat_assert(n >= 0 && n <= namelen);
n = mkstemp(namebuf);
if (n == -1)
return NULL;
return namebuf;
}

View File

@@ -178,3 +178,8 @@ int ssl_load_default_ca_certs(SSL_CTX *ctx)
return rc == 1 ? 0 : -1;
}
#endif
char *tempfile(const char *dir, const char *prefix)
{
return tempnam(dir, prefix);
}