Back to index

glibc  2.9
readv.c File Reference
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <errno.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


ssize_t __libc_readv (int fd, const struct iovec *vector, int count)

Function Documentation

ssize_t __libc_readv ( int  fd,
const struct iovec vector,
int  count 

Definition at line 34 of file readv.c.

  char *buffer;
  char *buffer_start;
  size_t bytes;
  ssize_t bytes_read;
  int i;
  bool use_malloc = false;

  /* Find the total number of bytes to be read.  */
  bytes = 0;
  for (i = 0; i < count; ++i)
      /* Check for ssize_t overflow.  */
      if (SSIZE_MAX - bytes < vector[i].iov_len)
         __set_errno (EINVAL);
         return -1;
      bytes += vector[i].iov_len;

  /* Allocate a temporary buffer to hold the data.  We should normally
     use alloca since it's faster and does not require synchronization
     with other threads.  But we cannot if the amount of memory
     required is too large.  */
  if (__libc_use_alloca (bytes))
    buffer = (char *) __alloca (bytes);
      buffer = (char *) malloc (bytes);
      if (buffer == NULL)
       /* XXX I don't know whether it is acceptable to try reading
          the data in chunks.  Probably not so we just fail here.  */
       return -1;

      use_malloc = true;

  /* Read the data.  */
  bytes_read = __read (fd, buffer, bytes);
  if (bytes_read <= 0)
    return -1;

  /* Copy the data from BUFFER into the memory specified by VECTOR.  */
  bytes = bytes_read;
  buffer_start = buffer;
  for (i = 0; i < count; ++i)
      size_t copy = MIN (vector[i].iov_len, bytes);

      (void) memcpy ((void *) vector[i].iov_base, (void *) buffer, copy);

      buffer += copy;
      bytes -= copy;
      if (bytes == 0)

  if (use_malloc)
    free (buffer_start);

  return bytes_read;

Here is the call graph for this function: