Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

creat(2)

dup(2)

fcntl(2)

lseek(2)

open(2)

pipe(2)

socket(2)

write(2)

NAME

write, writev − write on a file

SYNTAX

write(d, buf, nbytes)
int d;
char *buf;
int nbytes;

#include <sys/types.h>
#include <sys/uio.h>

writev(d, iov, ioveclen)
int d;
struct iovec *iov;
int ioveclen;

DESCRIPTION

The write system call attempts to write a buffer of data to a file.  The writev system call attempts to write an array of buffers of data to a file.

When a file is opened to a device capable of seeking (such a disk or tape), the write starts at the position given by the file pointer associated with the file descriptor d. This file pointer is the offset, in bytes, from the beginning of the file where the write is to begin. When the file is first opened, the file pointer is set at 0. It can be modified by the read(,), lseek(,), and write system calls. When the write call returns, the file pointer is incremented by the number of bytes actually written.

When the file is opened to a device not capable of seeking (such as sockets, pipes, or terminals), the write starts at the current position.  The value of the pointer associated with such an object is undefined. 

By default, write does asynchronous writes.  That is, after the data is written to a buffer cache, control returns to the program.  The actual write to a device takes place after control returns. However, if you use an open() or fcntl() call to open a file for synchronous writes, control does not return to the program until after the buffer cache has been written to the device.

Write requests to a pipe (or FIFO) are handled the same as a regular file with the following exceptions:

•A file offset is not associated with a pipe, therefore each write request appends to the end of the pipe.

•Write requests less than or equivalent to {PIPE_BUF} bytes are nnot interleaved with data from other processes doing writes on the same pipe.  Write requests greater than {PIPE_BUF} bytes can interleave on arbitrary boundaries with writes by other processes. 

•If the O_NDELAY and O_NONBLOCK flags are clear, a write can cause the process to block, but under normal completion it returns nbytes. 

•If the O_NDELAY or O_NONBLOCK flag is set, the write function does not block the process.  Write requests less than or equal to {PIPE_BUF} bytes either succeed and return nbytes or -1, and errno is set to [EWOULDBLOCK].  Write requests that exceed {PIPE_BUF} bytes can return complete success, partial write, or no success, and errno is to [EWOULDBLOCK].

OPTIONS

Argument Description

d is a descriptor returned by a creat, open, dup, fcntl, pipe, or socket(2) system call.

buf points to the buffer containing the data to be written. 

nbytes is a positive integer defining the number of bytes to be written from the buffer. 

iov points to a data structure of type iovec, which defines the starting location of the set of vectors forming the array and the length of each individual vector in the array to be written. This structure is defined in <sys/uio.h> as follows:

struct iovec {
        caddr_t   iov_base ;
       int       iov_len ;
} ;

The caddr_t data type is defined in <sys/types.h>, and is the recommended way to define an address for a character value.  In any case, the address iov_base is the starting address of the set of vectors. The integer value iov_len is the length of each individual vector, in bytes.

The ioveclen argument defines the number of vectors in the array of data to be written.  Note that the numbering of the vectors begins with 0 and proceeds through ioveclen −1. 

ENVIRONMENT

When your program is compiled using the System V environment, and the file was opened with the O_NDELAY flag set, then a write to a full pipe (or FIFO) will return an error, rather than a 0 as for the ULTRIX non-System V environment.

Differs from the System V definition in that the value nbytes is int rather than unsigned. 

When your program is compiled using POSIX environment, EAGAIN is returned in errno, in place of EWOULDBLOCK. 

RETURN VALUE

Upon successful completion, the number of bytes actually written is returned.  Otherwise, a −1 is returned, and errno is set to indicate the error. 

DIAGNOSTICS

The write system call will fail and the file pointer will remain unchanged if one or more of the following are true:

[EBADF] The d argument is not a valid descriptor open for writing. 

[EPIPE] An attempt was made to write to a pipe that is not open for reading by any process. 

[EPIPE] An attempt was made to write to a socket of type SOCK_STREAM which is not connected to a peer socket. 

[EFBIG] An attempt was made to write a file that exceeds the process’s file size limit, set by ulimit(,), or the maximum file size (approximately 2 Gigabytes).

[EFAULT] Part of the array pointed to by iov or data to be written to the file points outside the process’s allocated address space. 

[EWOULDBLOCK]
The O_NDELAY or O_NONBLOCK flag is set for the file descriptor and the process would be delayed in the write operation.

[ENOSPC] There is no free space remaining on the file system containing the file. 

[EDQUOT] The user’s quota of disk blocks on the file system containing the file has been exhausted. 

[EIO] An I/O error occurred while reading from or writing to the file system. 

[EINVAL] The nbytes argument is negative. 

[ETIMEDOUT] A "connect" request or remote file operation failed because the connected party did not properly respond after a period of time which is dependent on the communications protocol. 

SEE ALSO

creat(2), dup(2), fcntl(2), lseek(2), open(2), pipe(2), socket(2)

System Calls

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026