File: platform_posix.c

package info (click to toggle)
premake 3.7-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,148 kB
  • sloc: ansic: 20,490; cs: 15,185; makefile: 332
file content (249 lines) | stat: -rw-r--r-- 5,130 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**********************************************************************
 * Premake - platform_posix.h
 * Windows-specific functions.
 *
 * Copyright (c) 2002-2005 Jason Perkins.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License in the file LICENSE.txt for details.
 **********************************************************************/

#include "os.h"
#if defined(PLATFORM_POSIX)

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <dirent.h>
#include <fnmatch.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include "io.h"
#include "path.h"
#include "util.h"

static char buffer[8192];

struct PlatformMaskData
{
	DIR* handle;
	struct dirent* entry;
	char* mask;
};


int platform_chdir(const char* path)
{
	return !chdir(path);
}


int platform_copyfile(const char* src, const char* dest)
{
	sprintf(buffer, "cp %s %s", src, dest);
	return (system(buffer) == 0);
}


static int findLibHelper(const char* lib, const char* path)
{
	struct stat sb;

	sprintf(buffer, "%s/lib%s.so", path, lib);
	if (stat(buffer, &sb) == 0 && !S_ISDIR(sb.st_mode)) return 1;

	sprintf(buffer, "%s/%s.so", path, lib);
	if (stat(buffer, &sb) == 0 && !S_ISDIR(sb.st_mode)) return 1;

	sprintf(buffer, "%s/%s", path, lib);
	if (stat(buffer, &sb) == 0 && !S_ISDIR(sb.st_mode)) return 1;

	return 0;
}

int platform_findlib(const char* name, char* buffer, int len)
{
	FILE* file;
	char* libpaths;
	char* token;

	libpaths = getenv("LD_LIBRARY_PATH");
	token = libpaths != NULL ? strtok(libpaths, ":") : NULL;
	while (token != NULL)
	{
		if (findLibHelper(name, buffer))
		{
			strcpy(buffer, token);
			return 1;
		}
		token = strtok(NULL, ":");
	}

	file = fopen("/etc/ld.so.conf", "rt");
	if (file == NULL) 
		return 0;

	while (!feof(file))
	{
		/* Read a line and trim off any trailing whitespace */
		char* ptr;
		fgets(buffer, 4096, file);
		ptr = &buffer[strlen(buffer) - 1];
		while (isspace(*ptr))
			*(ptr--) = '\0';

		if (findLibHelper(name, buffer))
		{
			fclose(file);
			return 1;
		}
	}

	fclose(file);

	if (findLibHelper(name, "/lib"))
	{
		strcpy(buffer, "/lib");
		return 1;
	}

	if (findLibHelper(name, "/usr/lib"))
	{
		strcpy(buffer, "/usr/lib");
		return 1;
	}

	return 0;
}


int platform_getcwd(char* buffer, int len)
{
	return (getcwd(buffer, len) == 0);
}


void platform_getuuid(char* uuid)
{
	FILE* rnd = fopen("/dev/random", "rb");
	fread(uuid, 16, 1, rnd);
	fclose(rnd);
}


char platform_getseparator()
{
	return '/';
}


int platform_isAbsolutePath(const char* path)
{
	return (path[0] == '/');
}


int platform_mask_close(MaskHandle data)
{
	if (data->handle != NULL)
		closedir(data->handle);
	free(data->mask);
	free(data);
	return 1;
}


const char* platform_mask_getname(MaskHandle data)
{
	strcpy(buffer, path_getdir(data->mask));
	if (strlen(buffer) > 0)
		strcat(buffer, "/");
	strcat(buffer, data->entry->d_name);
	return buffer;
}


int platform_mask_getnext(MaskHandle data)
{
	const char* mask = path_getname(data->mask);
	
	if (data->handle == NULL)
		return 0;
		
	data->entry = readdir(data->handle);
	while (data->entry != NULL)
	{
		if (fnmatch(mask, data->entry->d_name, 0) == 0)
			return 1;
		data->entry = readdir(data->handle);
	}
	return 0;
}


int platform_mask_isfile(MaskHandle data)
{
	/* Converting to absolute removes any directories from
	 * the path that haven't been created yet (see below) */
	struct stat info;
	const char* name = platform_mask_getname(data);
	name = path_absolute(name);
	if (stat(name, &info) == 0)
	{
		return S_ISREG(info.st_mode);
	}
	return 0;
}


MaskHandle platform_mask_open(const char* mask)
{
	const char* path = path_getdir(mask);
	if (strlen(path) == 0)
		path = ".";
	
	MaskHandle data = ALLOCT(struct PlatformMaskData);
	data->mask = (char*)malloc(strlen(mask) + 1);
	strcpy(data->mask, mask);
	
	/* Converting to absolute path removes any directories
	 * from the path that haven't been created yet. Can
	 * happen if package.path = (something) where (something)
	 * doesn't exist at the time the script is running. */
	path = path_absolute(path);
	data->handle = opendir(path);
	
	return data;
}


int platform_mkdir(const char* path)
{
	return (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
}


int platform_remove(const char* path)
{
	unlink(path);
	return 1;
}


int platform_rmdir(const char* path)
{
	strcpy(buffer, "rm -rf ");
	strcat(buffer, path);
	return (system(buffer) == 0);
}

#endif