Welcome to mirror list, hosted at ThFree Co, Russian Federation.

os_pathsearch.c « host « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e26333ff53ba578141ded4867f0b10f6bbdae7e (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * \file   os_pathsearch.c
 * \brief  Locates a file, given a set of search paths.
 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
 *
 * \note This function is required by the bootstrapping code; it must be
 *       implemented here in the host and not scripted.
 */

#include <string.h>
#include "premake.h"


int os_pathsearch(lua_State* L)
{
	int i;
	for (i = 2; i <= lua_gettop(L); ++i)
	{
		const char* path;
		
		if (lua_isnil(L, i))
			continue;

		path = luaL_checkstring(L, i);
		do
		{
			const char* split;

			/* look for the closest path separator ; or : */
			/* can't use : on windows because it breaks on C:\path */
			const char* semi = strchr(path, ';');
#if !defined(PLATFORM_WINDOWS)
			const char* full = strchr(path, ':');
#else
			const char* full = NULL;
#endif

			if (!semi)
			{
				split = full;
			}
			else if (!full)
			{
				split = semi;
			}
			else
			{
				split = (semi < full) ? semi : full;
			}

			/* push this piece of the full search string onto the stack */
			if (split)
			{
				lua_pushlstring(L, path, split - path);
			}
			else
			{
				lua_pushstring(L, path);
			}

			/* keep an extra copy around, so I can return it if I have a match */
			lua_pushvalue(L, -1);

			/* append the filename to make the full test path */
			lua_pushstring(L, "/");
			lua_pushvalue(L, 1);
			lua_concat(L, 3);

			/* test it - if it exists return the path */
			if (do_isfile(lua_tostring(L, -1)))
			{
				lua_pop(L, 1);
				return 1;
			}

			/* no match, set up the next try */
			lua_pop(L, 2);
			path = (split) ? split + 1 : NULL;
		}
		while (path);
	}

	return 0;
}