blob: c071ab60d02a74ed7f00564f159f58b68a77f1c7 [file] [log] [blame] [raw]
/* A part of the Native C Library for Windows NT
Copyright 2007-2015 PC GO Ld.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#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.Length > sizeof(wchar_t) && old_path.Buffer[old_path.Length / sizeof(wchar_t) - 1] == L'/') {
old_path.Buffer[old_path.Length / sizeof(wchar_t) - 1] = 0;
old_path.Length -= sizeof(wchar_t);
old_path.MaximumLength -= sizeof(wchar_t);
}
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);
if(__set_errno_from_ntstatus(status)) return -1;
if(__set_errno_from_ntstatus(NtClose(handle))) return -1;
return 0;
}