blob: a27af4d0cd2fb7e113158487b2f93a8627f27976 [file] [log] [blame] [raw]
#include <windows.h>
#include <nt.h>
#include <errno.h>
#include <pathname.h>
#include <stdio.h>
int symlink(const char *oldpath, const char *newpath) {
if(!oldpath || !newpath) {
errno = EFAULT;
return -1;
}
void *handle;
UNICODE_STRING old_path;
UNICODE_STRING new_path;
RtlCreateUnicodeStringFromAsciiz(&old_path, oldpath);
RtlCreateUnicodeStringFromAsciiz(&new_path, newpath);
if(old_path.Buffer[old_path.Length / 2 - 1] == L'/') {
old_path.Buffer[old_path.Length / 2 - 1] = 0;
old_path.Length -= 2;
old_path.MaximumLength -= 2;
}
PATHNAME_UNIX2NT_UTF16_STRUCT(old_path);
PATHNAME_UNIX2NT_UTF16_STRUCT(new_path);
OBJECT_ATTRIBUTES new_object = { sizeof(OBJECT_ATTRIBUTES), NULL, &new_path, OBJ_PERMANENT, NULL, NULL };
long int status = NtCreateSymbolicLinkObject(&handle, SYMBOLIC_LINK_ALL_ACCESS, &new_object, &old_path);
RtlFreeUnicodeString(&old_path);
RtlFreeUnicodeString(&new_path);
//printf("nativelibc debug: symlink: status = 0x%lx\n", status);
if(ntstatus_to_errno(status)) return -1;
if(ntstatus_to_errno(NtClose(handle))) return -1;
//if(status < 0) return -1;
//if(NtClose(handle) < 0) return -1;
return 0;
}