uio.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef HAVE_SYS_UIO_H
  17. #include <cutils/uio.h>
  18. #include <unistd.h>
  19. int readv( int fd, struct iovec* vecs, int count )
  20. {
  21. int total = 0;
  22. for ( ; count > 0; count--, vecs++ ) {
  23. const char* buf = vecs->iov_base;
  24. int len = vecs->iov_len;
  25. while (len > 0) {
  26. int ret = read( fd, buf, len );
  27. if (ret < 0) {
  28. if (total == 0)
  29. total = -1;
  30. goto Exit;
  31. }
  32. if (ret == 0)
  33. goto Exit;
  34. total += ret;
  35. buf += ret;
  36. len -= ret;
  37. }
  38. }
  39. Exit:
  40. return total;
  41. }
  42. int writev( int fd, const struct iovec* vecs, int count )
  43. {
  44. int total = 0;
  45. for ( ; count > 0; count--, vecs++ ) {
  46. const char* buf = (const char*)vecs->iov_base;
  47. int len = (int)vecs->iov_len;
  48. while (len > 0) {
  49. int ret = write( fd, buf, len );
  50. if (ret < 0) {
  51. if (total == 0)
  52. total = -1;
  53. goto Exit;
  54. }
  55. if (ret == 0)
  56. goto Exit;
  57. total += ret;
  58. buf += ret;
  59. len -= ret;
  60. }
  61. }
  62. Exit:
  63. return total;
  64. }
  65. #endif /* !HAVE_SYS_UIO_H */