summaryrefslogtreecommitdiff
path: root/gobject-introspection/grealpath.h
blob: f5af7cb58c038293df3c46f1d3e643d44346c670 (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
#ifndef __G_REALPATH_H__
#define __G_REALPATH_H__

#include <stdlib.h>

/**
 * g_realpath:
 *
 * this should be a) filled in for win32 and b) put in glib...
 */
	
static inline gchar*
g_realpath (const char *path)
{
#ifndef _WIN32
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
	char buffer [PATH_MAX];
	if (realpath(path, buffer))
		return g_strdup(buffer);
	else
		return NULL;
#else
	/* We don't want to include <windows.h> as it clashes horribly
	 * with token names from scannerparser.h. So just declare
	 * GetFullPathNameA() here.
	 */
	extern __stdcall GetFullPathNameA(const char*, int, char*, char**);
	char *buffer;
	char dummy;
	int rc, len;

	rc = GetFullPathNameA(path, 1, &dummy, NULL);

	if (rc == 0)
	  {
	    /* Weird failure, so just return the input path as such */
	    return g_strdup(path);
	  }

	len = rc + 1;
	buffer = g_malloc(len);

	rc = GetFullPathNameA(path, len, buffer, NULL);

	if (rc == 0 || rc > len)
	  {
	    /* Weird failure again */
	    g_free(buffer);
	    return g_strdup(path);
	  }

	return buffer;
#endif
}

#endif