write()

NAME

write(), writev() - write output

SYNOPSIS

#include <unistd.h>

ssize_t write (int d, const void *buf, size_t nbytes)
#include <sys/uio.h>
ssize_t writev (int d, const struct iovec *iov, int iovcnt)

DESCRIPTION

The write(2) function attempts to write nbytes of data to the object referenced by the descriptor d from the buffer pointed to by buf. The function writev(2) performs the same action, but gathers the output data from the iovcnt buffers specified by the members of the iov array: iov[0], iov[1], ..., iov[iovcnt-1].

For writev(2), the iovec structure is defined as:

struct iovec {
	 void *iov_base;
	 size_t iov_len;
};

Each iovec entry specifies the base address and length of an area in memory from which data should be written. The writev(2) function will always write a complete area before proceeding to the next.

On objects capable of seeking, the write(2) starts at a position given by the pointer associated with d, (see lseek(2)). Upon return from write(2), the pointer is incremented by the number of bytes which were written.

Objects that are not capable of seeking always write from the current position. The value of the pointer associated with such an object is undefined.

When using non-blocking input/output (I/O) on objects such as sockets that are subject to flow control, write(2) and writev(2) can write fewer bytes than requested; the return value must be noted, and the remainder of the operation should be retried when possible.

RETURN VALUES

Upon successful completion the number of bytes which were written is returned. Otherwise a -1 is returned and the global variable errno is set to indicate the error.

When calling the write(2) function on a file that is not a regular file with nbytes set to zero, the function returns 0.

When the nbytes argument exceeds {SSIZE_MAX}, the return value is truncated to type ssize_t.

ERRORS

The write(2) and writev(2) functions will fail and the file pointer will remain unchanged if:

[EAGAIN]
The file was marked for non-blocking I/O, and no data could be written immediately.
[EBADF]
D is not a valid descriptor open for writing.
[EFAULT]
Part of iov or data to be written to the file points outside the process's allocated address space.
[EFBIG]
An attempt was made to write a file that exceeds the process's file size limit or the maximum file size.
[EINVAL]
The pointer associated with d was negative, or d is on a device that doesn't support the operation.
[EIO]
An I/O error occurred while reading from or writing to the file system.
[EPIPE]
An attempt is made to write to a socket of type SOCK_STREAM that is not connected to a peer socket.
[ENOSPC]
There is no free space remaining on the file system containing the file.
[EPIPE]
An attempt is made to write to a pipe that is not open for reading by any process.

In addition, writev(2) may return one of the following errors:

[EINVAL]
Iovcnt was less than or equal to 0, or greater than
[EINVAL]
One of the iov_len values in the iov array was negative.
[EINVAL]
The sum of the iov_len values in the iov array overflowed a 32-bit integer.

SEE ALSO

fcntl(2)

lseek(2)

open(2)

pipe(2)

select(2)