master
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7#include <sys/stat.h>
8#include <stdlib.h>
9
10/**
11 * Returns _path without trailing slash if any
12 *
13 * - if _path has no trailing slash, the function returns it
14 * - if _path has a trailing slash, but is of the form C:/, then it returns it
15 * - otherwise, the function creates a new string, which is a copy of _path
16 * without the trailing slash. It is then the responsibility of the caller
17 * to free it.
18 */
19
20char* __mingw_fix_stat_path (const char* _path);
21char* __mingw_fix_stat_path (const char* _path)
22{
23 int len;
24 char *p;
25
26 p = (char*)_path;
27
28 if (_path && *_path) {
29 len = strlen (_path);
30
31 /* Ignore X:\ */
32
33 if (len <= 1 || ((len == 2 || len == 3) && _path[1] == ':'))
34 return p;
35
36 /* Check UNC \\abc\<name>\ */
37 if ((_path[0] == '\\' || _path[0] == '/')
38 && (_path[1] == '\\' || _path[1] == '/'))
39 {
40 const char *r = &_path[2];
41 while (*r != 0 && *r != '\\' && *r != '/')
42 ++r;
43 if (*r != 0)
44 ++r;
45 if (*r == 0)
46 return p;
47 while (*r != 0 && *r != '\\' && *r != '/')
48 ++r;
49 if (*r != 0)
50 ++r;
51 if (*r == 0)
52 return p;
53 }
54
55 if (_path[len - 1] == '/' || _path[len - 1] == '\\')
56 {
57 p = (char*)malloc (len);
58 memcpy (p, _path, len - 1);
59 p[len - 1] = '\0';
60 }
61 }
62
63 return p;
64}