diff options
| author | Bill Wohler | 2014-02-23 18:04:35 -0800 |
|---|---|---|
| committer | Bill Wohler | 2014-02-23 18:04:35 -0800 |
| commit | 3e93bafb95608467e438ba7f725fd1f020669f8c (patch) | |
| tree | f2f90109f283e06a18caea3cb2a2623abcfb3a92 /lib-src/ntlib.c | |
| parent | 791c0d7634e44bb92ca85af605be84ff2ae08963 (diff) | |
| parent | e918e27fdf331e89268fc2c9d7cf838d3ecf7aa7 (diff) | |
| download | emacs-3e93bafb95608467e438ba7f725fd1f020669f8c.tar.gz emacs-3e93bafb95608467e438ba7f725fd1f020669f8c.zip | |
Merge from trunk; up to 2014-02-23T23:41:17Z!lekktu@gmail.com.
Diffstat (limited to 'lib-src/ntlib.c')
| -rw-r--r-- | lib-src/ntlib.c | 85 |
1 files changed, 82 insertions, 3 deletions
diff --git a/lib-src/ntlib.c b/lib-src/ntlib.c index f43117457cb..23d7ba77b8e 100644 --- a/lib-src/ntlib.c +++ b/lib-src/ntlib.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* Utility and Unix shadow routines for GNU Emacs support programs on NT. | 1 | /* Utility and Unix shadow routines for GNU Emacs support programs on NT. |
| 2 | 2 | ||
| 3 | Copyright (C) 1994, 2001-2013 Free Software Foundation, Inc. | 3 | Copyright (C) 1994, 2001-2014 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | Author: Geoff Voelker (voelker@cs.washington.edu) | 5 | Author: Geoff Voelker (voelker@cs.washington.edu) |
| 6 | Created: 10-8-94 | 6 | Created: 10-8-94 |
| @@ -34,21 +34,26 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 34 | 34 | ||
| 35 | #include "ntlib.h" | 35 | #include "ntlib.h" |
| 36 | 36 | ||
| 37 | /* MinGW64 defines _TIMEZONE_DEFINED and defines 'struct timespec' in | ||
| 38 | its system headers. */ | ||
| 39 | #ifndef _TIMEZONE_DEFINED | ||
| 37 | struct timezone | 40 | struct timezone |
| 38 | { | 41 | { |
| 39 | int tz_minuteswest; /* minutes west of Greenwich */ | 42 | int tz_minuteswest; /* minutes west of Greenwich */ |
| 40 | int tz_dsttime; /* type of dst correction */ | 43 | int tz_dsttime; /* type of dst correction */ |
| 41 | }; | 44 | }; |
| 45 | #endif | ||
| 42 | 46 | ||
| 43 | #define MAXPATHLEN _MAX_PATH | 47 | #define MAXPATHLEN _MAX_PATH |
| 44 | 48 | ||
| 45 | /* Emulate sleep...we could have done this with a define, but that | 49 | /* Emulate sleep...we could have done this with a define, but that |
| 46 | would necessitate including windows.h in the files that used it. | 50 | would necessitate including windows.h in the files that used it. |
| 47 | This is much easier. */ | 51 | This is much easier. */ |
| 48 | void | 52 | unsigned |
| 49 | sleep (unsigned long seconds) | 53 | sleep (unsigned seconds) |
| 50 | { | 54 | { |
| 51 | Sleep (seconds * 1000); | 55 | Sleep (seconds * 1000); |
| 56 | return 0; | ||
| 52 | } | 57 | } |
| 53 | 58 | ||
| 54 | /* Get the current working directory. */ | 59 | /* Get the current working directory. */ |
| @@ -134,6 +139,12 @@ getuid (void) | |||
| 134 | } | 139 | } |
| 135 | 140 | ||
| 136 | unsigned | 141 | unsigned |
| 142 | geteuid (void) | ||
| 143 | { | ||
| 144 | return getuid (); | ||
| 145 | } | ||
| 146 | |||
| 147 | unsigned | ||
| 137 | getgid (void) | 148 | getgid (void) |
| 138 | { | 149 | { |
| 139 | return 0; | 150 | return 0; |
| @@ -412,3 +423,71 @@ lstat (const char * path, struct stat * buf) | |||
| 412 | return stat (path, buf); | 423 | return stat (path, buf); |
| 413 | } | 424 | } |
| 414 | 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 | |||
| 436 | int | ||
| 437 | mkostemp (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. */ | ||
| 482 | int | ||
| 483 | sys_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 | } | ||