Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > W

write(2)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

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 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.

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 dependent.

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 implementation 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 implementation 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.

  • The system-dependent maximum number of bytes that a pipe or FIFO can store is PIPSIZ as defined in <sys/inode.h>.

  • The minimum value of PIPSIZ on any HP-UX system is 8192.

When writing a pipe with the O_NDELAY or O_NONBLOCK file status flag set, the following apply:

  • If nbyte is less than or equal to PIPSIZ and sufficient room exists in the pipe or FIFO, the write() succeeds and returns the number of bytes written.

  • If nbyte is less than or equal to PIPSIZ but insufficient room exists in the pipe or FIFO, the write() returns having written nothing. If O_NONBLOCK is set, -1 is returned and errno is set to EAGAIN. If O_NDELAY is set, 0 is returned.

  • If nbyte is greater than PIPSIZ and the pipe or FIFO is full, the write returns having written nothing. If O_NONBLOCK is set, -1 is returned and errno is set to EAGAIN. If O_NDELAY is set, 0 is returned.

  • If nbyte is greater than PIPSIZ, and some room exists in the pipe or FIFO, as much data as fits in the pipe or FIFO is written, and write() returns the number of bytes actually written, an amount less than the number of bytes requested.

When writing a pipe and the O_NDELAY and O_NONBLOCK file status flags are clear, the write() always executes correctly (blocking as necessary), and returns the number of bytes written.

When attempting to write to a file descriptor (other than a pipe or FIFO) that supports non-blocking writes and cannot accept the data immediately, the following apply:

  • If the O_NONBLOCK flag is clear, write() will block until the data can be accepted.

  • If the O_NONBLOCK flag is set, write() will not block the process. If some data can be written without blocking the process, write() will write what it can and return the number of bytes written. Otherwise, it will return -1 and errno will be set to EAGAIN.

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 character special devices, if the stopio() call was used on the same device after it was opened, write() returns -1, sets errno to EBADF, and issues the SIGHUP signal to the process. write() also clears the potential and granted privilege vectors on the file.

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, the following apply:

  • If O_NONBLOCK is clear, and the STREAM cannot accept data (the STREAM write queue is full due to internal flow control conditions), write() will block until data can be accepted.

  • If O_NONBLOCK is set and the STREAM cannot accept data, write() will return -1 and set errno to EAGAIN.

  • If O_NONBLOCK is set and part of the buffer has been written while a condition in which the STREAM cannot accept additional data occurs, write() will terminate and return the number of bytes written.

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.

If the write is performed by any user other than the owner or a user who has appropriate privileges, write() clears the set-user-ID, set-group-ID, and sticky bits on all nondirectory files. If the write is performed by the owner or a user who has appropriate privileges, the behavior is file-system dependent. In some file systems, the write clears the set-user-ID, set-group-ID, and sticky bits on a nondirectory file. In other file systems, the write does not clear these bits on a nondirectory file.

For directories, write() does not clear the set-user-ID, set-group-ID, and sticky bits.

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}, as 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. The iovec structure is defined in /usr/include/sys/uio.h.

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 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 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.

When a write() system call executes on a local file system and needs a new buffer to be allocated to hold the data, the buffer maps onto the disk at that time. If the disk is full, write() returns an error and errno is set to ENOSPC.

When a write() system call executes on an NFS file system, the write() allocates the new buffer without communicating with the NFS server to see if there is space for the buffer (to improve NFS performance). The disk-full condition is checked, only when the buffer is written to the server (at file close or the buffer is full). If the disk is full, close() returns an error. See close(2).

RETURN VALUE

Upon successful completion, write() and pwrite() will return the number of bytes actually written to the file associated with fildes. This number will never be greater than nbyte. Otherwise, -1 is returned and errno is set to indicate the error.

Upon successful completion, writev() returns the number of bytes actually written. Otherwise, it returns a value of -1, the file pointer remains unchanged, and errno is set to indicate an error.

A write to a STREAMS file may 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.

ERRORS

Under the following conditions, write(), pwrite() and writev() fail and set errno to:

EAGAIN

The O_NONBLOCK flag was set for the file descriptor and the process was delayed in the write() operation.

EAGAIN

Enforcement-mode file and record locking was set, O_NDELAY was set, and there was a blocking record lock.

EBADF

The fildes argument was not a valid file descriptor open for writing.

EDEADLK

A resource deadlock would occur as a result of this operation (see lockf(2) and fcntl(2)).

EDQUOT

User's disk quota block limit has been reached for this file system.

EFBIG

An attempt was made to write a file that exceeds the implementation-dependent maximum file size or the process' file size limit.

EFBIG

The file is a regular file and nbyte is greater than zero and the starting position is greater than or equal to the offset maximum established in the open file description associated with fildes.

EINTR

The write operation was terminated due to the receipt of a signal, and no data was transferred.

EINVAL

The STREAM or multiplexer referenced by fildes is linked (directly or indirectly) downstream from a multiplexer.

EIO

A physical I/O error has occurred.

EIO

The process is a member of a background process group attempting to write to its controlling terminal. TOSTOP is set, the process is neither ignoring nor blocking SIGTTOU, and the process group of the process is orphaned. This error may also be returned under implementation-dependent conditions.

ENOLCK

The system record lock table is full, preventing the write from sleeping until the blocking record lock is removed.

ENOSPC

Not enough space on the file system. The process does not possess the limit effective privilege to override this restriction.

ENXIO

A request was made of a non-existent device, or the request was outside the capabilities of the device.

ENXIO

A hangup occurred on the STREAM being written to.

EPIPE

An attempt is made to write to a pipe or FIFO that is not open for reading by any process, or that only has one end open. A SIGPIPE signal will also be sent to the process.

ERANGE

The transfer request size was outside the range supported by the STREAMS file associated with fildes.

Under the following conditions, writev() fails and sets errno to:

EFAULT

iov_base or iov points outside of the allocated address space. The reliable detection of this error is implementation dependent.

EINVAL

One of the iov_len values in the iov array is negative.

EINVAL

The sum of the iov_len values in the iov array would overflow an ssize_t.

Under the following conditions, the writev() function may fail and set errno to:

EINVAL

The iovcnt argument was less than or equal to 0, or greater than {IOV_MAX}.

Under the following conditions, the pwrite() function fails, the file pointer remains unchanged and errno is set to:

EINVAL

The offset argument is invalid, and the value is negative.

ESPIPE

The fildes argument is associated with a pipe or FIFO.

Under the following conditions, write() or writev() fails, the file offset is updated to reflect the amount of data transferred and errno is set to:

EFAULT

buf points outside the process's allocated address space. The reliable detection of this error is implementation dependent.

EXAMPLES

Assuming a process opened a file for writing, the following call to write() attempts to write mybufsize bytes to the file from the buffer to which mybuf points.

#include <string.h> int fildes; size_t mybufsize; ssize_t nbytes; char *mybuf = "aeiou and sometimes y"; mybufsize = (size_t)strlen (mybuf); nbytes = write (fildes, (void *)mybuf, mybufsize);

WARNINGS

Character special devices, and raw disks in particular, apply constraints on how write() can be used. See specific Section 7 manual entries for details on particular devices.

AUTHOR

write() was developed by HP, AT&T, and the University of California, Berkeley.

SEE ALSO

mkfs(1M), chmod(2), creat(2), dup(2), fcntl(2), getrlimit(2), lockf(2), lseek(2), open(2), pipe(2), ulimit(2), ustat(2), privileges(5), signal(5), <limits.h>, <stropts.h>, <sys/uio.h>, <unistd.h>.

STANDARDS CONFORMANCE

write(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, POSIX.4

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.