void System_SetEnvironment(char * envName, char * envValue) { #if defined(__WIN32__) uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null); uint16 * _wenvValue = __ecereNameSpace__ecere__sys__UTF8toUTF16(envValue, null); SetEnvironmentVariable(_wenvName, _wenvValue); __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName); __ecereNameSpace__ecere__com__eSystem_Delete(_wenvValue); #else setenv(envName, envValue, 1); #endif }
bool System_MoveFile(char * source, char * dest) { #ifdef __WIN32__ bool result; uint16 * _wsource = __ecereNameSpace__ecere__sys__UTF8toUTF16(source, null); uint16 * _wdest = __ecereNameSpace__ecere__sys__UTF8toUTF16(dest, null); result = MoveFileEx(_wsource, _wdest, MOVEFILE_COPY_ALLOWED) != 0; __ecereNameSpace__ecere__com__eSystem_Delete(_wsource); __ecereNameSpace__ecere__com__eSystem_Delete(_wdest); return result; #else return rename(source, dest) == 0; #endif }
bool System_ShellOpen(char * fileName, va_list args) { bool result = false; char filePath[MAX_F_STRING]; int len; #if defined(__WIN32__) filePath[0] = '"'; vsprintf(filePath+1, fileName, args); #else vsprintf(filePath, fileName, args); #endif len = strlen(filePath); #if defined(__WIN32__) filePath[len] = '"'; filePath[len+1] = '\0'; #else filePath[len] = '\0'; #endif #if !defined(__WIN32__) { strcat(filePath, " &"); if(system(filePath) != -1) result = true; } #elif defined(ECERE_VANILLA) { uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null); if(_wsystem(_wfilePath) != -1) result = true; __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath); } #else { uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null); char curDir[MAX_LOCATION]; uint16 * _wcurDir; __ecereNameSpace__ecere__sys__StripLastDirectory(filePath, curDir); _wcurDir = __ecereNameSpace__ecere__sys__UTF8toUTF16(curDir, null); //if(ShellExecute(null, "open", _wfilePath, null, curDir, SW_SHOWNORMAL) > 32) if((void *)ShellExecute(null, null, _wfilePath, null, _wcurDir, SW_SHOWNORMAL) > (void *)32) result = true; __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath); __ecereNameSpace__ecere__com__eSystem_Delete(_wcurDir); } #endif return result; }
bool System_RenameFile(char * oldName, char * newName) { #if defined(__WIN32__) bool result; uint16 * _woldName = __ecereNameSpace__ecere__sys__UTF8toUTF16(oldName, null); uint16 * _wnewName = __ecereNameSpace__ecere__sys__UTF8toUTF16(newName, null); result = _wrename(_woldName, _wnewName) == 0; __ecereNameSpace__ecere__com__eSystem_Delete(_woldName); __ecereNameSpace__ecere__com__eSystem_Delete(_wnewName); return result; #else return rename(oldName, newName) == 0; #endif }
bool System_MoveFile(const char * source, const char * dest, uint replaceAndFlush) { #ifdef __WIN32__ bool result; uint16 * _wsource = __ecereNameSpace__ecere__sys__UTF8toUTF16(source, null); uint16 * _wdest = __ecereNameSpace__ecere__sys__UTF8toUTF16(dest, null); // TODO: Select options individually result = MoveFileEx(_wsource, _wdest, MOVEFILE_COPY_ALLOWED | (replaceAndFlush ? MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH : 0)) != 0; __ecereNameSpace__ecere__com__eSystem_Delete(_wsource); __ecereNameSpace__ecere__com__eSystem_Delete(_wdest); return result; #else return rename(source, dest) == 0; #endif }
bool System_Execute(char * env, char * command, va_list args) { bool result = false; char commandLine[MAX_F_STRING]; vsprintf(commandLine, command, args); #ifndef __WIN32__ { strcat(commandLine, "&"); system(commandLine); } #else { PROCESS_INFORMATION pi; STARTUPINFO si = { 0 }; uint16 * _wcommandLine = __ecereNameSpace__ecere__sys__UTF8toUTF16(commandLine, null); // Set up the start up info struct. si.cb = sizeof(STARTUPINFO); // if(CreateProcess(null, _wcommandLine, null, null, TRUE, 0, env, null, &si, &pi)) if(CreateProcess(null, _wcommandLine, null, null, TRUE, CREATE_NEW_CONSOLE, env, null, &si, &pi)) result = true; __ecereNameSpace__ecere__com__eSystem_Delete(_wcommandLine); } #endif return result; }
char * System_GetEnvironment(char * envName, char * envValue, int max) { #if defined(__WIN32__) uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null); //uint16 * result; uint16 result[2048]; int success; success = GetEnvironmentVariable(_wenvName, result, sizeof(result) / sizeof(uint16)); //result = _wgetenv(_wenvName); //if(result) if(success && success < sizeof(result) / sizeof(uint16)) __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(result, (byte *)envValue, max); else envValue[0] = 0; __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName); return envValue; //result ? envValue : null; #else char * result = getenv(envName); if(result) strncpy(envValue, result, max); else envValue[0] = 0; return result ? envValue : null; #endif }
bool System_Execute(const char * env, const char * command, va_list args, bool wait) { bool result = false; char commandLine[MAX_F_STRING*4]; vsnprintf(commandLine, sizeof(commandLine)-1, command, args); commandLine[sizeof(commandLine)-1] = 0; #ifndef __WIN32__ { if(!wait) strcat(commandLine, "&"); result = system(commandLine) != -1; } #else { PROCESS_INFORMATION pi; STARTUPINFO si = { 0 }; uint16 * _wcommandLine = __ecereNameSpace__ecere__sys__UTF8toUTF16(commandLine, null); // Set up the start up info struct. si.cb = sizeof(STARTUPINFO); // if(CreateProcess(null, _wcommandLine, null, null, TRUE, 0, env, null, &si, &pi)) if(CreateProcess(null, _wcommandLine, null, null, TRUE, CREATE_NEW_CONSOLE, (void *)env, null, &si, &pi)) { if(wait) WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); result = true; } __ecereNameSpace__ecere__com__eSystem_Delete(_wcommandLine); } #endif return result; }
const char * System_GetEnvironment(const char * envName, char * envValue, int max) { #if defined(__WIN32__) uint16 * _wenvName = __ecereNameSpace__ecere__sys__UTF8toUTF16(envName, null); //uint16 * result; uint16 result[2048]; int success; success = GetEnvironmentVariable(_wenvName, result, sizeof(result) / sizeof(uint16)); //result = _wgetenv(_wenvName); //if(result) if(success && success < sizeof(result) / sizeof(uint16)) __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(result, envValue, max); else envValue[0] = 0; __ecereNameSpace__ecere__com__eSystem_Delete(_wenvName); // Distinguish empty vs. unexisting environment variables with GetLastError() return (success || !GetLastError()) ? envValue : null; #else char * result = getenv(envName); if(result) strncpy(envValue, result, max); else envValue[0] = 0; return result ? envValue : null; #endif }
void System_GetFreeSpace(char * path, FileSize * size) { uint64 freeSize = 0; #ifdef __WIN32__ uint16 * _wpath = __ecereNameSpace__ecere__sys__UTF8toUTF16(path, null); GetDiskFreeSpaceEx(_wpath, (PULARGE_INTEGER)&freeSize, null, null); __ecereNameSpace__ecere__com__eSystem_Delete(_wpath); #endif *size = (FileSize)freeSize; }
bool System_ChangeWorkingDir(char * buf) { #if defined(__WIN32__) bool result; uint16 * _wbuf = __ecereNameSpace__ecere__sys__UTF8toUTF16(buf, null); result = !chdir(buf); __ecereNameSpace__ecere__com__eSystem_Delete(_wbuf); return result; #else return !chdir(buf); #endif }
bool System_DeleteFile(char * fileName) { bool result = true; #if defined(__WIN32__) uint16 * _wfileName = __ecereNameSpace__ecere__sys__UTF8toUTF16(fileName, null); if(_wunlink(_wfileName)) result = false; //if(errno == 13/*EACCES*/) printf("delete file access error\n"); //else if(errno == 2/*ENOENT*/) printf("delete file file does not exist error\n"); __ecereNameSpace__ecere__com__eSystem_Delete(_wfileName); #else unlink(fileName); #endif return result; }
bool System_ShellOpen(char * fileName, va_list args) { bool result = false; char filePath[MAX_F_STRING]; int len; #if defined(__WIN32__) filePath[0] = '"'; vsprintf(filePath+1, fileName, args); #else vsprintf(filePath, fileName, args); #endif len = strlen(filePath); #if defined(__WIN32__) filePath[len] = '"'; filePath[len+1] = '\0'; #else filePath[len] = '\0'; #endif #if !defined(__WIN32__) { char command[MAX_LOCATION] = ""; char desktop[MAX_F_STRING]; __ecereNameSpace__ecere__sys__GetEnvironment("ECERE_DESKTOP", desktop, sizeof(desktop)); if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "ecere", false, false)) sprintf(command, "ede-open \"%s\" &", filePath); else { __ecereNameSpace__ecere__sys__GetEnvironment("DESKTOP_SESSION", desktop, sizeof(desktop)); if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "gnome", false, false)) sprintf(command, "gnome-open \"%s\" &", filePath); else if(__ecereNameSpace__ecere__sys__SearchString(desktop, 0, "kde", false, false)) sprintf(command, "kde-open \"%s\" &", filePath); else { if(FILE_FileExists(filePath) != isDirectory) sprintf(command, "%s &", filePath); } } if(command[0] && system(command) != -1) result = true; } #elif defined(ECERE_VANILLA) { uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null); if(_wsystem(_wfilePath) != -1) result = true; __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath); } #else { uint16 * _wfilePath = __ecereNameSpace__ecere__sys__UTF8toUTF16(filePath, null); char curDir[MAX_LOCATION]; uint16 * _wcurDir; __ecereNameSpace__ecere__sys__StripLastDirectory(filePath, curDir); _wcurDir = __ecereNameSpace__ecere__sys__UTF8toUTF16(curDir, null); //if(ShellExecute(null, "open", _wfilePath, null, curDir, SW_SHOWNORMAL) > 32) if((void *)ShellExecute(null, null, _wfilePath, null, _wcurDir, SW_SHOWNORMAL) > (void *)32) result = true; __ecereNameSpace__ecere__com__eSystem_Delete(_wfilePath); __ecereNameSpace__ecere__com__eSystem_Delete(_wcurDir); } #endif return result; }
_DualPipe * _DualPipeOpen(PipeOpenMode mode, const char * commandLine, const char * env, void ** inputPtr, void ** outputPtr) { _DualPipe * f = null; #define PIPE_READ 0 #define PIPE_WRITE 1 #if !defined(__WIN32__) { FILE * input = null, * output = null; int hInput[2] = { 0 }, hOutput[2] = { 0 }; pid_t pid; bool result = true; if((mode & POM_error) || (mode & POM_output)) if(pipe(hOutput)) result = false; if((mode & POM_input)) if(pipe(hInput)) result = false; if(result) { pid = fork(); if(pid > 0) { // This process if(hInput[PIPE_READ]) { close(hInput[PIPE_READ]); output = fdopen(hInput[PIPE_WRITE],"w"); } if(hOutput[PIPE_WRITE]) { close(hOutput[PIPE_WRITE]); input = fdopen(hOutput[PIPE_READ],"r"); } } else if(pid == 0) { // Child process char * tokens[129]; int numTokens; char * commandLineCopy = __ecereNameSpace__ecere__sys__CopyString(commandLine); if(hInput[PIPE_WRITE]) close(hInput[PIPE_WRITE]); if(hOutput[PIPE_READ]) close(hOutput[PIPE_READ]); if((mode & POM_error) && hOutput[PIPE_WRITE] != STDERR_FILENO) dup2(hOutput[PIPE_WRITE], STDERR_FILENO); if((mode & POM_output) && hOutput[PIPE_WRITE] != STDOUT_FILENO) dup2(hOutput[PIPE_WRITE], STDOUT_FILENO); if(hOutput[PIPE_WRITE] && hOutput[PIPE_WRITE] != STDOUT_FILENO) close(hOutput[PIPE_WRITE]); if((mode & POM_input) && hInput[PIPE_READ] != STDIN_FILENO) { dup2(hInput[PIPE_READ], STDIN_FILENO); close(hInput[PIPE_READ]); } #if 0 //#ifdef _DEBUG fprintf(stderr, "\n_DualPipeOpen (in child): %s\n\n", commandLineCopy); #endif numTokens = __ecereNameSpace__ecere__sys__Tokenize(commandLineCopy, sizeof(tokens) / sizeof(tokens[0]) - 1, tokens, forArgsPassing); #if 0 //#ifdef _DEBUG { int c; for(c=0; c<numTokens; c++) fprintf(stderr, "argv[%d]: %s\n", c, tokens[c]); fprintf(stderr, "\n"); } #endif tokens[numTokens] = null; if(env) { char * envTokens[129]; char * envCopy = __ecereNameSpace__ecere__sys__CopyString(env); int numEnvTokens = __ecereNameSpace__ecere__sys__Tokenize(envCopy, sizeof(envTokens) / sizeof(envTokens[0]) - 1, envTokens, false); envTokens[numEnvTokens] = null; if(execve(tokens[0], tokens, envTokens) < 0) { __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); __ecereNameSpace__ecere__com__eSystem_Delete(envCopy); exit(1); } __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); __ecereNameSpace__ecere__com__eSystem_Delete(envCopy); exit(0); } else { if(execvp(tokens[0], (char **)tokens) < 0) { __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); exit(1); } __ecereNameSpace__ecere__com__eSystem_Delete(commandLineCopy); exit(0); } } if(input || output) { f = calloc(1, sizeof(_DualPipe)); *inputPtr = f->input = input; *outputPtr = f->output = output; f->pid = pid; } } else { if(hInput[PIPE_READ]) close(hInput[PIPE_READ]); if(hInput[PIPE_WRITE]) close(hInput[PIPE_WRITE]); if(hOutput[PIPE_WRITE]) close(hOutput[PIPE_WRITE]); if(hOutput[PIPE_READ]) close(hOutput[PIPE_READ]); } } #else { HANDLE hOutput[2] = { 0 },hOutputRead = 0; HANDLE hInput[2] = { 0 }, hInputWrite = 0; HANDLE hError[2] = { 0 }, hErrorRead = 0; HANDLE hStdErr = 0, hStdIn = 0, hStdOut = 0; SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi = { 0 }; STARTUPINFO si = { 0 }; uint16 * _wcommandLine = __ecereNameSpace__ecere__sys__UTF8toUTF16(commandLine, null); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = null; sa.bInheritHandle = TRUE; // Force redirecting if GUI application if(!(mode & POM_error)) hStdErr = GetStdHandle(STD_ERROR_HANDLE); if(!(mode & POM_input)) hStdIn = GetStdHandle(STD_INPUT_HANDLE); if(!(mode & POM_output)) hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if((mode & POM_output) || !hStdOut) CreatePipe(&hOutput[PIPE_READ],&hOutput[PIPE_WRITE],&sa,0); if(( (mode & POM_error) && !(mode & POM_output)) || (!(mode & POM_error) && !hStdErr)) CreatePipe(&hError[PIPE_READ], &hError[PIPE_WRITE],&sa,0); if((mode & POM_input) || !hStdIn) CreatePipe(&hInput[PIPE_READ], &hInput[PIPE_WRITE], &sa,0); if(hInput[PIPE_READ]) DuplicateHandle(GetCurrentProcess(),hInput[PIPE_WRITE],GetCurrentProcess(),&hInputWrite,0,FALSE,DUPLICATE_SAME_ACCESS); if((mode & POM_error) && (mode & POM_output)) { DuplicateHandle(GetCurrentProcess(),hOutput[PIPE_READ],GetCurrentProcess(),&hOutputRead,0,FALSE,DUPLICATE_SAME_ACCESS); DuplicateHandle(GetCurrentProcess(),hOutput[PIPE_WRITE],GetCurrentProcess(),&hError[PIPE_WRITE],0,TRUE,DUPLICATE_SAME_ACCESS); } else { if(hOutput[PIPE_WRITE]) DuplicateHandle(GetCurrentProcess(),hOutput[PIPE_READ],GetCurrentProcess(),&hOutputRead,0,FALSE,DUPLICATE_SAME_ACCESS); if(hError[PIPE_WRITE]) DuplicateHandle(GetCurrentProcess(),hError[PIPE_READ],GetCurrentProcess(),&hErrorRead,0,FALSE,DUPLICATE_SAME_ACCESS); } if(hOutput[PIPE_READ]) CloseHandle(hOutput[PIPE_READ]); if(hError[PIPE_READ]) CloseHandle(hError[PIPE_READ]); if(hInput[PIPE_WRITE]) CloseHandle(hInput[PIPE_WRITE]); // Set up the start up info struct. si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = (mode & POM_showWindow) ? SW_SHOW : SW_HIDE; si.hStdOutput = hOutput[PIPE_WRITE] ? hOutput[PIPE_WRITE] : hStdOut; si.hStdInput = hInput [PIPE_READ] ? hInput [PIPE_READ] : hStdIn; if((mode & POM_error) && (mode & POM_output)) si.hStdError = hOutput[PIPE_WRITE]; else if((mode & POM_error)) si.hStdError = hError[PIPE_WRITE]; else si.hStdError = hError[PIPE_WRITE] ? hError[PIPE_WRITE] : hStdErr; if(CreateProcess(null,_wcommandLine,null,null,TRUE, 0,(void *)env,null ,&si,&pi)) { CloseHandle(pi.hThread); f = calloc(1, sizeof(_DualPipe)); f->inputHandle = hOutputRead; f->outputHandle = hInputWrite; f->hProcess = pi.hProcess; f->pid = pi.dwProcessId; *inputPtr = null; *outputPtr = null; } else { if(hOutputRead) CloseHandle(hOutputRead); if(hInputWrite) CloseHandle(hInputWrite); if(hErrorRead) CloseHandle(hErrorRead); } if(hInput [PIPE_READ]) CloseHandle(hInput [PIPE_READ]); if(hOutput[PIPE_WRITE]) CloseHandle(hOutput[PIPE_WRITE]); if(hError [PIPE_WRITE]) CloseHandle(hError [PIPE_WRITE]); __ecereNameSpace__ecere__com__eSystem_Delete(_wcommandLine); } #endif return f; }