maint: update almost all copyright ranges to include 2011
[gnulib.git] / lib / pwrite.c
1 /* Write block to given position in file without changing file pointer.
2    POSIX version.
3    Copyright (C) 1997-1999, 2002, 2011 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <unistd.h>
24
25 #include <errno.h>
26
27 #if HAVE_PWRITE
28
29 ssize_t
30 pwrite (int fd, const void *buf, size_t nbyte, off_t offset)
31 # undef pwrite
32 {
33   if (offset < 0)
34     {
35       errno = EINVAL;
36       return -1;
37     }
38   return pwrite (fd, buf, nbyte, offset);
39 }
40
41 #else
42
43 # define __libc_lseek(f,o,w) lseek (f, o, w)
44 # define __set_errno(Val) errno = (Val)
45 # define __libc_write(f,b,n) write (f, b, n)
46
47 /* Note: This implementation of pwrite is not multithread-safe.  */
48
49 ssize_t
50 pwrite (int fd, const void *buf, size_t nbyte, off_t offset)
51 {
52   /* Since we must not change the file pointer preserve the value so that
53      we can restore it later.  */
54   int save_errno;
55   ssize_t result;
56   off_t old_offset = __libc_lseek (fd, 0, SEEK_CUR);
57   if (old_offset == (off_t) -1)
58     return -1;
59
60   /* Set to wanted position.  */
61   if (__libc_lseek (fd, offset, SEEK_SET) == (off_t) -1)
62     return -1;
63
64   /* Write out the data.  */
65   result = __libc_write (fd, buf, nbyte);
66
67   /* Now we have to restore the position.  If this fails we have to
68      return this as an error.  But if the writing also failed we
69      return this error.  */
70   save_errno = errno;
71   if (__libc_lseek (fd, old_offset, SEEK_SET) == (off_t) -1)
72     {
73       if (result == -1)
74         __set_errno (save_errno);
75       return -1;
76     }
77   __set_errno (save_errno);
78
79   return result;
80 }
81
82 #endif