uio.c revision dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0
1/*
2 * Copyright (C) 2007 The Android Open Source Project
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 <cutils/uio.h>
20#include <unistd.h>
21
22int  readv( int  fd, struct iovec*  vecs, int  count )
23{
24    int   total = 0;
25
26    for ( ; count > 0; count--, vecs++ ) {
27        const char*  buf = vecs->iov_base;
28        int          len = vecs->iov_len;
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    }
45Exit:
46    return total;
47}
48
49int  writev( int  fd, const struct iovec*  vecs, int  count )
50{
51    int   total = 0;
52
53    for ( ; count > 0; count--, vecs++ ) {
54        const char*  buf = (const char*)vecs->iov_base;
55        int          len = (int)vecs->iov_len;
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    }
72Exit:
73    return total;
74}
75
76#endif /* !HAVE_SYS_UIO_H */
77