write.2 (2010 09)

w
write(2) write(2)
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 associ-
ated 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.
For ordinary files, if the
O_DSYNC file status flag is set, the write does not return until both the file data
and the file attributes required to retrieve the data are physically updated. If the O_SYNC flag is set, the
behavior is identical to that of
O_DSYNC, with the addition that all file attributes changed by the write
operation, including access time, modification time and status change time, are also physically updated
before returning to the calling process.
For block special files, if the
O_DSYNC or the O_SYNC flag is set, the write does not return until the data
is physically updated. How the data reaches the physical media is implementation and hardware depen-
dent.
A write to an ordinary file is prevented if enforcement-mode file and record locking is set, another process
owns a lock on the segment of the file being written, and the following apply:
If
O_NDELAY or O_NONBLOCK is set, the write returns 1 and sets errno to [EAGAIN].
If
O_NDELAY and O_NONBLOCK are clear, the write does not complete until the blocking record
lock is removed.
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 imple-
mentation will generate a
SIGXFSZ signal for the process.
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 implementa-
tion dependent.
After a
write() to a regular file has successfully returned:
Any successful
read() 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 will be handled the same as a regular file with the following exceptions:
There is no file offset associated with a pipe, hence each write request will append to the end of
the pipe.
HP-UX 11i Version 3: September 2010 1 Hewlett-Packard Company 1

Summary of content (6 pages)