write, writev -- write to a file


cc . . . -lc
#include <unistd.h>

ssize_t write(int fildes, const void *buf, size_t nbyte);

#include <sys/types.h> #include <sys/uio.h>

ssize_t writev(int fildes, const struct iovec *iov, int iovcnt);


write- write to a file

writev- write to a file using multiple buffers

write attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with fildes. If nbyte is 0 and the file is a regular file, write returns 0 and has no other results. If the value of nbyte is greater than {SSIZE_MAX} the result is undefined. fildes is a file descriptor obtained from a creat(S), open(S), dup(S), fcntl(S), pipe(S), or ioctl(S) system call.

writev performs the same action as write, but gathers the output data from the iovcnt buffers specified by the members of the iov array: iov[0], iov[1], ... iov[iovcnt-1]. The iovcnt is valid only if greater than 0 and less than or equal to {IOV_MAX}.

For writev, the iovec structure contains the following members:

   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. writev always writes a complete area before proceeding to the next.

On devices capable of seeking, the writing of data proceeds from the position in the file indicated by the file pointer. On return from write, the file pointer is incremented by the number of bytes actually written. On a regular file, if the incremented file pointer is greater than the length of the file, the length of the file is set to the new file pointer.

On devices incapable of seeking, writing always takes place starting at the current position. The value of a file pointer associated with such a device is undefined.

If the O_APPEND flag of the file status flags is set, the file pointer is set to the end of the file before each write.

For regular files, if the O_SYNC flag of the file status flags is set, write does not return until both the file data and file status are delivered to the underlying hardware. This function is for special applications that require extra reliability at the cost of performance. For block special files, if O_SYNC is set, write does not return until the data has been delivered to the underlying hardware.

A write to a regular file is blocked if mandatory file/record locking is set (see chmod(S)) and there is a record lock owned by another process on the segment of the file to be written:

Note that O_NDELAY is only provided for backward compatibility and N_NONBLOCK should be used for new applications.

If a write requests that more bytes be written than there is room for -- for example, if the write would exceed the process file size limit (see getrlimit(S) and ulimit(S)) the system file size limit, or the free space on the device -- only as many bytes as there is room for will be written. For example, suppose there is space for 20 bytes more in a file before reaching a limit. A write of 512-bytes returns 20. The next write of a non-zero number of bytes gives a failure return (except as noted for pipes and FIFO below).

Write requests to a pipe or FIFO are handled the same as a regular file with the following exceptions:

When attempting to write to a file descriptor (other than a pipe or FIFO) that supports nonblocking writes and cannot accept the data immediately:

For STREAMS files (see Intro(S)) the operation of write is determined by the values of the minimum and maximum nbyte range (``packet size'') accepted by the stream. These values are contained in the topmost stream module. Unless the user pushes the topmost module (see I_PUSH in streamio(M)), these values cannot be set or tested from user level. If nbyte falls within the packet size range, nbyte bytes are written. If nbyte does not fall within the range and the minimum packet size value is 0, write breaks the buffer into maximum packet size segments prior to sending the data downstream (the last segment may be smaller than the maximum packet size). If nbyte does not fall within the range and the minimum value is non-zero, write fails and sets errno to ERANGE. Writing a zero-length buffer (nbyte is 0) to a STREAMS device sends a zero-length message with 0 returned. However, writing a zero-length buffer to a pipe or FIFO sends no message and 0 is returned. The user program may issue the I_SWROPT ioctl to enable zero-length messages to be sent across the pipe or FIFO (see streamio(M)).

When writing to a stream, data messages are created with a priority band of 0. When writing to a stream that is not a pipe or FIFO:

Return values

On success, write and writev return the number of bytes written and mark for update the st_ctime and st_mtime fields of the file. On failure, write and writev return -1 and set errno to identify the error.


In the following conditions, write and writev fail and set errno to:

Mandatory file/record locking is set, O_NDELAY or O_NONBLOCK is set, and there is a blocking record lock.

Total amount of system memory available when reading via raw I/O is temporarily insufficient.

An attempt is made to write to a stream that cannot accept data with the O_NDELAY or O_NONBLOCK flag set.

If a write to a pipe or FIFO of {PIPE_BUF} bytes or less is requested and less than nbytes of free space is available.

fildes is not a valid file descriptor open for writing.

The write was going to go to sleep and cause a deadlock to occur.

buf points outside the process's allocated address space.

An attempt is made to write a file that exceeds the process's file size limit or the maximum file size (see getrlimit(S) and ulimit(S)).

A signal was caught during the write system call.

[EINTR] and [SIGXFSZ] signal
A write was attempted that would exceed the maximum file size limit.

An attempt is made to write to a stream linked below a multiplexor.

The process is in the background and is attempting to write to its controlling terminal whose TOSTOP flag is set; the process is neither ignoring nor blocking SIGTTOU signals, and the process group of the process is orphaned.

The system record lock table was full, so the write could not go to sleep until the blocking record lock was removed.

fildes is on a remote machine and the link to that machine is no longer active.

An attempt is made to write to a stream with insufficient STREAMS memory resources available in the system.

During a write to an ordinary file, there is no free space left on the device.

The device associated with the file descriptor is a block-special or character-special file and the file-pointer value is out of range.

[EPIPE] and [SIGPIPE] signal
An attempt is made to write to a pipe that is not open for reading by any process.

An attempt is made to write to a FIFO that is not open for reading by any process.

An attempt is made to write to a pipe that has only one end open.

An attempt is made to write to a stream with nbyte outside specified minimum and maximum write range, and the minimum value is non-zero.

Enforced record locking was enabled and {LOCK_MAX} regions are already locked in the system.
In addition, in the following conditions writev fails and sets errno to:

iovcnt was less than or equal to 0, or greater than {IOV_MAX}.

An iov_len value in the iov array was negative.

The sum of the iov_len values in the iov array overflowed a 32-bit integer.
A write to a STREAMS file can fail if an error message has been received at the stream head. In this case, errno is set to the value included in the error message.

After carrier loss, M_HANGUP is set, and a subsequent write will return -1 with errno set to EIO. To write after disconnecting and reconnecting the line, set the CLOCAL flag to tell the driver to ignore the state of the line and the driver will not send M_HANGUP to the stream head. If CLOCAL is not set, and hangup occurs, the application is responsible for re-establishing the connection.


linking library

See also

creat(S), dup(S), fcntl(S), getrlimit(S), Intro(S), lseek(S), open(S), pipe(S), read(S), types(FP), ulimit(S)

Standards conformance

write is conformant with:

AT&T SVID Issue 3;
X/Open CAE Specification C202, System Interfaces and Headers, Issue 4, 1992;
Intel386 Binary Compatibility Specification, Edition 2 (iBCSe2) ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .

writev is conformant with:

AT&T SVID Issue 3,
X/Open CAE Specification C202, System Interfaces and Headers, Issue 4, 1992

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003