NAME | SYNOPSIS | DESCRIPTION
#include <unistd.h>ssize_t write(int fildes, const void * buf, size_t nbyte);
#include <sys/uio.h>ssize_t writev(int fildes, const struct iovec * iov, int iovcnt);
#include <tsol/rdwrl.h>ssize_t writel(int fildes, void * buf, size_t nbyte, bclabel_t * label_p);
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.
If the O_SYNC flag of the file status flags is set and fildes refers to a regular file, a successful write() does not return until the data is delivered to the underlying hardware.
If fildes refers to a socket, write() is equivalent to send(3N) with no flags set.
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.
For regular files, no data transfer will occur past the offset maximum established in the open file description with fildes .
A write() to a regular file is blocked if mandatory file/record locking is set (see chmod(2) ), and there is a record lock owned by another process on the segment of the file to be written:
If O_NDELAY or O_NONBLOCK is set, write() returns -1 and sets errno to EAGAIN .
If O_NDELAY and O_NONBLOCK are clear, write() sleeps until all blocking locks are removed or the write() is terminated by a signal.
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(2) and ulimit(2) ), 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).
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:
Any successful read(2) from each byte position in the file that was modified by that write will return the data specified by the write() for that position until such byte positions are again modified.
Any subsequent successful write() to the same byte position in the file will overwrite that file data.
Write requests to a pipe or FIFO are handled the same as a regular file with the following exceptions:
There is no file offset associated with a pipe, hence each write request appends to the end of the pipe.
Write requests of {PIPE_BUF} bytes or less are guaranteed not to be interleaved with data from other processes doing writes on the same pipe. Writes of greater than {PIPE_BUF} bytes may have data interleaved, on arbitrary boundaries, with writes by other processes, whether or not the O_NONBLOCK or O_NDELAY flags are set.
If O_NONBLOCK and O_NDELAY are clear, a write request may cause the process to block, but on normal completion it returns nbyte .
If O_NONBLOCK and O_NDELAY are set, write() does not block the process. If a write() request for PIPE_BUF or fewer bytes succeeds completely write() returns nbyte . Otherwise, if O_NONBLOCK is set, it returns -1 and sets errno to EAGAIN or if O_NDELAY is set, it returns 0 . A write() request for greater than {PIPE_BUF} bytes transfers what it can and returns the number of bytes written or it transfers no data and, if O_NONBLOCK is set, returns -1 with errno set to EAGAIN or if O_NDELAY is set, it returns 0 . Finally, if a request is greater than PIPE_BUF bytes and all data previously written to the pipe has been read, write() transfers at least PIPE_BUF bytes.
When attempting to write to a file descriptor (other than a pipe, a FIFO , a socket, or a STREAM) that supports nonblocking writes and cannot accept the data immediately:
If O_NONBLOCK and O_NDELAY are clear, write() blocks until the data can be accepted.
If O_NONBLOCK or O_NDELAY is set, write() does not block the process. If some data can be written without blocking the process, write() writes what it can and returns the number of bytes written. Otherwise, if O_NONBLOCK is set, it returns -1 and sets errno to EAGAIN or if O_NDELAY is set, it returns 0 .
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.
For STREAMS files (see intro(2) and streamio(7I) ), 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, and can not 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 zero, 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 limit
NAME | SYNOPSIS | DESCRIPTION