Colin Cross | e355ded | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 023eabf | 2014-01-06 08:14:11 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2007-2014 The Android Open Source Project |
Colin Cross | e355ded | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef HAVE_SYS_UIO_H |
| 18 | |
| 19 | #include <log/uio.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | int readv( int fd, struct iovec* vecs, int count ) |
| 23 | { |
| 24 | int total = 0; |
| 25 | |
| 26 | for ( ; count > 0; count--, vecs++ ) { |
Mark Salyzyn | 023eabf | 2014-01-06 08:14:11 -0800 | [diff] [blame^] | 27 | char* buf = vecs->iov_base; |
| 28 | int len = vecs->iov_len; |
Colin Cross | e355ded | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 29 | |
| 30 | while (len > 0) { |
| 31 | int ret = read( fd, buf, len ); |
| 32 | if (ret < 0) { |
| 33 | if (total == 0) |
| 34 | total = -1; |
| 35 | goto Exit; |
| 36 | } |
| 37 | if (ret == 0) |
| 38 | goto Exit; |
| 39 | |
| 40 | total += ret; |
| 41 | buf += ret; |
| 42 | len -= ret; |
| 43 | } |
| 44 | } |
| 45 | Exit: |
| 46 | return total; |
| 47 | } |
| 48 | |
| 49 | int writev( int fd, const struct iovec* vecs, int count ) |
| 50 | { |
| 51 | int total = 0; |
| 52 | |
| 53 | for ( ; count > 0; count--, vecs++ ) { |
Mark Salyzyn | 023eabf | 2014-01-06 08:14:11 -0800 | [diff] [blame^] | 54 | const char* buf = vecs->iov_base; |
| 55 | int len = vecs->iov_len; |
Colin Cross | e355ded | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 56 | |
| 57 | while (len > 0) { |
| 58 | int ret = write( fd, buf, len ); |
| 59 | if (ret < 0) { |
| 60 | if (total == 0) |
| 61 | total = -1; |
| 62 | goto Exit; |
| 63 | } |
| 64 | if (ret == 0) |
| 65 | goto Exit; |
| 66 | |
| 67 | total += ret; |
| 68 | buf += ret; |
| 69 | len -= ret; |
| 70 | } |
| 71 | } |
| 72 | Exit: |
| 73 | return total; |
| 74 | } |
| 75 | |
| 76 | #endif /* !HAVE_SYS_UIO_H */ |