Back to index

glibc  2.9
writev.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_writev (int fd, const struct iovec *vector, int count)

Function Documentation

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

Definition at line 34 of file writev.c.

  char *buffer;
  register char *bp;
  size_t bytes, to_copy;
  ssize_t bytes_written;
  int i;
  bool use_malloc = false;

  /* Find the total number of bytes to be written.  */
  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 writing
          the data in chunks.  Probably not so we just fail here.  */
       return -1;

      use_malloc = true;

  /* Copy the data into BUFFER.  */
  to_copy = bytes;
  bp = buffer;
  for (i = 0; i < count; ++i)
      size_t copy = MIN (vector[i].iov_len, to_copy);

      bp = __mempcpy ((void *) bp, (void *) vector[i].iov_base, copy);

      to_copy -= copy;
      if (to_copy == 0)

  bytes_written = __write (fd, buffer, bytes);

  if (use_malloc)
    free (buffer);

  return bytes_written;

Here is the call graph for this function: