source: rtems/cpukit/libcsupport/src/writev.c @ 0d923d9

4.115
Last change on this file since 0d923d9 was 0d923d9, checked in by Joel Sherrill <joel.sherrill@…>, on 07/01/10 at 17:47:48

2010-07-01 Joel Sherrill <joel.sherrill@…>

  • libcsupport/src/_rename_r.c, libcsupport/src/getdents.c, libcsupport/src/unlink.c, libcsupport/src/utime.c, libcsupport/src/writev.c: Remove remaining checks for missing handlers.
  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 *  writev() - POSIX 1003.1 - Read a Vector
3 *
4 *  OpenGroup URL:
5 *
6 *  http://www.opengroup.org/onlinepubs/009695399/functions/writev.html
7 *
8 *  COPYRIGHT (c) 1989-2007.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.com/license/LICENSE.
14 *
15 *  $Id$
16 */
17
18#if HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <sys/types.h>
23#include <sys/uio.h>
24
25#include <rtems/libio_.h>
26#include <rtems/seterr.h>
27
28ssize_t writev(
29  int                 fd,
30  const struct iovec *iov,
31  int                 iovcnt
32)
33{
34  ssize_t        total;
35  int            v;
36  int            bytes;
37  rtems_libio_t *iop;
38  ssize_t        old;
39  bool           all_zeros;
40
41  rtems_libio_check_fd( fd );
42  iop = rtems_libio_iop( fd );
43  rtems_libio_check_is_open( iop );
44  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
45
46  /*
47   *  Argument validation on IO vector
48   */
49  if ( !iov )
50    rtems_set_errno_and_return_minus_one( EINVAL );
51
52  if ( iovcnt <= 0 )
53    rtems_set_errno_and_return_minus_one( EINVAL );
54
55  if ( iovcnt > IOV_MAX )
56    rtems_set_errno_and_return_minus_one( EINVAL );
57
58  /*
59   *  OpenGroup says that you are supposed to return EINVAL if the
60   *  sum of the iov_len values in the iov array would overflow a
61   *  ssize_t.
62   *
63   *  Also we would like to ensure that no IO is performed if there
64   *  are obvious errors in the iovec.  So this extra loop ensures
65   *  that we do not do anything if there is an argument error.
66   *
67   *  In addition,the OpenGroup specification says that if all the
68   *  iov_len entries are zero, then the call has no effect.  So
69   *  this loop does that check as well and sets "all-zero" appropriately.
70   *  The variable "all_zero" is used as an early exit point before
71   *  entering the write loop.
72   */
73  all_zeros = true;
74  for ( old=0, total=0, v=0 ; v < iovcnt ; v++ ) {
75
76    if ( !iov[v].iov_base )
77      rtems_set_errno_and_return_minus_one( EINVAL );
78
79    if ( iov[v].iov_len < 0 )
80      rtems_set_errno_and_return_minus_one( EINVAL );
81
82    if ( iov[v].iov_len )
83      all_zeros = false;
84
85    /* check for wrap */
86    old    = total;
87    total += iov[v].iov_len;
88    if ( total < old || total > SSIZE_MAX )
89      rtems_set_errno_and_return_minus_one( EINVAL );
90  }
91
92  /*
93   * A writev with all zeros is supposed to have no effect per OpenGroup.
94   */
95  if ( all_zeros == true ) {
96    return 0;
97  }
98
99  /*
100   *  Now process the writev().
101   */
102  for ( total=0, v=0 ; v < iovcnt ; v++ ) {
103    /* all zero lengths has no effect */
104    if ( iov[v].iov_len == 0 )
105      continue;
106
107    bytes = (*iop->handlers->write_h)( iop, iov[v].iov_base, iov[v].iov_len );
108
109    if ( bytes < 0 )
110      return -1;
111
112    if ( bytes > 0 ) {
113      iop->offset += bytes;
114      total       += bytes;
115    }
116
117    if (bytes != iov[ v ].iov_len)
118      break;
119  }
120
121  return total;
122}
123
Note: See TracBrowser for help on using the repository browser.