• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define __CRT__NO_INLINE
2 #include <sys/stat.h>
3 #include <stdlib.h>
4 #include <malloc.h>
5 
6 /**
7  * Returns _path without trailing slash if any
8  *
9  * - if _path has no trailing slash, the function returns it
10  * - if _path has a trailing slash, but is of the form C:/, then it returns it
11  * - otherwise, the function creates a new string, which is a copy of _path
12  *   without the trailing slash. It is then the responsibility of the caller
13  *   to free it.
14  */
15 
16 static wchar_t*
_mingw_no_trailing_slash(const wchar_t * _path)17 _mingw_no_trailing_slash (const wchar_t* _path)
18 {
19   int len;
20   wchar_t *p;
21 
22   p = (wchar_t*)_path;
23 
24   if (_path && *_path) {
25     len = wcslen (_path);
26 
27     /* Ignore X:\ */
28 
29     if (len <= 1 || ((len == 2 || len == 3) && _path[1] == L':'))
30       return p;
31 
32     /* Check UNC \\abc\<name>\ */
33     if ((_path[0] == L'\\' || _path[0] == L'/')
34 	&& (_path[1] == L'\\' || _path[1] == L'/'))
35       {
36 	const wchar_t *r = &_path[2];
37 	while (*r != 0 && *r != L'\\' && *r != L'/')
38 	  ++r;
39 	if (*r != 0)
40 	  ++r;
41 	if (*r == 0)
42 	  return p;
43 	while (*r != 0 && *r != L'\\' && *r != L'/')
44 	  ++r;
45 	if (*r != 0)
46 	  ++r;
47 	if (*r == 0)
48 	  return p;
49       }
50 
51     if (_path[len - 1] == L'/' || _path[len - 1] == L'\\')
52       {
53 	p = (wchar_t*)malloc (len * sizeof(wchar_t));
54 	memcpy (p, _path, (len - 1) * sizeof(wchar_t));
55 	p[len - 1] = L'\0';
56       }
57   }
58 
59   return p;
60 }
61 
62 /* FIXME: Relying on _USE_32BIT_TIME_T, which is a user-macro,
63 during CRT compilation is plainly broken.  Need an appropriate
64 implementation to provide users the ability of compiling the
65 CRT only with 32-bit time_t behavior. */
66 #if defined(_USE_32BIT_TIME_T)
67 int __cdecl
wstat(const wchar_t * _Filename,struct stat * _Stat)68 wstat(const wchar_t *_Filename,struct stat *_Stat)
69 {
70   struct _stat32 st;
71   wchar_t *_path = _mingw_no_trailing_slash(_Filename);
72 
73   int ret=_wstat32(_path,&st);
74 
75   if (_path != _Filename)
76     free (_path);
77 
78   if (ret == -1) {
79     memset(_Stat,0,sizeof(struct stat));
80     return -1;
81   }
82   /* struct stat and struct _stat32
83      are the same for this case. */
84   memcpy(_Stat, &st, sizeof(struct _stat32));
85   return ret;
86 }
87 #else
88 int __cdecl
wstat(const wchar_t * _Filename,struct stat * _Stat)89 wstat(const wchar_t *_Filename,struct stat *_Stat)
90 {
91   struct _stat64 st;
92   wchar_t *_path = _mingw_no_trailing_slash(_Filename);
93 
94   int ret=_wstat64(_path,&st);
95 
96   if (_path != _Filename)
97     free (_path);
98 
99   if (ret == -1) {
100     memset(_Stat,0,sizeof(struct stat));
101     return -1;
102   }
103   /* struct stat and struct _stat64i32
104      are the same for this case. */
105   _Stat->st_dev=st.st_dev;
106   _Stat->st_ino=st.st_ino;
107   _Stat->st_mode=st.st_mode;
108   _Stat->st_nlink=st.st_nlink;
109   _Stat->st_uid=st.st_uid;
110   _Stat->st_gid=st.st_gid;
111   _Stat->st_rdev=st.st_rdev;
112   _Stat->st_size=(_off_t) st.st_size;
113   _Stat->st_atime=st.st_atime;
114   _Stat->st_mtime=st.st_mtime;
115   _Stat->st_ctime=st.st_ctime;
116   return ret;
117 }
118 #endif
119 
120