1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2020 Todd C. Miller <Todd.Miller@sudo.ws>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /*
20 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22 */
23
24 #include <config.h>
25
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include "sudo_compat.h"
30
31 #if !defined(HAVE_PWRITE) && !defined(HAVE_PWRITE64)
32 ssize_t
sudo_pwrite(int fd,const void * buf,size_t nbytes,off_t offset)33 sudo_pwrite(int fd, const void *buf, size_t nbytes, off_t offset)
34 {
35 ssize_t nwritten;
36 off_t old_offset;
37
38 old_offset = lseek(fd, (off_t)0, SEEK_CUR);
39 if (old_offset == -1 || lseek(fd, offset, SEEK_SET) == -1)
40 return -1;
41
42 nwritten = write(fd, buf, nbytes);
43 if (lseek(fd, old_offset, SEEK_SET) == -1)
44 return -1;
45
46 return nwritten;
47 }
48 #endif /* HAVE_PWRITE && !HAVE_PWRITE64 */
49