#include <unistd.h>
ssize_t write (FileDescriptor, Buffer, NBytes) int FileDescriptor; const void *Buffer; size_t NBytes;
int writex (FileDescriptor, Buffer, NBytes, Extension) int FileDescriptor; char *Buffer; unsigned int NBytes; int Extension;
#include <sys/uio.h>
ssize_t writev (FileDescriptor, iov, iovCount) int FileDescriptor; const struct iovec * iov; int iovCount;
int writevx (FileDescriptor, iov, iovCount, Extension) int FileDescriptor; struct iovec *iov; int iovCount; int Extension;
ssize_t pwrite (FileDescriptor, Buffer, NBytes, Offset) int FileDescriptor; const void *Buffer; size_t NBytes; off_t Offset;
The write subroutine attempts to write the number of bytes of data specified by the NBytes parameter to the file associated with the FileDescriptor parameter from the buffer pointed to by the Buffer parameter.
The writev subroutine performs the same action but gathers the output data from the iovCount buffers specified by the array of iovec structures pointed to by the iov parameter. Each iovec entry specifies the base address and length of an area in memory from which data should be written. The writev subroutine always writes a complete area before proceeding to the next.
The writex and writevx subroutines are the same as the write and writev subroutines, respectively, with the addition of an Extension parameter, which is used when writing to some device drivers.
With regular files and devices capable of seeking, the actual writing of data proceeds from the position in the file indicated by the file pointer. Upon return from the write subroutine, the file pointer increments by the number of bytes actually written.
With 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 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.
Fewer bytes can be written than requested if there is not enough room to satisfy the request. In this case the number of bytes written is returned. The next attempt to write a nonzero number of bytes is unsuccessful (except as noted in the following text). The limit reached can be either that set by the ulimit subroutine or the end of the physical medium.
Successful completion of a write subroutine clears the SetUserID bit (S_ISUID) of a file if all of the following are true:
The write subroutine clears the SetGroupID bit (S_ISGID) if all of the following are true:
Note: Clearing of the SetUserID and SetGroupID bits can occur even if the write subroutine is unsuccessful, if file data was modified before the error was detected.
If the O_APPEND flag of the file status is set, the file offset is set to the end of the file prior to each write.
If the FileDescriptor parameter refers to a regular file whose file status flags specify O_SYNC, this is a synchronous update (as described in the open subroutine).
If the FileDescriptor parameter refers to a regular file that a process has opened with the O_DEFER file status flag set, the data and file size are not updated on permanent storage until a process issues an fsync subroutine or performs a synchronous update. If all processes that have the file open with the O_DEFER file status flag set close the file before a process issues an fsync subroutine or performs a synchronous update, the data and file size are not updated on permanent storage.
Write requests to a pipe (or first-in-first-out (FIFO)) are handled the same as a regular file with the following exceptions:
When attempting to write to a character special file that supports nonblocking writes, such as a terminal, and no data can currently be written:
When attempting to write to a regular file that supports enforcement-mode record locks, and all or part of the region to be written is currently locked by another process, the following can occur:
Note: The fcntl subroutine provides more information about record locks.
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.
The behavior of an interrupted write subroutine depends on how the handler for the arriving signal was installed. The handler can be installed in one of two ways, with the following results:
Note: A write to a regular file is not interruptible. Only writes to objects that may block indefinitely, such as FIFOs, sockets, and some devices, are generally interruptible.
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.
Upon successful completion, the write, writex, writev, and writevx subroutines return the number of bytes that were actually written. The number of bytes written is never greater than the value specified by the NBytes parameter. Otherwise, a value of -1 is returned and the errno global variable is set to indicate the error.
The write, writex, writev, and writevx subroutines are unsuccessful when one of the following is true:
The write, writex, writev, and writevx subroutines may be unsuccessful if the following is true:
These subroutines are part of Base Operating System (BOS) Runtime.
The fcntl, dup, or dup2 subroutine, fsync subroutine, ioctl subroutine, lockfx subroutine, lseek subroutine, open, openx, or creat subroutine, pathconf subroutine, pipe subroutine, poll subroutine, select subroutine, ulimit subroutine.
The limits.h, stropts.h, sys/uio.h, unistd.h files.
The Input and Output Handling Programmer's Overview in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.