10 #if (defined(__unix__) || defined(__APPLE__)) && !defined(__dos__)
18 // Maximum length for a vsprintf string
19 #define MAX_F_STRING 1025
21 #if defined(__WIN32__)
23 #define WIN32_LEAN_AND_MEAN
37 typedef unsigned int FileAttribs;
38 typedef unsigned char byte;
39 typedef unsigned int FileSize;
40 typedef unsigned short uint16;
41 typedef unsigned int uint;
42 typedef unsigned long long uint64;
43 typedef uint64 FileSize64;
48 #define null ((void *)0)
50 #define isDirectory 0x0040
52 #define MAX_LOCATION 797
53 #define MAX_FILENAME 274
55 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
56 void * __ecereNameSpace__ecere__com__eSystem_New(unsigned int size);
57 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
58 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
59 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
61 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(const char * source, int * wordCount);
62 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(const char * source, uint16 * dest, int max);
63 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(const uint16 * source);
64 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(const uint16 * source, char * dest, int max);
66 char * __ecereNameSpace__ecere__sys__StripLastDirectory(const char * string, char * output);
68 char * __ecereNameSpace__ecere__sys__GetEnvironment(const char * envName, char * envValue, int max);
69 char * __ecereNameSpace__ecere__sys__SearchString(const char * buffer, int start, const char * subStr, bool matchCase, bool matchWord);
71 FileAttribs FILE_FileExists(const char * fileName);
73 bool System_MoveFile(const char * source, const char * dest, uint replaceAndFlush)
77 uint16 * _wsource = __ecereNameSpace__ecere__sys__UTF8toUTF16(source, null);
78 uint16 * _wdest = __ecereNameSpace__ecere__sys__UTF8toUTF16(dest, null);
79 // TODO: Select options individually
80 result = MoveFileEx(_wsource, _wdest, MOVEFILE_COPY_ALLOWED | (replaceAndFlush ? MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH : 0)) != 0;
81 __ecereNameSpace__ecere__com__eSystem_Delete(_wsource);
82 __ecereNameSpace__ecere__com__eSystem_Delete(_wdest);
85 return rename(source, dest) == 0;
89 bool System_RenameFile(const char * oldName, const char * newName)
91 #if defined(__WIN32__)
93 uint16 * _woldName = __ecereNameSpace__ecere__sys__UTF8toUTF16(oldName, null);
94 uint16 * _wnewName = __ecereNameSpace__ecere__sys__UTF8toUTF16(newName, null);
95 result = _wrename(_woldName, _wnewName) == 0;
96 __ecereNameSpace__ecere__com__eSystem_Delete(_woldName);
97 __ecereNameSpace__ecere__com__eSystem_Delete(_wnewName);
101 return rename(oldName, newName) == 0;
105 bool System_DeleteFile(const char * fileName)
108 #if defined(__WIN32__)
109 uint16 * _wfileName = __ecereNameSpace__ecere__sys__UTF8toUTF16(fileName, null);
110 if(_wunlink(_wfileName))
112 //if(errno == 13/*EACCES*/) printf("delete file access error\n");
113 //else if(errno == 2/*ENOENT*/) printf("delete file file does not exist error\n");
114 __ecereNameSpace__ecere__com__eSystem_Delete(_wfileName);
121 bool System_MakeDir(const char * path)
124 char location[MAX_LOCATION] = "";
128 if(path[0] && path[1] == ':')
130 else if(path[0] == '\\' && path[1] == '\\')
134 if(path[0] == '/' || path[0] == '\\')
137 strncpy(location, path, c);
143 char directory[MAX_FILENAME];
146 for(;(ch = path[c]) && (ch == '/' || ch == '\\'); c++);
147 for(;(ch = path[c]) && (ch != '/' && ch != '\\'); c++)
149 if(len < MAX_FILENAME)
150 directory[len++] = ch;
152 directory[len] = '\0';
153 if(locationLen > 0 &&
154 (location[locationLen-1] != '\\' && location[locationLen-1] != '/'))
155 strcat(location, DIR_SEPS);
156 strcat(location, directory);
157 locationLen = strlen(location);
159 if(FILE_FileExists(location) != isDirectory)
161 #if defined(__unix__) || defined(__APPLE__)
162 result = !mkdir(location, 0755);
164 uint16 _wlocation[MAX_LOCATION];
165 __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(location, _wlocation, MAX_LOCATION);
166 result = !_wmkdir(_wlocation);
173 bool System_RemoveDir(const char * path)
176 char location[MAX_LOCATION] = "";
180 if(path[0] && path[1] == ':')
182 else if(path[0] == '\\' && path[1] == '\\')
186 if(path[0] == '/' || path[0] == '\\')
189 strncpy(location, path, c);
195 char directory[MAX_FILENAME];
198 for(;(ch = path[c]) && (ch == '/' || ch == '\\'); c++);
199 for(;(ch = path[c]) && (ch != '/' && ch != '\\'); c++)
201 if(len < MAX_FILENAME)
202 directory[len++] = ch;
204 directory[len] = '\0';
205 if(locationLen > 0 &&
206 (location[locationLen-1] != '\\' && location[locationLen-1] != '/'))
207 strcat(location, DIR_SEPS);
208 strcat(location, directory);
209 locationLen = strlen(location);
211 if(FILE_FileExists(location) == isDirectory)
213 #if defined(__unix__) || defined(__APPLE__)
216 uint16 _wlocation[MAX_LOCATION];
217 __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(location, _wlocation, MAX_LOCATION);
226 char * System_GetWorkingDir(char * buf, int size)
228 #if defined(__WIN32__)
229 uint16 * _wbuf = __ecereNameSpace__ecere__com__eSystem_New(sizeof(uint16) * size);
230 _wgetcwd(_wbuf, size);
231 __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(_wbuf, buf, size);
232 __ecereNameSpace__ecere__com__eSystem_Delete(_wbuf);
235 return getcwd(buf, size);
239 bool System_ChangeWorkingDir(const char * buf)
241 #if defined(__WIN32__)
243 uint16 * _wbuf = __ecereNameSpace__ecere__sys__UTF8toUTF16(buf, null);
244 result = !chdir(buf);
245 __ecereNameSpace__ecere__com__eSystem_Delete(_wbuf);
252 const char * System_GetEnvironment(const char * envName, char * envValue, int max)
254 #if defined(__WIN32__)
255 uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null);
260 success = GetEnvironmentVariable(_wenvName, result, sizeof(result) / sizeof(uint16));
261 //result = _wgetenv(_wenvName);
263 if(success && success < sizeof(result) / sizeof(uint16))
264 __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(result, envValue, max);
268 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName);
269 // Distinguish empty vs. unexisting environment variables with GetLastError()
270 return (success || !GetLastError()) ? envValue : null;
272 char * result = getenv(envName);
274 strncpy(envValue, result, max);
277 return result ? envValue : null;
281 void System_SetEnvironment(const char * envName, const char * envValue)
283 #if defined(__WIN32__)
284 uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null);
285 uint16 * _wenvValue = __ecereNameSpace__ecere__sys__UTF8toUTF16(envValue, null);
286 SetEnvironmentVariable(_wenvName, _wenvValue);
287 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName);
288 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvValue);
290 setenv(envName, envValue, 1);
294 void System_UnsetEnvironment(const char * envName)
296 #if defined(__WIN32__)
297 uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null);
298 SetEnvironmentVariable(_wenvName, null);
299 __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName);
305 bool System_Execute(const char * env, const char * command, va_list args, bool wait)
308 char commandLine[MAX_F_STRING*4];
309 vsnprintf(commandLine, sizeof(commandLine)-1, command, args);
310 commandLine[sizeof(commandLine)-1] = 0;
314 if(!wait) strcat(commandLine, "&");
315 result = system(commandLine) != -1;
319 PROCESS_INFORMATION pi;
320 STARTUPINFO si = { 0 };
321 uint16 * _wcommandLine = __ecereNameSpace__ecere__sys__UTF8toUTF16(commandLine, null);
323 // Set up the start up info struct.
324 si.cb = sizeof(STARTUPINFO);
325 // if(CreateProcess(null, _wcommandLine, null, null, TRUE, 0, env, null, &si, &pi))
326 if(CreateProcess(null, _wcommandLine, null, null, TRUE, CREATE_NEW_CONSOLE, (void *)env, null, &si, &pi))
329 WaitForSingleObject(pi.hProcess, INFINITE);
331 CloseHandle(pi.hProcess);
332 CloseHandle(pi.hThread);
335 __ecereNameSpace__ecere__com__eSystem_Delete(_wcommandLine);
341 bool System_ShellOpen(const char * fileName, va_list args)
344 char filePath[MAX_F_STRING*4];
346 #if defined(__WIN32__)
348 vsnprintf(filePath+1, sizeof(filePath)-2,fileName, args);
350 vsnprintf(filePath, sizeof(filePath), fileName, args);
352 filePath[sizeof(filePath)-1] = 0;
354 len = strlen(filePath);
355 #if defined(__WIN32__)
357 filePath[len+1] = '\0';
359 filePath[len] = '\0';
362 #if !defined(__WIN32__)
364 char command[MAX_LOCATION] = "";
365 char desktop[MAX_F_STRING];
366 __ecereNameSpace__ecere__sys__GetEnvironment("ECERE_DESKTOP", desktop, sizeof(desktop));
367 if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "ecere", false, false))
368 sprintf(command, "ede-open \"%s\" &", filePath);
371 __ecereNameSpace__ecere__sys__GetEnvironment("DESKTOP_SESSION", desktop, sizeof(desktop));
372 if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "gnome", false, false))
373 sprintf(command, "gnome-open \"%s\" &", filePath);
374 else if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "kde", false, false))
375 sprintf(command, "kde-open \"%s\" &", filePath);
377 sprintf(command, "xdg-open \"%s\" &", filePath);
380 if(command[0] && system(command) != -1)
383 #elif defined(ECERE_VANILLA)
385 uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null);
386 if(_wsystem(_wfilePath) != -1)
388 __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath);
392 uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null);
393 char curDir[MAX_LOCATION];
395 __ecereNameSpace__ecere__sys__StripLastDirectory(filePath, curDir);
396 _wcurDir = __ecereNameSpace__ecere__sys__UTF8toUTF16(curDir, null);
397 //if(ShellExecute(null, "open", _wfilePath, null, curDir, SW_SHOWNORMAL) > 32)
398 if((void *)ShellExecute(null, null, _wfilePath, null, _wcurDir, SW_SHOWNORMAL) > (void *)32)
400 __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath);
401 __ecereNameSpace__ecere__com__eSystem_Delete(_wcurDir);
407 void System_GetFreeSpace(const char * path, FileSize64 * size)
411 uint16 * _wpath = __ecereNameSpace__ecere__sys__UTF8toUTF16(path, null);
412 GetDiskFreeSpaceEx(_wpath, (PULARGE_INTEGER)&freeSize, null, null);
413 __ecereNameSpace__ecere__com__eSystem_Delete(_wpath);
415 *size = (FileSize64)freeSize;