int __gsh_open (char *path, int kind) { int fd; int mode; int perm; TCHAR wpath[32768]; if (kind > READ_MODE) { mode = O_WRONLY | O_CREAT | O_BINARY | O_NOINHERIT; if (kind == APPEND_MODE) { mode = mode | O_APPEND; } perm = S_IWRITE; } else { perm = S_IREAD; mode = O_RDONLY | O_BINARY | O_NOINHERIT; } S2WSC (wpath, path, 32768); fd = _topen (wpath, mode, perm); return fd < 0 ? -1 : fd; }
int __gnat_mkdir (char *dir_name, int encoding ATTRIBUTE_UNUSED) { #if defined (__vxworks) /* Pretend that the system mkdir is posix compliant even though it sometimes is not, not expecting the second argument in some configurations (e.g. vxworks 653 2.2, difference from 2.5). The second actual argument will just be ignored in this case. */ typedef int posix_mkdir (const char * name, mode_t mode); posix_mkdir * vxmkdir = (posix_mkdir *)&mkdir; return vxmkdir (dir_name, S_IRWXU | S_IRWXG | S_IRWXO); #elif defined (__MINGW32__) TCHAR wname [GNAT_MAX_PATH_LEN + 2]; if (encoding == Encoding_Unspecified) S2WSC (wname, dir_name, GNAT_MAX_PATH_LEN); else if (encoding == Encoding_UTF8) S2WSU (wname, dir_name, GNAT_MAX_PATH_LEN); else S2WS (wname, dir_name, GNAT_MAX_PATH_LEN); return _tmkdir (wname); #else return mkdir (dir_name, S_IRWXU | S_IRWXG | S_IRWXO); #endif }
unsigned long __gsh_unlink (char *path) { /* Initialize the UNICODE string that contains the file name using native representation. */ unsigned int len = strlen(path); WCHAR wname[len + 1]; WCHAR wname2[len + 1 + 4]; UNICODE_STRING name; UNLINK_RESULT result; S2WSC (wname, path, len + 1); _tcscpy(wname2, L"\\??\\"); _tcscat(wname2, wname); name.Length = (len + 4) * sizeof(WCHAR); name.MaximumLength = name.Length; name.Buffer = wname2; result = safe_unlink(name); if (NT_SUCCESS(result.last_error_code)) return 0; return result.last_error_code; }
void * __gsh_open_directory (char *path) { /* Initialize the UNICODE string that contains the file name using native representation. */ unsigned int len = strlen(path); WCHAR wname[len + 1]; WCHAR wname2[len + 1 + 4]; UNICODE_STRING name; NTSTATUS result; HANDLE handle; S2WSC (wname, path, len + 1); _tcscpy(wname2, L"\\??\\"); _tcscat(wname2, wname); name.Length = (len + 4) * sizeof (WCHAR); name.MaximumLength = name.Length; name.Buffer = wname2; result = __gsh_u_open_directory (name, &handle); if (NT_SUCCESS(result)) return handle; return NULL; }
static TCHAR * wjoin (char *args[]) { char *aresult; int asize = 1; int k = 0; TCHAR *wresult; int wsize = 0; while (args[k]) { asize += strlen (args[k]) + 1; k++; } aresult = (char *) malloc (asize); aresult[0] = 0; k = 0; while (args[k]) { strcat (aresult, args[k]); strcat (aresult, " "); k++; } wsize = asize * 2; wresult = (TCHAR *) malloc (wsize); S2WSC (wresult, aresult, wsize); free (aresult); return wresult; }
int __gnat_mkdir (char *dir_name, int encoding ATTRIBUTE_UNUSED) { #if defined (__vxworks) && !(defined (__RTP__) && (_WRS_VXWORKS_MINOR != 0)) return mkdir (dir_name); #elif defined (__MINGW32__) TCHAR wname [GNAT_MAX_PATH_LEN + 2]; if (encoding == Encoding_Unspecified) S2WSC (wname, dir_name, GNAT_MAX_PATH_LEN); else if (encoding == Encoding_UTF8) S2WSU (wname, dir_name, GNAT_MAX_PATH_LEN); else S2WS (wname, dir_name, GNAT_MAX_PATH_LEN); return _tmkdir (wname); #else return mkdir (dir_name, S_IRWXU | S_IRWXG | S_IRWXO); #endif }
unsigned long __gsh_copy_file (char *source, char *target, char fail_if_exists, char preserve_attributes) { unsigned int slen = strlen(source); unsigned int tlen = strlen(target); /* Ensure we have the Windows build number */ if (windows_bid == 0) { windows_bid = (DWORD) (HIWORD (GetVersion())); } BOOL result; WCHAR wsource[slen + 1]; WCHAR wsource2[slen + 1 + 4]; WCHAR wtarget[tlen + 1]; WCHAR wtarget2[tlen + 1 + 4]; SECURITY_ATTRIBUTES SA; HANDLE h; FILETIME ft; SYSTEMTIME st; /* create source and target filenames in UNICODE format). */ S2WSC (wsource, source, slen + 1); S2WSC (wtarget, target, tlen + 1); /* On Windows XP it seems that CopyFile does not support long paths starting with \?\ so use the regular path names which limit the path size to 262. */ if (windows_bid > WINDOWS_XP_BID) { _tcscpy (wsource2, L"\\??\\"); _tcscpy (wtarget2, L"\\??\\"); _tcscat (wsource2, wsource); _tcscat (wtarget2, wtarget); } else { _tcscpy(wsource2, wsource); _tcscpy(wtarget2, wtarget); } result = CopyFile (wsource2, wtarget2, fail_if_exists != 0); if (result == 0) { return (unsigned long) GetLastError(); } if (preserve_attributes == 0) { /* By default windows keep attributes. So if preserve_attribute is FALSE then reset the timestamps. */ SA.nLength = sizeof (SECURITY_ATTRIBUTES); SA.bInheritHandle = FALSE; GetSystemTime (&st); SystemTimeToFileTime (&st, &ft); SA.lpSecurityDescriptor = NULL; h = CreateFile (wtarget2, GENERIC_WRITE, FILE_SHARE_READ, &SA, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); SetFileTime (h, &ft, &ft, &ft); CloseHandle (h); } return 0; }
HANDLE __gsh_no_block_spawn (char *args[], char *cwd, char *env[], int pstdin, int pstdout, int pstderr) { BOOL result; STARTUPINFO SI; PROCESS_INFORMATION PI; SECURITY_ATTRIBUTES SA; TCHAR *wcommand; TCHAR *wcwd = NULL; void *block_env = NULL; /* Startup info. */ SI.cb = sizeof (STARTUPINFO); SI.lpReserved = NULL; SI.lpDesktop = NULL; SI.lpTitle = NULL; SI.dwFlags = STARTF_USESTDHANDLES; SI.wShowWindow = SW_HIDE; SI.cbReserved2 = 0; SI.lpReserved2 = NULL; SI.hStdInput = (HANDLE) _get_osfhandle (pstdin); SI.hStdOutput = (HANDLE) _get_osfhandle (pstdout); SI.hStdError = (HANDLE) _get_osfhandle (pstderr); /* Security attributes. */ SA.nLength = sizeof (SECURITY_ATTRIBUTES); SA.bInheritHandle = TRUE; SA.lpSecurityDescriptor = NULL; /* Prepare the command string. */ wcommand = wjoin (args); /* Then the environment block */ if (env != NULL) block_env = ablock (env); /* Convert dir to widestrig */ if (cwd != NULL) { wcwd = (TCHAR *) malloc (strlen (cwd) * 2 + 2); S2WSC (wcwd, cwd, strlen (cwd) * 2 + 2); } result = CreateProcess (NULL, wcommand, &SA, NULL, TRUE, GetPriorityClass (GetCurrentProcess()), block_env, wcwd, &SI, &PI); free (block_env); free (wcommand); free (wcwd); if (result == TRUE) { CloseHandle (PI.hThread); return PI.hProcess; } else { printf ("%d\n", GetLastError ()); return NULL; } }