write, writex, write64x, writev, writevx, ewrite, ewritev, pwrite, or pwritev Subroutine

Purpose

Writes to a file.

Library

Item Description
write, writex, write64x, writev, writevx, pwrite, pwritev Standard C Library (libc.a)
ewrite, ewritev MLS Library (libmls.a)

Syntax

#include <unistd.h>
ssize_t write (FileDescriptor, Buffer, NBytes)
int  FileDescriptor;
const void * Buffer;
size_t  NBytes;

int writex (FileDescriptorBufferNBytesExtension)
int FileDescriptor;
char *Buffer;
unsigned int NBytes;
int  Extension;

int write64x (FileDescriptorBufferNBytesExtension)
int FileDescriptor;
void *Buffer;
size_t NBytes;
void *Extension;

ssize_t pwrite (FileDescriptor, Buffer, NBytes, Offset)
int  FileDescriptor;
const void * Buffer;
size_t  NBytes;
off_t Offset;
#include <sys/uio.h>
ssize_t writev (FileDescriptor, iov, iovCount)
int FileDescriptor;
const struct iovec *  iov;
int  iovCount;

ssize_t writevx (FileDescriptorioviovCountExtension)
int FileDescriptor;
struct iovec *iov;
int iovCount;
int  Extension;
#include <unistd.h>
#include <sys/uio.h>
ssize_t pwritev ( 
int  FileDescriptor,
const struct iovec * iov,
int iovCount,
offset_t offset);

ssize_t ewrite (FileDescriptor, Buffer, Nbytes,  labels)
int  FileDescriptor;
const void * Buffer;
size_t  NBytes;
sec_labels_t * labels;

ssize_t ewritev (FileDescriptor, iov, iovCount,labels)
int FileDescriptor;
const struct iovec * iov;
int  iovCount;
sec_labels_t * labels;

Description

The write subroutine attempts to write the number of bytes of data that is 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 runs 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 is written. The writev subroutine always writes a complete area before it proceeds to the next.

The writex and writevx subroutines are the same as the write and writev subroutines, with the addition of an Extension parameter, which is used to write 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 written.

With devices incapable of seeking, writing always begins at the current position. The value of a file pointer that is 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 are written. For example, suppose there is space for 20 bytes more in a file before it reaches 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 in current topic) and the implementation generates a SIGXFSZ signal for the thread.

Fewer bytes can be written than requested if there is not enough room to satisfy the request. Here, 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 that is 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 calling process does not have root user authority.
  • The effective user ID of the calling process does not match the user ID of the file.
  • The file is executable by the group (S_IXGRP) or other (S_IXOTH).

The write subroutine clears the SetGroupID bit (S_ISGID) if all of the following are true:

  • The calling process does not have root user authority.
  • The group ID of the file does not match the effective group ID or one of the supplementary group IDs of the process.
  • The file is executable by the owner (S_IXUSR) or others (S_IXOTH).
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 before each write.

If the FileDescriptor parameter refers to a regular file whose file status flags specify O_SYNC, this action is a synchronous update (as described in the open subroutine).

If the FileDescriptor parameter refers to a regular file that a process opens 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 conducts a synchronous update. If all processes that opened the file with the O_DEFER file status flag set close the file before a process issues an fsync subroutine or conducts 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:

  • There is no file offset associated with a pipe; hence, each write request appends to the end of the pipe.
  • If the size of the write request is less than or equal to the value of the PIPE_BUF system variable (described in the pathconf routine), the write subroutine is automatic. The data is not interleaved with data from other write processes on the same pipe. Writes of greater than PIPE_BUF bytes can have data that is interleaved, on arbitrary boundaries, with writes by other processes, whether the O_NDELAY or O_NONBLOCK file status flags are set.
  • If the O_NDELAY and O_NONBLOCK file status flags are clear (the default), a write request to a full pipe causes the process to block until enough space becomes available to handle the entire request.
  • If the O_NDELAY file status flag is set, a write to a full pipe returns a 0.
  • If the O_NONBLOCK file status flag is set, a write to a full pipe returns a value of -1 and sets the errno global variable to EAGAIN.

When the systems attempts to write to a character special file that supports nonblocking writes and no data can currently be written (streams are an exception that described later):

  • If the O_NDELAY and O_NONBLOCK flags are clear (the default), the write subroutine blocks until data can be written.
  • If the O_NDELAY flag is set, the write subroutine returns 0.
  • If the O_NONBLOCK flag is set, the write subroutine returns -1 and sets the errno global variable to EAGAIN if no data can be written.

When the systems attempts to write to a regular file that supports enforcement-mode record locks, and all or part of the region to be written is locked by another process, the following can occur:

  • If the O_NDELAY and O_NONBLOCK file status flags are clear (the default), the calling process blocks until the lock is released.
  • If the O_NDELAY or O_NONBLOCK file status flag is set, then the write subroutine returns a value of -1 and sets the errno global variable to EAGAIN.
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 are written. If nbyte does not fall within the range and the minimum packet size value is 0, write breaks the buffer into maximum packet size segments before it sends the data downstream (the last segment contains less than the maximum packet size). If nbyte does not fall within the range and the minimum value is non-zero, write fails 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 can issue I_SWROPT ioctl to enable zero-length messages to be sent across the pipe or FIFO.

When the system writes to a STREAM, data messages are created with a priority band of 0. When it is writing to a STREAM that is not a pipe or FIFO:

  • O_NONBLOCK specify either O_NONBLOCK or O_NDELAY. The IBM® streams implementation treats these two the same.
  • If O_NONBLOCK or O_NDELAY is clear, 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_NONBLOCK or O_NDELAY is set and the STREAM cannot accept data, write returns -1 and set errno to EAGAIN.
  • If O_NONBLOCK or O_NDELAY is set and part of the buffer was written while a condition in which the STREAM cannot accept more data occurs, write ends and return the number of bytes written.
Note: The IBM streams implementation treats O_NONBLOCK and O_NDELAY the same.

In addition, write and writev fail if the STREAM head processes an asynchronous error before the call. Here, 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 is written. The writev function always writes a complete area before it proceeds 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 returns 0 and have no other effect. For other file types, the behavior 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:

  • If the handler is installed with an indication that subroutines not be restarted, the write subroutine returns a value of -1 and sets the errno global variable to EINTR (even if some data was already written).
  • If the handler is installed with an indication that subroutines be restarted, and:
    • If no data was written when the interrupt was handled, the write subroutine does not return a value (it is restarted).
    • If data was written when the interrupt was handled, this write subroutine returns the amount of data already written.
Note: A write to a regular file is not interruptible. Only the writes to objects that can block indefinitely, such as FIFOs, sockets, and some devices, are interruptible. If fildes refers to a socket, write is equivalent to the send subroutine with no flags set.

The write64x subroutine is the same as the writex subroutine, where the Extension parameter is a pointer to a j2_ext structure (see the j2/j2_cntl.h file). The write64x subroutine is used to write an encrypted file in raw mode (see O_RAW in the fcntl.h file). Using the O_RAW flag on encrypted files has the same limitations as using O_DIRECT on regular files.

The ewrite and ewritev subroutines write to a stream and set the security attributes. The ewrite subroutine copies the number of bytes of the data that is specified by the Nbyte parameter from the buffer pointed to by the Buffer parameter to a stream associated with the FileDescriptor parameter. Security information for the message is set to the values in the structure pointed to by the labels parameter.

The pwrite function conducts 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 that is offset for the wanted position inside the file.

ssize_t pwrite64(int fd , const void *buf , size_t nbytes , off64_t offset)

The pwrite64 subroutine conducts the same action as pwrite but the limit of offset to the maximum file size for the file that is associated with the fileDescriptor and DEV_OFF_MAX if the file associated with fileDescriptor is a block special or character special file.

Using the write or pwrite subroutine with a file descriptor obtained from a call to the shm_open subroutine fails with ENXIO.

The pwritev subroutine conducts the same action as the writev subroutine, except that the pwritev subroutine writes to the given position in the file without changing the file pointer. The first three arguments of the pwritev subroutine are the same as the writev subroutine with the addition of the offset argument that points to the position that you want inside the file. An error occurs when the file that the pwritev subroutine writes to is incapable of seeking.

Parameters

Item Description
Buffer Identifies the buffer that contains the data to be written.
Extension Provides communication with character device drivers that require more information or return additional status. Each driver interprets the Extension parameter in a device-dependent way, either as a value or as a pointer to a communication area. Drivers must apply reasonable defaults when the Extension parameter value is 0.
FileDescriptor Identifies the object to which the data is to be written.
iov Points to an array of iovec structures, which identifies the buffers that contain the data to be written. The iovec structure is defined in the sys/uio.h file and contains the following members:
caddr_t  iov_base;
size_t  iov_len;
iovCount Specifies the number of iovec structures pointed to by the iov parameter.
NBytes Specifies the number of bytes to write.
offset The position in the file where the writing begins.
labels A pointer to the extended security attribute structure.

Return Values

Upon successful completion, the write, writex, write64x, writev, writevx, and pwritev subroutines return the number of bytes that were 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.

Upon successful completion, the ewrite and ewritev subroutines return a value of 0. Otherwise, the global variable errno is set to identify the error.

Error Codes

The write, writex, write64x, writev, writevx, ewrite, ewritev, and pwritev subroutines are unsuccessful when one or more of the following are true:

Item Description
EAGAIN The O_NONBLOCK flag is set on this file and the process would be delayed in the write operation; or an enforcement-mode record lock is outstanding in the portion of the file that is to be written.
EBADF The FileDescriptor parameter does not specify a valid file descriptor open for writing.
EDQUOT New disk blocks cannot be allocated for the file because the user or group quota of disk blocks is exhausted on the file system.
EFBIG An offset greater than MAX_FILESIZE was requested on the 32-bit kernel.
EFAULT The Buffer parameter or part of the iov parameter points to a location outside of the allocated address space of the process.
EFBIG An attempt was made to write a file that exceeds the process' file size limit or the maximum file size. If the user sets the environment variable XPG_SUS_ENV=ON before execution of the process, then the SIGXFSZ signal is posted to the process when it exceeds the process' file size limit.
EINVAL The file position pointer that is associated with the FileDescriptor parameter was negative; the iovCount parameter value was not 1 - 16, inclusive; or one of the iov_len values in the iov array was negative.
EINVAL The sum of the iov_len values from a 32-bit application overflowed a 32-bit signed integer in either a 32-bit or a 64-bit kernel environment, or the sum of the iov_len values from a 64-bit application overflowed a 32-bit signed integer in a 32-bit kernel environment.
EINVAL The STREAM or multiplexer that is referenced byfileDescriptor is linked (directly or indirectly) downstream from a multiplexer.
EINVAL The value of the Nbytes parameter that is larger than OFF_MAX, was requested on the 32-bit kernel. Here, the system call is requested from a 64-bit application that is running on a 32-bit kernel.
EINTR A signal was caught during the write operation, and the signal handler was installed with an indication that subroutines are not to be restarted.
EIO An I/O error occurred while the system is writing to the file system; or the process is a member of a background process group that is attempting to write to its control terminal, TOSTOP is set, the process is not ignoring or blocking SIGTTOU, and the process group has no parent process.
ENOSPC No free space is left on the file system that contains the file.
ENXIO A hangup occurred on the STREAM being written to.

The write or pwrite subroutine was used with a file descriptor obtained from a call to the shm_open subroutine.

EPIPE An attempt was made to write to a file that is not opened for reading by any process, or to a socket of type SOCK_STREAM that is not connected to a peer socket; or an attempt was made to write to a pipe or FIFO that is not open for reading by any process. If this condition occurs, a SIGPIPE signal is sent to the process.
ERANGE The transfer request size was outside the range that is supported by the STREAMS file that is associated with FileDescriptor.

The write, writex, writev, writevx, and pwritev subroutines might be unsuccessful if the following is true:

Item Description
ENXIO A request was made of a nonexistent device, or the request was outside the capabilities of the device.
EFBIG An attempt was made to write to a regular file where NBytes greater than zero and the starting offset is greater than or equal to the offset maximum established in the open file description that is associated with FileDescriptor.
EINVAL The offset argument is invalid. The value is negative.
ESPIPE fildes is associated with a pipe or FIFO.
The write64x subroutine was unsuccessful if the EINVAL error code is returned:
Item Description
EINVAL The j2_ext structure was not initialized correctly. For example, the version was wrong, or the file was not encrypted.
EINVAL The j2_ext structure is passed by using the J2EXTCMD_RDRAW command for files that were not opened in raw-mode.
The ewrite and ewritev subroutines were unsuccessful if one of the following error codes is true:
Item Description
ENOMEM The memory or space is too small.
EACCES Permission is denied. The user does not have sufficient privilege to write data.
ERESTART ERESTART is used to determine whether a system call is restartable.