| /* 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 <unistd.h> |
| #include <errno.h> |
| #include <ntstatus.h> |
| |
| off_t lseek(int fd, off_t offset, int whence) { |
| if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { |
| int real_fd = (fd == STDOUT_FILENO ? _get_stdout_fd : _get_stderr_fd)(); |
| switch(real_fd) { |
| case TTY_FD_INVALID: |
| errno = EBADF; |
| return -1; |
| case TTY_FD_DEFAULT: |
| errno = ESPIPE; |
| return -1; |
| } |
| fd = real_fd; |
| } |
| |
| IO_STATUS_BLOCK io_status; |
| FILE_POSITION_INFORMATION position; |
| FILE_STANDARD_INFORMATION standard; |
| long int status = NtQueryInformationFile((void *)fd, &io_status, &position, sizeof position, FilePositionInformation); |
| if(status == STATUS_OBJECT_TYPE_MISMATCH) { |
| errno = ESPIPE; |
| return -1; |
| } |
| if(__set_errno_from_ntstatus(status)) return -1; |
| switch(whence) { |
| case SEEK_SET: |
| position.CurrentByteOffset.QuadPart = offset; |
| break; |
| case SEEK_CUR: |
| position.CurrentByteOffset.QuadPart += offset; |
| break; |
| case SEEK_END: |
| status = NtQueryInformationFile((void *)fd, &io_status, &standard, sizeof standard, FileStandardInformation); |
| if(__set_errno_from_ntstatus(status)) return -1; |
| position.CurrentByteOffset.QuadPart = standard.EndOfFile.QuadPart + offset; |
| break; |
| default: |
| errno = EINVAL; |
| return -1; |
| } |
| status = NtSetInformationFile((void *)fd, &io_status, &position, sizeof position, FilePositionInformation); |
| return __set_errno_from_ntstatus(status) ? -1 : position.CurrentByteOffset.QuadPart; |
| } |