Updated to fedora-glibc-20090407T0545
[glibc/history.git] / sysdeps / posix / pwritev.c
blob0b6627dc9ded12abed65a0589cfcfa0cb730bac6
1 /* Copyright (C) 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <stdbool.h>
25 #include <sys/param.h>
26 #if __WORDSIZE == 64 && !defined PWRITEV
27 /* Hide the pwritev64 declaration. */
28 # define pwritev64 __redirect_pwritev64
29 #endif
30 #include <sys/uio.h>
31 #include <bits/wordsize.h>
33 #ifndef PWRITEV
34 # define PWRITEV pwritev
35 # define PWRITE __pwrite
36 # define OFF_T off_t
37 #endif
40 static void
41 ifree (char **ptrp)
43 free (*ptrp);
47 /* Read data from file descriptor FD at the given position OFFSET
48 without change the file pointer, and put the result in the buffers
49 described by VECTOR, which is a vector of COUNT 'struct iovec's.
50 The buffers are filled in the order specified. Operates just like
51 'read' (see <unistd.h>) except that data are put in VECTOR instead
52 of a contiguous buffer. */
53 ssize_t
54 PWRITEV (int fd, const struct iovec *vector, int count, OFF_T offset)
56 /* Find the total number of bytes to be read. */
57 size_t bytes = 0;
58 for (int i = 0; i < count; ++i)
60 /* Check for ssize_t overflow. */
61 if (SSIZE_MAX - bytes < vector[i].iov_len)
63 __set_errno (EINVAL);
64 return -1;
66 bytes += vector[i].iov_len;
69 /* Allocate a temporary buffer to hold the data. We should normally
70 use alloca since it's faster and does not require synchronization
71 with other threads. But we cannot if the amount of memory
72 required is too large. */
73 char *buffer;
74 char *malloced_buffer __attribute__ ((__cleanup__ (ifree))) = NULL;
75 if (__libc_use_alloca (bytes))
76 buffer = (char *) __alloca (bytes);
77 else
79 malloced_buffer = buffer = (char *) malloc (bytes);
80 if (buffer == NULL)
81 return -1;
84 /* Read the data. */
85 ssize_t bytes_read = PWRITE (fd, buffer, bytes, offset);
86 if (bytes_read <= 0)
87 return -1;
89 /* Copy the data from BUFFER into the memory specified by VECTOR. */
90 bytes = bytes_read;
91 for (int i = 0; i < count; ++i)
93 size_t copy = MIN (vector[i].iov_len, bytes);
95 (void) memcpy ((void *) vector[i].iov_base, (void *) buffer, copy);
97 buffer += copy;
98 bytes -= copy;
99 if (bytes == 0)
100 break;
103 return bytes_read;
105 #if __WORDSIZE == 64 && defined pwritev64
106 # undef pwritev64
107 strong_alias (pwritev, pwritev64)
108 #endif