aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/mingw/stdio/__mingw_fix_stat_path.c
blob: 564658473f02a6287b2b70287fc315d6dc6cb392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the mingw-w64 runtime package.
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */

#include <sys/stat.h>
#include <stdlib.h>

/**
 * Returns _path without trailing slash if any
 *
 * - if _path has no trailing slash, the function returns it
 * - if _path has a trailing slash, but is of the form C:/, then it returns it
 * - otherwise, the function creates a new string, which is a copy of _path
 *   without the trailing slash. It is then the responsibility of the caller
 *   to free it.
 */

char* __mingw_fix_stat_path (const char* _path);
char* __mingw_fix_stat_path (const char* _path)
{
  int len;
  char *p;

  p = (char*)_path;

  if (_path && *_path) {
    len = strlen (_path);

    /* Ignore X:\ */

    if (len <= 1 || ((len == 2 || len == 3) && _path[1] == ':'))
      return p;

    /* Check UNC \\abc\<name>\ */
    if ((_path[0] == '\\' || _path[0] == '/')
	&& (_path[1] == '\\' || _path[1] == '/'))
      {
	const char *r = &_path[2];
	while (*r != 0 && *r != '\\' && *r != '/')
	  ++r;
	if (*r != 0)
	  ++r;
	if (*r == 0)
	  return p;
	while (*r != 0 && *r != '\\' && *r != '/')
	  ++r;
	if (*r != 0)
	  ++r;
	if (*r == 0)
	  return p;
      }

    if (_path[len - 1] == '/' || _path[len - 1] == '\\')
      {
	p = (char*)malloc (len);
	memcpy (p, _path, len - 1);
	p[len - 1] = '\0';
      }
  }

  return p;
}