From 034abb1cc56e6b4d15282635ea46f276b97680d8 Mon Sep 17 00:00:00 2001 From: David Levine Date: Mon, 3 Feb 2014 21:42:44 -0600 Subject: [PATCH] Use rename(2), not link(2), in m_mktemps() if mkstemps() is not available. --- h/prototypes.h | 2 +- sbr/m_mktemp.c | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/h/prototypes.h b/h/prototypes.h index fceef407..1ff92f64 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -191,7 +191,7 @@ int m_putenv (char *, char *); int m_rand (unsigned char *, size_t); char *m_mktemp(const char *, int *, FILE **); char *m_mktemp2(const char *, const char *, int *, FILE **); -char *m_mktemps(const char *pfx, const char *, int *, FILE **); +char *m_mktemps(const char *pfx, const char *suffix, int *, FILE **); char *get_temp_dir(); void m_unknown(m_getfld_state_t *, FILE *); int makedir (char *); diff --git a/sbr/m_mktemp.c b/sbr/m_mktemp.c index 17cf7458..009df0a1 100644 --- a/sbr/m_mktemp.c +++ b/sbr/m_mktemp.c @@ -181,31 +181,15 @@ m_mktemps( fd = mkstemp(tmpfil); { char *oldfilename = tmpfil; - tmpfil = concat(oldfilename, suffix, NULL); - /* link(2) requires that the new path not exist. And if we - have to resort to rename(2), at least try to remove a file - that would be in the way. */ - if (unlink(tmpfil) != 0 && errno != ENOENT) { - advise("unlink", "Failed to unlink \"%s\"", tmpfil); - (void) unlink(oldfilename); - free(oldfilename); - free(tmpfil); - return NULL; - } - - /* link() doesn't always work, such as on Windows FAT - filesystems. If it fails, try rename(). */ - if (link(oldfilename, tmpfil) != 0 && - rename(oldfilename, tmpfil) != 0) { + if (rename(oldfilename, tmpfil) != 0) { (void) unlink(oldfilename); free(oldfilename); free(tmpfil); return NULL; } - (void) unlink(oldfilename); free(oldfilename); } #endif /* ! HAVE_MKSTEMPS */ -- 2.56.0