write(2) write(2) NAME write, writev, pwrite, pwrite64 - 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); ssize_t pwrite64(int fildes, const void *buf, size_t nbyte, off64_t offset); #include <sys/uio.h> ssize_t writev(int fildes, const struct iovec *iov, int iovcnt); DESCRIPTION write attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with fildes. If nbyte is zero and the file is a regular file, write returns zero and has no other results. fildes is a file descriptor obtained from a creat, open, dup, fcntl, pipe, or ioctl system call. pwrite and pwrite64 are the same as write except that they do the equivalent of an lseek (for pwrite) or lseek64 (for pwrite64) offset bytes with the whence set to SEEK_SET before writing. On return from pwrite or pwrite64, the file pointer is unchanged. If fildes refers to a file incapable of seeking (a fifo or socket) then an error is returned and errno will be set to ESPIPE. 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}. {IOV_MAX} can be obtained from a call to sysconf() [see sysconf(3C)]. For writev, the iovec structure contains the following members: void *iov_base; ssize_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 have been physically updated. 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 physically updated. If the O_DSYNC flag of the file status flags is set, write does not return until the file data has been physically updated. This function is for special applications that require extra reliability at the cost of performance. 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). In addition to an error return, the SIGXFSZ signal is sent to the caller. The default disposition of this signal depends on the environment from which the application was run - some standard shells invoke programs with this signal ignored, others with the signal set to the default (which will kill the process). When attempting to write to a file with O_DIRECT or FDIRECT set, -1 will be returned and errno will be set to EINVAL if nbyte or the current file position is not a multiple of the underlying device's blocksize, nbyte is too big or buf isn't properly aligned. See also F_DIOINFO in fcntl(2). When attempting to write to a file with O_DIRECT or FDIRECT set, the portion being written can not be locked in memory by any process. In this case, -1 will be returned and errno will be set to EBUSY. When attempting to write to a regular file in a DMAPI file system, if the DMAPI application will take a considerable time to make a portion of the file available needed for the write to proceed: 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 the the DMAPI application has made the file data available, and then allows the write to proceed. 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 threads doing writes on the same pipe. Writes of greater than {PIPE_BUF} bytes may have data interleaved, on arbitrary boundaries, with writes by other threads, whether the O_NONBLOCK or O_NDELAY flags are set. If O_NONBLOCK and O_NDELAY are clear, a write request may cause the thread to block, but on normal completion it returns nbyte. If O_NONBLOCK is set, write requests are handled in the following way: the write does not block the thread; write requests for {PIPE_BUF} or fewer bytes either succeed completely and return nbyte, or return -1 and set errno to EAGAIN. A write request for greater than {PIPE_BUF} bytes either transfers what it can and returns the number of bytes written, or transfers no data and returns -1 with errno set to EAGAIN. Also, 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. If O_NDELAY is set, write requests are handled in the following way: the write does not block the thread; write requests for {PIPE_BUF} or fewer bytes either succeed completely and return nbyte, or return 0. A write request for greater than {PIPE_BUF} bytes either transfers what it can and returns the number of bytes written, or transfers no data and returns 0. Also, 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 or FIFO) 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 thread. If some data can be written without blocking the thread, 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. For STREAMS files [see intro(2)], 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(7)], these values 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 errno to ERANGE. Writing a zero-length buffer (nbyte is zero) to a STREAMS device sends a zero length message with zero returned. However, writing a zero-length buffer to a pipe or FIFO sends no message and zero is returned. The user program may issue the I_SWROPT ioctl(2) to enable zero-length messages to be sent across the pipe or FIFO [see streamio(7)]. When writing to a stream, data messages are created with a priority band of zero. When writing to a stream that is not a pipe or FIFO: If O_NDELAY and O_NONBLOCK are not set, and the stream cannot accept data (the stream write queue is full because of internal flow control conditions), write blocks until data can be accepted. If O_NDELAY or O_NONBLOCK is set and the stream cannot accept data, write returns -1 and sets errno to EAGAIN. If O_NDELAY or O_NONBLOCK is set and part of the buffer has already been written when a condition occurs in which the stream cannot accept additional data, write terminates and returns the number of bytes written. The number of bytes written may be less than nbyte. This can occur if the write gets interrupted for some reason, but some bytes have already been written. write and writev fail and the file pointer remains unchanged if one or more of the following are true: EACCES fildes points to a file in an NFS-mounted filesystem and the server refused permission to write. EAGAIN Mandatory file/record locking is set, O_NDELAY or O_NONBLOCK is set, and there is a blocking record lock. EAGAIN Total amount of system memory available when reading via raw I/O is temporarily insufficient. EAGAIN An attempt is made to write to a stream that can not accept data with the O_NDELAY or O_NONBLOCK flag set. EAGAIN If a write to a pipe or FIFO of {PIPE_BUF} bytes or less is requested and less than nbyte of free space is available. EAGAIN A DMAPI application might delay a considerable time retrieving a portion of the file data needed for the write to proceed, and O_NDELAY or O_NONBLOCK was set. EBADF fildes is not a valid file descriptor open for writing. EBUSY Could not do direct I/O write because pages of the target file are locked in memory. EDEADLK The write was going to go to sleep and cause a deadlock to occur. EDQUOT Disc quota exceeded [see intro(2)]. EFAULT buf points outside the process's allocated address space. EFBIG An attempt is made to write a file that exceeds the process's file size limit or the maximum file size [see getrlimit(2) and ulimit(2)]. EINTR A signal was caught during the write system call and no bytes had been written. EINVAL An attempt is made to write to a stream linked below a multiplexor. EINVAL fildes has O_DIRECT or FDIRECT set and either the buffer alignment, current file pointer alignment or write request size is not valid for direct I/O. See also F_DIOINFO in fcntl(2). EIO 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. EIO fildes points to a device special file that is in the closing state. EIO fildes points to a file in an NFS-mounted filesystem and there was an NFS protocol error. EISDIR fildes points to a directory, or fildes points to an object in an NFS-mounted filesystem which is not a regular file. EMSGSIZE fildes points to a file in an NFS-mounted filesystem and a message sent to the server was larger than the internal message buffer or some other network limit. ENOBUFS fildes points to a file in an NFS-mounted filesystem and the system lacked sufficient buffer space to send a message to the server. ENOLCK The system record lock table was full, so the write could not go to sleep until the blocking record lock was removed. ENOSR An attempt is made to write to a stream with insufficient STREAMS memory resources available in the system. ENOSPC During a write to an ordinary file, there is no free space left on the device. ENXIO 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. EPIPE An attempt is made to write to a FIFO that is not open for reading by any process. EPIPE An attempt is made to write to a pipe that has only one end open. ERANGE 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. ESTALE Stale NFS file handle [see intro(2)]. ETIMEDOUT The object of the write is located on a remote system which is not available [see intro(2)]. ENOLCK Enforced record locking was enabled and {LOCK_MAX} regions are already locked in the system. ESPIPE pwrite or pwrite64 was called on a file incapable of seeking. In addition, writev may return one of the following errors: EINVAL iovcnt was less than or equal to 0, or greater than {IOV_MAX}. {IOV_MAX} can be obtained from a call to sysconf() [see sysconf(3C)]. EINVAL An iov_len value in the iov array was negative. EINVAL 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. On successful completion write and writev mark for update the st_ctime and st_mtime fields of the file. SEE ALSO creat(2), dup(2), fcntl(2), getrlimit(2), intro(2), lseek(2), open(2), pipe(2), sysconf(3C), types(5), ulimit(2) DIAGNOSTICS On success, write returns the number of bytes actually written. Otherwise, it returns -1 and sets errno to identify the error. Page 7