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