]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 2009-2015 Free Software Foundation, Inc. |
e109c612 UD |
2 | This file is part of the GNU C Library. |
3 | ||
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. | |
8 | ||
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. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
e109c612 UD |
17 | |
18 | #include <errno.h> | |
19 | #include <stdlib.h> | |
20 | #include <unistd.h> | |
21 | #include <string.h> | |
22 | #include <limits.h> | |
23 | #include <stdbool.h> | |
24 | #include <sys/param.h> | |
25 | #if __WORDSIZE == 64 && !defined PWRITEV | |
26 | /* Hide the pwritev64 declaration. */ | |
27 | # define pwritev64 __redirect_pwritev64 | |
28 | #endif | |
29 | #include <sys/uio.h> | |
30 | #include <bits/wordsize.h> | |
31 | ||
32 | #ifndef PWRITEV | |
33 | # define PWRITEV pwritev | |
34 | # define PWRITE __pwrite | |
35 | # define OFF_T off_t | |
36 | #endif | |
37 | ||
38 | ||
39 | static void | |
40 | ifree (char **ptrp) | |
41 | { | |
42 | free (*ptrp); | |
43 | } | |
44 | ||
45 | ||
7f8a28ef UD |
46 | /* Write data pointed by the buffers described by IOVEC, which is a |
47 | vector of COUNT 'struct iovec's, to file descriptor FD at the given | |
48 | position OFFSET without change the file pointer. The data is | |
49 | written in the order specified. Operates just like 'write' (see | |
50 | <unistd.h>) except that the data are taken from IOVEC instead of a | |
51 | contiguous buffer. */ | |
e109c612 UD |
52 | ssize_t |
53 | PWRITEV (int fd, const struct iovec *vector, int count, OFF_T offset) | |
54 | { | |
55 | /* Find the total number of bytes to be read. */ | |
56 | size_t bytes = 0; | |
57 | for (int i = 0; i < count; ++i) | |
58 | { | |
59 | /* Check for ssize_t overflow. */ | |
60 | if (SSIZE_MAX - bytes < vector[i].iov_len) | |
61 | { | |
62 | __set_errno (EINVAL); | |
63 | return -1; | |
64 | } | |
65 | bytes += vector[i].iov_len; | |
66 | } | |
67 | ||
68 | /* Allocate a temporary buffer to hold the data. We should normally | |
69 | use alloca since it's faster and does not require synchronization | |
70 | with other threads. But we cannot if the amount of memory | |
71 | required is too large. */ | |
72 | char *buffer; | |
73 | char *malloced_buffer __attribute__ ((__cleanup__ (ifree))) = NULL; | |
74 | if (__libc_use_alloca (bytes)) | |
75 | buffer = (char *) __alloca (bytes); | |
76 | else | |
77 | { | |
78 | malloced_buffer = buffer = (char *) malloc (bytes); | |
79 | if (buffer == NULL) | |
80 | return -1; | |
81 | } | |
82 | ||
e109c612 | 83 | /* Copy the data from BUFFER into the memory specified by VECTOR. */ |
7f8a28ef | 84 | char *ptr = buffer; |
e109c612 | 85 | for (int i = 0; i < count; ++i) |
7f8a28ef UD |
86 | ptr = __mempcpy ((void *) ptr, (void *) vector[i].iov_base, |
87 | vector[i].iov_len); | |
e109c612 | 88 | |
7f8a28ef UD |
89 | /* Write the data. */ |
90 | return PWRITE (fd, buffer, bytes, offset); | |
e109c612 UD |
91 | } |
92 | #if __WORDSIZE == 64 && defined pwritev64 | |
93 | # undef pwritev64 | |
94 | strong_alias (pwritev, pwritev64) | |
95 | #endif |