Update copyright for 2022
[pgsql.git] / src / port / pwrite.c
blobeeaffacc48af709e70809cd62f1c3483e397e90c
1 /*-------------------------------------------------------------------------
3 * pwrite.c
4 * Implementation of pwrite(2) for platforms that lack one.
6 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
8 * IDENTIFICATION
9 * src/port/pwrite.c
11 * Note that this implementation changes the current file position, unlike
12 * the POSIX function, so we use the name pg_pwrite().
14 *-------------------------------------------------------------------------
18 #include "postgres.h"
20 #ifdef WIN32
21 #include <windows.h>
22 #else
23 #include <unistd.h>
24 #endif
26 ssize_t
27 pg_pwrite(int fd, const void *buf, size_t size, off_t offset)
29 #ifdef WIN32
30 OVERLAPPED overlapped = {0};
31 HANDLE handle;
32 DWORD result;
34 handle = (HANDLE) _get_osfhandle(fd);
35 if (handle == INVALID_HANDLE_VALUE)
37 errno = EBADF;
38 return -1;
41 overlapped.Offset = offset;
42 if (!WriteFile(handle, buf, size, &result, &overlapped))
44 _dosmaperr(GetLastError());
45 return -1;
48 return result;
49 #else
50 if (lseek(fd, offset, SEEK_SET) < 0)
51 return -1;
53 return write(fd, buf, size);
54 #endif