• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define __CRT__NO_INLINE
2 #include <sys/stat.h>
3 #include <stdlib.h>
4 
5 /**
6  * Returns _path without trailing slash if any
7  *
8  * - if _path has no trailing slash, the function returns it
9  * - if _path has a trailing slash, but is of the form C:/, then it returns it
10  * - otherwise, the function creates a new string, which is a copy of _path
11  *   without the trailing slash. It is then the responsibility of the caller
12  *   to free it.
13  */
14 
15 static wchar_t*
_mingw_no_trailing_slash(const wchar_t * _path)16 _mingw_no_trailing_slash (const wchar_t* _path)
17 {
18   int len;
19   wchar_t *p;
20 
21   p = (wchar_t*)_path;
22 
23   if (_path && *_path) {
24     len = wcslen (_path);
25 
26     /* Ignore X:\ */
27 
28     if (len <= 1 || ((len == 2 || len == 3) && _path[1] == L':'))
29       return p;
30 
31     /* Check UNC \\abc\<name>\ */
32     if ((_path[0] == L'\\' || _path[0] == L'/')
33 	&& (_path[1] == L'\\' || _path[1] == L'/'))
34       {
35 	const wchar_t *r = &_path[2];
36 	while (*r != 0 && *r != L'\\' && *r != L'/')
37 	  ++r;
38 	if (*r != 0)
39 	  ++r;
40 	if (*r == 0)
41 	  return p;
42 	while (*r != 0 && *r != L'\\' && *r != L'/')
43 	  ++r;
44 	if (*r != 0)
45 	  ++r;
46 	if (*r == 0)
47 	  return p;
48       }
49 
50     if (_path[len - 1] == L'/' || _path[len - 1] == L'\\')
51       {
52 	p = (wchar_t*)malloc (len * sizeof(wchar_t));
53 	memcpy (p, _path, (len - 1) * sizeof(wchar_t));
54 	p[len - 1] = L'\0';
55       }
56   }
57 
58   return p;
59 }
60 
_wstat64i32(const wchar_t * _Name,struct _stat64i32 * _Stat)61 int __cdecl _wstat64i32(const wchar_t *_Name,struct _stat64i32 *_Stat)
62 {
63   struct _stat64 st;
64   wchar_t *_path = _mingw_no_trailing_slash(_Name);
65 
66   int ret=_wstat64(_path,&st);
67 
68   if (_path != _Name)
69     free(_path);
70 
71   if (ret == -1) {
72     memset(_Stat,0,sizeof(struct _stat64i32));
73     return -1;
74   }
75   _Stat->st_dev=st.st_dev;
76   _Stat->st_ino=st.st_ino;
77   _Stat->st_mode=st.st_mode;
78   _Stat->st_nlink=st.st_nlink;
79   _Stat->st_uid=st.st_uid;
80   _Stat->st_gid=st.st_gid;
81   _Stat->st_rdev=st.st_rdev;
82   _Stat->st_size=(_off_t) st.st_size;
83   _Stat->st_atime=st.st_atime;
84   _Stat->st_mtime=st.st_mtime;
85   _Stat->st_ctime=st.st_ctime;
86   return ret;
87 }
88 
89