The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

write, writev, pwrite - write on a file

 SYNOPSIS



#include <unistd.h>

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

#include <sys/uio.h>

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

 DESCRIPTION

The write() function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes.

If nbyte is 0, write() will return 0 and have no other results if the file is a regular file; otherwise, the results are unspecified.

On a regular file or other file capable of seeking, the actual writing of data proceeds from the position in the file indicated by the file offset associated with fildes. Before successful return from write(), the file offset is incremented by the number of bytes actually written. On a regular file, if this incremented file offset is greater than the length of the file, the length of the file will be set to this file offset.

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

If the O_APPEND flag of the file status flags is set, the file offset will be set to the end of the file prior to each write and no intervening file modification operation will occur between changing the file offset and the write operation.

If a write() requests that more bytes be written than there is room for (for example, the ulimit or the physical end of a medium), 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 will return 20. The next write of a non-zero number of bytes will give a failure return (except as noted below)  and the implementation will generate a SIGXFSZ signal for the thread.

If write() is interrupted by a signal before it writes any data, it will return -1 with errno set to [EINTR].

If write() is interrupted by a signal after it successfully writes some data, it will return the number of bytes written.

If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-dependent.

After a write() to a regular file has successfully returned:

Write requests to a pipe or FIFO will be 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 non-blocking writes and cannot accept the data immediately:

Upon successful completion, where nbyte is greater than 0, write() will mark for update the st_ctime and st_mtime fields of the file, and if the file is a regular file, the S_ISUID and S_ISGID bits of the file mode may be cleared.

If fildes refers to a STREAM, 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 determined by the topmost STREAM module. If nbyte falls within the packet size range, nbyte bytes will be written. If nbyte does not fall within the range and the minimum packet size value is 0, write() will break the buffer into maximum packet size segments prior to sending the data downstream (the last segment may contain less than the maximum packet size). If nbyte does not fall within the range and the minimum value is non-zero, write() will fail with errno set to [ERANGE]. Writing a zero-length buffer (nbyte is 0) to a STREAMS device sends 0 bytes with 0 returned. However, writing a zero-length buffer to a STREAMS-based pipe or FIFO sends no message and 0 is returned. The process may issue I_SWROPT ioctl() to enable zero-length messages to be sent across the pipe or FIFO.

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:

In addition, write() and writev() will fail if the STREAM head had processed an asynchronous error before the call. In this case, the value of errno does not reflect the result of write() or writev() but reflects the prior error.

The writev() function is equivalent to 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]. iovcnt is valid if greater than 0 and less than or equal to {IOV_MAX}, defined in <limits.h>.

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

If fildes refers to a regular file and all of the iov_len members in the array pointed to by iov are 0, writev() will return 0 and have no other effect. For other file types, the behaviour is unspecified.

If the sum of the iov_len values is greater than SSIZE_MAX, the operation fails and no data is transferred.

If the Synchronized Input and Output option is supported:

If the O_DSYNC bit has been set, write I/O operations on the file descriptor complete as defined by synchronised I/O data integrity completion.

If the O_SYNC bit has been set, write I/O operations on the file descriptor complete as defined by synchronised I/O file integrity completion.

If the Shared Memory Objects option is supported:

If fildes refers to a shared memory object, the result of the write() function is unspecified.

For regular files, no data transfer will occur past the offset maximum established in the open file description associated with fildes.

The pwrite() function performs the same action as write(), except that it writes into a given position without changing the file pointer. The first three arguments to pwrite() are the same as write() with the addition of a fourth argument offset for the desired position inside the file.

 RETURN VALUE

Upon successful completion, write() and pwrite() will return the number of bytes actually written to the file associated with fildes. This number will never be greater than nbyte. Otherwise, -1 is returned and errno is set to indicate the error.

Upon successful completion, writev() returns the number of bytes actually written. Otherwise, it returns a value of -1, the file-pointer remains unchanged, and errno is set to indicate an error.

 ERRORS

The write(), writev() and pwrite() functions will fail if:
[EAGAIN]
The O_NONBLOCK flag is set for the file descriptor and the thread would be delayed in the write() operation.
[EBADF]
The fildes argument is not a valid file descriptor open for writing.
[EFBIG]
An attempt was made to write a file that exceeds the implementation-dependent maximum file size  or the process' file size limit.
[EFBIG]
The file is a regular file, nbyte is greater than 0 and the starting position is greater than or equal to the offset maximum established in the open file description associated with fildes.
[EINTR]
The write operation was terminated due to the receipt of a signal, and no data was transferred.
[EIO]
A physical I/O error has occurred.
[EIO]
The process is a member of a background process group attempting to write to its controlling terminal, TOSTOP is set, the process is neither ignoring nor blocking SIGTTOU and the process group of the process is orphaned. This error may also be returned under implementation-dependent conditions.
[ENOSPC]
There was no free space remaining on the device containing the file.
[EPIPE]
An attempt is made to write to a pipe or FIFO that is not open for reading by any process, or that only has one end open. A SIGPIPE signal will also be sent to the thread.
[ERANGE]
The transfer request size was outside the range supported by the STREAMS file associated with fildes.

The writev() function will fail if:

[EINVAL]
The sum of the iov_len values in the iov array would overflow an ssize_t.

The write(), writev() and pwrite() functions may fail if:

[EINVAL]
The STREAM or multiplexer referenced by fildes is linked (directly or indirectly) downstream from a multiplexer.
[ENXIO]
A request was made of a non-existent device, or the request was outside the capabilities of the device.
[ENXIO]
A hangup occurred on the STREAM being written to.

A write to a STREAMS file may 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.

The writev() function may fail and set errno to:

[EINVAL]
The iovcnt argument was less than or equal to 0, or greater than {IOV_MAX}.

The pwrite() function fails and the file pointer remains unchanged if:

[EINVAL]
The offset argument is invalid. The value is negative.
[ESPIPE]
fildes is associated with a pipe or FIFO.

 EXAMPLES

None.

 APPLICATION USAGE

None.

 FUTURE DIRECTIONS

None.

 SEE ALSO

chmod(), creat(), dup(), fcntl(), getrlimit(), lseek(), open(), pipe(), ulimit(), <limits.h>, <stropts.h>, <sys/uio.h>, <unistd.h>.

DERIVATION

Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]