| /* A part of the Native C Library for Windows NT |
| Copyright 2007-2015 PC GO Ld. |
| Copyright 2026 Rivoreo |
| |
| 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 <fcntl.h> |
| #include <errno.h> |
| #include <pathname.h> |
| |
| int symlinkat(const char *oldpath, int dir_fd, const char *newpath) { |
| if(dir_fd == -1) { |
| errno = EBADF; |
| return -1; |
| } |
| 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_attr = { |
| .Length = sizeof(OBJECT_ATTRIBUTES), .ObjectName = &new_path, .Attributes = OBJ_PERMANENT |
| }; |
| if(*newpath != '/') { |
| if(dir_fd == AT_FDCWD) { |
| RtlFreeUnicodeString(&new_path); |
| errno = EPERM; |
| return -1; |
| } |
| new_object_attr.RootDirectory = (void *)dir_fd; |
| } |
| long int status = NtCreateSymbolicLinkObject(&handle, SYMBOLIC_LINK_ALL_ACCESS, &new_object_attr, &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; |
| } |
| |
| int symlink(const char *oldpath, const char *newpath) { |
| return symlinkat(oldpath, AT_FDCWD, newpath); |
| } |