comparison src/w32.c @ 15354:0e45e7d8bfdf

(sys_mktemp): Complete rewrite.
author Richard M. Stallman <rms@gnu.org>
date Fri, 07 Jun 1996 23:07:00 +0000
parents e64bd8310edc
children 6686192ea2b3
comparison
equal deleted inserted replaced
15353:26a5accd904d 15354:0e45e7d8bfdf
986 sys_mkdir (const char * path) 986 sys_mkdir (const char * path)
987 { 987 {
988 return _mkdir (map_win32_filename (path, NULL)); 988 return _mkdir (map_win32_filename (path, NULL));
989 } 989 }
990 990
991 /* Because of long name mapping issues, we need to implement this
992 ourselves. Also, MSVC's _mktemp returns NULL when it can't generate
993 a unique name, instead of setting the input template to an empty
994 string.
995
996 Standard algorithm seems to be use pid or tid with a letter on the
997 front (in place of the 6 X's) and cycle through the letters to find a
998 unique name. We extend that to allow any reasonable character as the
999 first of the 6 X's. */
991 char * 1000 char *
992 sys_mktemp (char * template) 1001 sys_mktemp (char * template)
993 { 1002 {
994 return (char *) map_win32_filename ((const char *) _mktemp (template), NULL); 1003 char * p;
1004 int i;
1005 unsigned uid = GetCurrentThreadId ();
1006 static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
1007
1008 if (template == NULL)
1009 return NULL;
1010 p = template + strlen (template);
1011 i = 5;
1012 /* replace up to the last 5 X's with uid in decimal */
1013 while (--p >= template && p[0] == 'X' && --i >= 0)
1014 {
1015 p[0] = '0' + uid % 10;
1016 uid /= 10;
1017 }
1018
1019 if (i < 0 && p[0] == 'X')
1020 {
1021 i = 0;
1022 do
1023 {
1024 int save_errno = errno;
1025 p[0] = first_char[i];
1026 if (sys_access (template, 0) < 0)
1027 {
1028 errno = save_errno;
1029 return template;
1030 }
1031 }
1032 while (++i < sizeof (first_char));
1033 }
1034
1035 /* Template is badly formed or else we can't generate a unique name,
1036 so return empty string */
1037 template[0] = 0;
1038 return template;
995 } 1039 }
996 1040
997 int 1041 int
998 sys_open (const char * path, int oflag, int mode) 1042 sys_open (const char * path, int oflag, int mode)
999 { 1043 {