annotate lib/pwrite.c @ 13479:8a0b8adde2be

Use spaces for indentation, not tabs.
author Bruno Haible <bruno@clisp.org>
date Wed, 28 Jul 2010 10:08:37 +0200
parents d44fde85189e
children 081ce9a593c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13314
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
1 /* Write block to given position in file without changing file pointer.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
2 POSIX version.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
3 Copyright (C) 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
4 This file is part of the GNU C Library.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
6
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
7 This program is free software: you can redistribute it and/or modify
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
9 the Free Software Foundation; either version 3 of the License, or
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
10 (at your option) any later version.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
11
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
15 GNU General Public License for more details.
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
16
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
19
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
20 #include <config.h>
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
21
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
22 /* Specification. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
23 #include <unistd.h>
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
24
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
25 #include <errno.h>
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
26
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
27 #define __libc_lseek(f,o,w) lseek (f, o, w)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
28 #define __set_errno(Val) errno = (Val)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
29 #define __libc_write(f,b,n) write (f, b, n)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
30
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
31 /* Note: This implementation of pwrite is not multithread-safe. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
32
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
33 ssize_t
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
34 pwrite (int fd, const void *buf, size_t nbyte, off_t offset)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
35 {
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
36 /* Since we must not change the file pointer preserve the value so that
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
37 we can restore it later. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
38 int save_errno;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
39 ssize_t result;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
40 off_t old_offset = __libc_lseek (fd, 0, SEEK_CUR);
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
41 if (old_offset == (off_t) -1)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
42 return -1;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
43
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
44 /* Set to wanted position. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
45 if (__libc_lseek (fd, offset, SEEK_SET) == (off_t) -1)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
46 return -1;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
47
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
48 /* Write out the data. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
49 result = __libc_write (fd, buf, nbyte);
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
50
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
51 /* Now we have to restore the position. If this fails we have to
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
52 return this as an error. But if the writing also failed we
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
53 return this error. */
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
54 save_errno = errno;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
55 if (__libc_lseek (fd, old_offset, SEEK_SET) == (off_t) -1)
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
56 {
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
57 if (result == -1)
13479
8a0b8adde2be Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 13314
diff changeset
58 __set_errno (save_errno);
13314
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
59 return -1;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
60 }
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
61 __set_errno (save_errno);
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
62
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
63 return result;
d44fde85189e New module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
64 }