aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src/ntlib.c
diff options
context:
space:
mode:
authorXue Fuqiao2013-09-04 08:39:34 +0800
committerXue Fuqiao2013-09-04 08:39:34 +0800
commitadf2fc4a01efe77d73cd52bc9173914ed56ff531 (patch)
treea5a280a5554a7bffeaf94fccae29fa3ac1a5d066 /lib-src/ntlib.c
parent63191d9f2043d2e67657e85a7b3842805dd1dad6 (diff)
parent38726039b77db432989fed106c88e9f1aa463281 (diff)
downloademacs-adf2fc4a01efe77d73cd52bc9173914ed56ff531.tar.gz
emacs-adf2fc4a01efe77d73cd52bc9173914ed56ff531.zip
Merge from mainline.
Diffstat (limited to 'lib-src/ntlib.c')
-rw-r--r--lib-src/ntlib.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib-src/ntlib.c b/lib-src/ntlib.c
index 41b4e3a0cbc..ab7d8b590df 100644
--- a/lib-src/ntlib.c
+++ b/lib-src/ntlib.c
@@ -422,3 +422,72 @@ lstat (const char * path, struct stat * buf)
422{ 422{
423 return stat (path, buf); 423 return stat (path, buf);
424} 424}
425
426/* Implementation of mkostemp for MS-Windows, to avoid race conditions
427 when using mktemp. Copied from w32.c.
428
429 This is used only in update-game-score.c. It is overkill for that
430 use case, since update-game-score renames the temporary file into
431 the game score file, which isn't atomic on MS-Windows anyway, when
432 the game score already existed before running the program, which it
433 almost always does. But using a simpler implementation just to
434 make a point is uneconomical... */
435
436int
437mkostemp (char * template, int flags)
438{
439 char * p;
440 int i, fd = -1;
441 unsigned uid = GetCurrentThreadId ();
442 int save_errno = errno;
443 static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
444
445 errno = EINVAL;
446 if (template == NULL)
447 return -1;
448
449 p = template + strlen (template);
450 i = 5;
451 /* replace up to the last 5 X's with uid in decimal */
452 while (--p >= template && p[0] == 'X' && --i >= 0)
453 {
454 p[0] = '0' + uid % 10;
455 uid /= 10;
456 }
457
458 if (i < 0 && p[0] == 'X')
459 {
460 i = 0;
461 do
462 {
463 p[0] = first_char[i];
464 if ((fd = open (template,
465 flags | _O_CREAT | _O_EXCL | _O_RDWR,
466 S_IRUSR | S_IWUSR)) >= 0
467 || errno != EEXIST)
468 {
469 if (fd >= 0)
470 errno = save_errno;
471 return fd;
472 }
473 }
474 while (++i < sizeof (first_char));
475 }
476
477 /* Template is badly formed or else we can't generate a unique name. */
478 return -1;
479}
480
481/* On Windows, you cannot rename into an existing file. */
482int
483sys_rename (const char *from, const char *to)
484{
485 int retval = rename (from, to);
486
487 if (retval < 0 && errno == EEXIST)
488 {
489 if (unlink (to) == 0)
490 retval = rename (from, to);
491 }
492 return retval;
493}