8 #if (defined(__unix__) || defined(__APPLE__)) && !defined(__dos__)
16 // Maximum length for a vsprintf string
17 #define MAX_F_STRING 1025
19 #if defined(__WIN32__)
21 #define WIN32_LEAN_AND_MEAN
35 typedef unsigned int FileAttribs;
36 typedef unsigned char byte;
37 typedef unsigned int FileSize;
38 typedef unsigned short uint16;
39 typedef unsigned int uint;
40 typedef unsigned long long uint64;
41 typedef uint64 FileSize64;
46 #define null ((void *)0)
48 #define isDirectory 0x0040
50 #define MAX_LOCATION 797
51 #define MAX_FILENAME 274
53 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
54 void * __ecereNameSpace__ecere__com__eSystem_New(unsigned int size);
55 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
56 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
57 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
59 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(const char * source, int * wordCount);
60 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(const char * source, uint16 * dest, int max);
61 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(const uint16 * source);
62 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(const uint16 * source, char * dest, int max);
64 char * __ecereNameSpace__ecere__sys__StripLastDirectory(const char * string, char * output);
66 char * __ecereNameSpace__ecere__sys__GetEnvironment(const char * envName, char * envValue, int max);
67 char * __ecereNameSpace__ecere__sys__SearchString(const char * buffer, int start, const char * subStr, bool matchCase, bool matchWord);
69 FileAttribs FILE_FileExists(const char * fileName);
71 bool System_MoveFile(const char * source, const char * dest)
75 uint16 * _wsource = __ecereNameSpace__ecere__sys__UTF8toUTF16(source, null);
76 uint16 * _wdest = __ecereNameSpace__ecere__sys__UTF8toUTF16(dest, null);
77 result = MoveFileEx(_wsource, _wdest, MOVEFILE_COPY_ALLOWED) != 0;
78 __ecereNameSpace__ecere__com__eSystem_Delete(_wsource);
79 __ecereNameSpace__ecere__com__eSystem_Delete(_wdest);
82 return rename(source, dest) == 0;
86 bool System_RenameFile(const char * oldName, const char * newName)
88 #if defined(__WIN32__)
90 uint16 * _woldName = __ecereNameSpace__ecere__sys__UTF8toUTF16(oldName, null);
91 uint16 * _wnewName = __ecereNameSpace__ecere__sys__UTF8toUTF16(newName, null);
92 result = _wrename(_woldName, _wnewName) == 0;
93 __ecereNameSpace__ecere__com__eSystem_Delete(_woldName);
94 __ecereNameSpace__ecere__com__eSystem_Delete(_wnewName);
98 return rename(oldName, newName) == 0;
102 bool System_DeleteFile(const char * fileName)
105 #if defined(__WIN32__)
106 uint16 * _wfileName = __ecereNameSpace__ecere__sys__UTF8toUTF16(fileName, null);
107 if(_wunlink(_wfileName))
109 //if(errno == 13/*EACCES*/) printf("delete file access error\n");
110 //else if(errno == 2/*ENOENT*/) printf("delete file file does not exist error\n");
111 __ecereNameSpace__ecere__com__eSystem_Delete(_wfileName);
118 bool System_MakeDir(const char * path)
121 char location[MAX_LOCATION] = "";
125 if(path[0] && path[1] == ':')
127 else if(path[0] == '\\' && path[1] == '\\')
131 if(path[0] == '/' || path[0] == '\\')
134 strncpy(location, path, c);
140 char directory[MAX_FILENAME];
143 for(;(ch = path[c]) && (ch == '/' || ch == '\\'); c++);
144 for(;(ch = path[c]) && (ch != '/' && ch != '\\'); c++)
146 if(len < MAX_FILENAME)
147 directory[len++] = ch;
149 directory[len] = '\0';
150 if(locationLen > 0 &&
151 (location[locationLen-1] != '\\' && location[locationLen-1] != '/'))
152 strcat(location, DIR_SEPS);
153 strcat(location, directory);
154 locationLen = strlen(location);
156 if(FILE_FileExists(location) != isDirectory)
158 #if defined(__unix__) || defined(__APPLE__)
159 result = !mkdir(location, 0755);
161 uint16 _wlocation[MAX_LOCATION];
162 __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(location, _wlocation, MAX_LOCATION);
163 result = !_wmkdir(_wlocation);
170 bool System_RemoveDir(const char * path)
173 char location[MAX_LOCATION] = "";
177 if(path[0] && path[1] == ':')
179 else if(path[0] == '\\' && path[1] == '\\')
183 if(path[0] == '/' || path[0] == '\\')
186 strncpy(location, path, c);
192 char directory[MAX_FILENAME];
195 for(;(ch = path[c]) && (ch == '/' || ch == '\\'); c++);
196 for(;(ch = path[c]) && (ch != '/' && ch != '\\'); c++)
198 if(len < MAX_FILENAME)
199 directory[len++] = ch;
201 directory[len] = '\0';
202 if(locationLen > 0 &&
203 (location[locationLen-1] != '\\' && location[locationLen-1] != '/'))
204 strcat(location, DIR_SEPS);
205 strcat(location, directory);
206 locationLen = strlen(location);
208 if(FILE_FileExists(location) == isDirectory)
210 #if defined(__unix__) || defined(__APPLE__)
213 uint16 _wlocation[MAX_LOCATION];
214 __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(location, _wlocation, MAX_LOCATION);
223 char * System_GetWorkingDir(char * buf, int size)
225 #if defined(__WIN32__)
226 uint16 * _wbuf = __ecereNameSpace__ecere__com__eSystem_New(sizeof(uint16) * size);
227 _wgetcwd(_wbuf, size);
228 __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(_wbuf, buf, size);
229 __ecereNameSpace__ecere__com__eSystem_Delete(_wbuf);
232 return getcwd(buf, size);
236 bool System_ChangeWorkingDir(const char * buf)
238 #if defined(__WIN32__)
240 uint16 * _wbuf = __ecereNameSpace__ecere__sys__UTF8toUTF16(buf, null);
241 result = !chdir(buf);
242 __ecereNameSpace__ecere__com__eSystem_Delete(_wbuf);
249 const char * System_GetEnvironment(const char * envName, char * envValue, int max)
251 #if defined(__WIN32__)
252 uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null);
257 success = GetEnvironmentVariable(_wenvName, result, sizeof(result) / sizeof(uint16));
258 //result = _wgetenv(_wenvName);
260 if(success && success < sizeof(result) / sizeof(uint16))
261 __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(result, envValue, max);
265 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName);
266 // Distinguish empty vs. unexisting environment variables with GetLastError()
267 return (success || !GetLastError()) ? envValue : null;
269 char * result = getenv(envName);
271 strncpy(envValue, result, max);
274 return result ? envValue : null;
278 void System_SetEnvironment(const char * envName, const char * envValue)
280 #if defined(__WIN32__)
281 uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null);
282 uint16 * _wenvValue = __ecereNameSpace__ecere__sys__UTF8toUTF16(envValue, null);
283 SetEnvironmentVariable(_wenvName, _wenvValue);
284 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName);
285 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvValue);
287 setenv(envName, envValue, 1);
291 void System_UnsetEnvironment(const char * envName)
293 #if defined(__WIN32__)
294 uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null);
295 SetEnvironmentVariable(_wenvName, null);
296 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName);
302 bool System_Execute(const char * env, const char * command, va_list args, bool wait)
305 char commandLine[MAX_F_STRING*4];
306 vsnprintf(commandLine, sizeof(commandLine)-1, command, args);
307 commandLine[sizeof(commandLine)-1] = 0;
311 if(!wait) strcat(commandLine, "&");
316 PROCESS_INFORMATION pi;
317 STARTUPINFO si = { 0 };
318 uint16 * _wcommandLine = __ecereNameSpace__ecere__sys__UTF8toUTF16(commandLine, null);
320 // Set up the start up info struct.
321 si.cb = sizeof(STARTUPINFO);
322 // if(CreateProcess(null, _wcommandLine, null, null, TRUE, 0, env, null, &si, &pi))
323 if(CreateProcess(null, _wcommandLine, null, null, TRUE, CREATE_NEW_CONSOLE, (void *)env, null, &si, &pi))
326 WaitForSingleObject(pi.hProcess, INFINITE);
328 CloseHandle(pi.hProcess);
329 CloseHandle(pi.hThread);
332 __ecereNameSpace__ecere__com__eSystem_Delete(_wcommandLine);
338 bool System_ShellOpen(const char * fileName, va_list args)
341 char filePath[MAX_F_STRING*4];
343 #if defined(__WIN32__)
345 vsnprintf(filePath+1, sizeof(filePath)-2,fileName, args);
347 vsnprintf(filePath, sizeof(filePath), fileName, args);
349 filePath[sizeof(filePath)-1] = 0;
351 len = strlen(filePath);
352 #if defined(__WIN32__)
354 filePath[len+1] = '\0';
356 filePath[len] = '\0';
359 #if !defined(__WIN32__)
361 char command[MAX_LOCATION] = "";
362 char desktop[MAX_F_STRING];
363 __ecereNameSpace__ecere__sys__GetEnvironment("ECERE_DESKTOP", desktop, sizeof(desktop));
364 if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "ecere", false, false))
365 sprintf(command, "ede-open \"%s\" &", filePath);
368 __ecereNameSpace__ecere__sys__GetEnvironment("DESKTOP_SESSION", desktop, sizeof(desktop));
369 if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "gnome", false, false))
370 sprintf(command, "gnome-open \"%s\" &", filePath);
371 else if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "kde", false, false))
372 sprintf(command, "kde-open \"%s\" &", filePath);
374 sprintf(command, "xdg-open \"%s\" &", filePath);
377 if(command[0] && system(command) != -1)
380 #elif defined(ECERE_VANILLA)
382 uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null);
383 if(_wsystem(_wfilePath) != -1)
385 __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath);
389 uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null);
390 char curDir[MAX_LOCATION];
392 __ecereNameSpace__ecere__sys__StripLastDirectory(filePath, curDir);
393 _wcurDir = __ecereNameSpace__ecere__sys__UTF8toUTF16(curDir, null);
394 //if(ShellExecute(null, "open", _wfilePath, null, curDir, SW_SHOWNORMAL) > 32)
395 if((void *)ShellExecute(null, null, _wfilePath, null, _wcurDir, SW_SHOWNORMAL) > (void *)32)
397 __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath);
398 __ecereNameSpace__ecere__com__eSystem_Delete(_wcurDir);
404 void System_GetFreeSpace(const char * path, FileSize64 * size)
408 uint16 * _wpath = __ecereNameSpace__ecere__sys__UTF8toUTF16(path, null);
409 GetDiskFreeSpaceEx(_wpath, (PULARGE_INTEGER)&freeSize, null, null);
410 __ecereNameSpace__ecere__com__eSystem_Delete(_wpath);
412 *size = (FileSize64)freeSize;