source: rtems/cpukit/libcsupport/src/writev.c @ fd2b1634

4.115
Last change on this file since fd2b1634 was fd2b1634, checked in by Sebastian Huber <sebastian.huber@…>, on 07/15/10 at 08:10:48

2010-07-16 Sebastian Huber <sebastian.huber@…>

  • libcsupport/include/rtems/libio.h: Removed file_info and handlers fields in rtems_libio_t.
  • libcsupport/src/close.c, libcsupport/src/fcntl.c, libcsupport/src/fdatasync.c, libcsupport/src/fstat.c, libcsupport/src/fsync.c, libcsupport/src/ftruncate.c, libcsupport/src/getdents.c, libcsupport/src/ioctl.c, libcsupport/src/libio_sockets.c, libcsupport/src/lseek.c, libcsupport/src/open.c, libcsupport/src/read.c, libcsupport/src/readv.c, libcsupport/src/write.c, libcsupport/src/writev.c, libfs/src/devfs/devclose.c, libfs/src/devfs/devioctl.c, libfs/src/devfs/devopen.c, libfs/src/devfs/devread.c, libfs/src/devfs/devwrite.c libfs/src/dosfs/msdos_dir.c libfs/src/dosfs/msdos_file.c libfs/src/imfs/deviceio.c libfs/src/imfs/imfs_directory.c libfs/src/imfs/imfs_fifo.c libfs/src/imfs/memfile.c libfs/src/nfsclient/src/nfs.c libfs/src/rfs/rtems-rfs-rtems-file.c libfs/src/rfs/rtems-rfs-rtems.h libnetworking/lib/ftpfs.c: Reflect changes above.
  • 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->pathinfo.handlers->write_h)(
108      iop,
109      iov[v].iov_base,
110      iov[v].iov_len
111    );
112
113    if ( bytes < 0 )
114      return -1;
115
116    if ( bytes > 0 ) {
117      iop->offset += bytes;
118      total       += bytes;
119    }
120
121    if (bytes != iov[ v ].iov_len)
122      break;
123  }
124
125  return total;
126}
127
Note: See TracBrowser for help on using the repository browser.