1*c8a0e2f4SThomas Veerman /* $NetBSD: pwrite.c,v 1.4 2008/04/28 20:24:12 martin Exp $ */
2*c8a0e2f4SThomas Veerman
3*c8a0e2f4SThomas Veerman /*-
4*c8a0e2f4SThomas Veerman * Copyright (c) 2001 The NetBSD Foundation, Inc.
5*c8a0e2f4SThomas Veerman * All rights reserved.
6*c8a0e2f4SThomas Veerman *
7*c8a0e2f4SThomas Veerman * This code is derived from software contributed to The NetBSD Foundation
8*c8a0e2f4SThomas Veerman * by Todd Vierling.
9*c8a0e2f4SThomas Veerman *
10*c8a0e2f4SThomas Veerman * Redistribution and use in source and binary forms, with or without
11*c8a0e2f4SThomas Veerman * modification, are permitted provided that the following conditions
12*c8a0e2f4SThomas Veerman * are met:
13*c8a0e2f4SThomas Veerman * 1. Redistributions of source code must retain the above copyright
14*c8a0e2f4SThomas Veerman * notice, this list of conditions and the following disclaimer.
15*c8a0e2f4SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
16*c8a0e2f4SThomas Veerman * notice, this list of conditions and the following disclaimer in the
17*c8a0e2f4SThomas Veerman * documentation and/or other materials provided with the distribution.
18*c8a0e2f4SThomas Veerman *
19*c8a0e2f4SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*c8a0e2f4SThomas Veerman * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*c8a0e2f4SThomas Veerman * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*c8a0e2f4SThomas Veerman * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*c8a0e2f4SThomas Veerman * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*c8a0e2f4SThomas Veerman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*c8a0e2f4SThomas Veerman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*c8a0e2f4SThomas Veerman * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*c8a0e2f4SThomas Veerman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*c8a0e2f4SThomas Veerman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*c8a0e2f4SThomas Veerman * POSSIBILITY OF SUCH DAMAGE.
30*c8a0e2f4SThomas Veerman */
31*c8a0e2f4SThomas Veerman
32*c8a0e2f4SThomas Veerman /* Emulate pwrite() with lseek()/write()/lseek(). Not thread-safe, of course. */
33*c8a0e2f4SThomas Veerman
34*c8a0e2f4SThomas Veerman #include "nbtool_config.h"
35*c8a0e2f4SThomas Veerman
36*c8a0e2f4SThomas Veerman #if !HAVE_PWRITE
37*c8a0e2f4SThomas Veerman #include <unistd.h>
38*c8a0e2f4SThomas Veerman
pwrite(int d,const void * buf,size_t nbytes,off_t offset)39*c8a0e2f4SThomas Veerman ssize_t pwrite(int d, const void *buf, size_t nbytes, off_t offset) {
40*c8a0e2f4SThomas Veerman off_t oldoff = lseek(d, offset, SEEK_SET);
41*c8a0e2f4SThomas Veerman int olderrno;
42*c8a0e2f4SThomas Veerman ssize_t nw;
43*c8a0e2f4SThomas Veerman
44*c8a0e2f4SThomas Veerman if (oldoff < 0)
45*c8a0e2f4SThomas Veerman return -1;
46*c8a0e2f4SThomas Veerman
47*c8a0e2f4SThomas Veerman nw = write(d, buf, nbytes);
48*c8a0e2f4SThomas Veerman
49*c8a0e2f4SThomas Veerman olderrno = errno;
50*c8a0e2f4SThomas Veerman lseek(d, oldoff, SEEK_SET);
51*c8a0e2f4SThomas Veerman errno = olderrno;
52*c8a0e2f4SThomas Veerman
53*c8a0e2f4SThomas Veerman return nw;
54*c8a0e2f4SThomas Veerman }
55*c8a0e2f4SThomas Veerman #endif
56