JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
man pages section 2: System Calls     Oracle Solaris 11.1 Information Library
search filter icon
search icon

Document Information

Preface

Introduction

System Calls

access(2)

acct(2)

acl(2)

adjtime(2)

alarm(2)

brk(2)

chdir(2)

chmod(2)

chown(2)

chroot(2)

close(2)

creat(2)

dup(2)

exec(2)

execl(2)

execle(2)

execlp(2)

execv(2)

execve(2)

execvex(2)

execvp(2)

_Exit(2)

_exit(2)

exit(2)

faccessat(2)

facl(2)

fchdir(2)

fchmod(2)

fchmodat(2)

fchown(2)

fchownat(2)

fchroot(2)

fcntl(2)

fexecve(2)

fgetlabel(2)

fork1(2)

fork(2)

forkall(2)

forkallx(2)

forkx(2)

fpathconf(2)

fstat(2)

fstatat(2)

fstatvfs(2)

futimens(2)

futimesat(2)

getacct(2)

getcontext(2)

getdents(2)

getegid(2)

geteuid(2)

getgid(2)

getgroups(2)

getisax(2)

getitimer(2)

getlabel(2)

getmsg(2)

getpflags(2)

getpgid(2)

getpgrp(2)

getpid(2)

getpmsg(2)

getppid(2)

getppriv(2)

getprojid(2)

getrctl(2)

getrlimit(2)

getsid(2)

gettaskid(2)

getuid(2)

getustack(2)

ioctl(2)

issetugid(2)

kill(2)

lchown(2)

link(2)

linkat(2)

llseek(2)

lseek(2)

lstat(2)

_lwp_cond_broadcast(2)

_lwp_cond_reltimedwait(2)

_lwp_cond_signal(2)

_lwp_cond_timedwait(2)

_lwp_cond_wait(2)

_lwp_continue(2)

_lwp_info(2)

_lwp_kill(2)

_lwp_mutex_lock(2)

_lwp_mutex_trylock(2)

_lwp_mutex_unlock(2)

_lwp_self(2)

_lwp_sema_init(2)

_lwp_sema_post(2)

_lwp_sema_trywait(2)

_lwp_sema_wait(2)

_lwp_suspend(2)

memcntl(2)

meminfo(2)

mincore(2)

mkdir(2)

mkdirat(2)

mknod(2)

mknodat(2)

mmap(2)

mmapobj(2)

mount(2)

mprotect(2)

msgctl(2)

msgget(2)

msgids(2)

msgrcv(2)

msgsnap(2)

msgsnd(2)

munmap(2)

nice(2)

ntp_adjtime(2)

ntp_gettime(2)

open(2)

openat(2)

pathconf(2)

pause(2)

pcsample(2)

pipe(2)

poll(2)

p_online(2)

ppoll(2)

pread(2)

priocntl(2)

priocntlset(2)

processor_bind(2)

processor_info(2)

profil(2)

pset_assign(2)

pset_bind(2)

pset_create(2)

pset_destroy(2)

pset_getattr(2)

pset_info(2)

pset_list(2)

pset_setattr(2)

putacct(2)

putmsg(2)

putpmsg(2)

pwrite(2)

read(2)

readlink(2)

readlinkat(2)

readv(2)

rename(2)

renameat(2)

resolvepath(2)

rmdir(2)

sbrk(2)

semctl(2)

semget(2)

semids(2)

semop(2)

semtimedop(2)

setcontext(2)

setegid(2)

seteuid(2)

setgid(2)

setgroups(2)

setitimer(2)

setpflags(2)

setpgid(2)

setpgrp(2)

setppriv(2)

setrctl(2)

setregid(2)

setreuid(2)

setrlimit(2)

setsid(2)

settaskid(2)

setuid(2)

setustack(2)

shmadv(2)

shmat(2)

shmctl(2)

shmdt(2)

shmget(2)

shmids(2)

shmop(2)

sigaction(2)

sigaltstack(2)

sigpending(2)

sigprocmask(2)

sigsend(2)

sigsendset(2)

sigsuspend(2)

sigwait(2)

__sparc_utrap_install(2)

stat(2)

statvfs(2)

stime(2)

swapctl(2)

symlink(2)

symlinkat(2)

sync(2)

sysfs(2)

sysinfo(2)

time(2)

times(2)

uadmin(2)

ulimit(2)

umask(2)

umount(2)

umount2(2)

uname(2)

unlink(2)

unlinkat(2)

ustat(2)

utime(2)

utimensat(2)

utimes(2)

uucopy(2)

vfork(2)

vforkx(2)

vhangup(2)

waitid(2)

wracct(2)

write(2)

writev(2)

yield(2)

write

, pwrite

, writev

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

If the O_SYNC bit has been set, write I/O operations on the file descriptor complete as defined by synchronized I/O file integrity completion.

If fildes refers to a socket, write() is equivalent to send(3SOCKET) with no flags set.

On a file not capable of seeking, writing always takes place starting at the current position. The value of a file offset associated with such a device is undefined.

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.

For regular files, no data transfer will occur past the offset maximum established in the open file description with fildes.

A write() to a regular file is blocked if mandatory file/record locking is set (see chmod(2)), and there is a record lock owned by another process on the segment of the file to be written:

If a write() requests that more bytes be written than there is room for—for example, if the write would exceed the process file size limit (see getrlimit(2) and ulimit(2)), the system file size limit, or the free space on the device—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 returns 20. The next write() of a non-zero number of bytes gives a failure return (except as noted for pipes and FIFO below).

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 write() exceeds the process file size limit, the application generates a SIGXFSZ signal, whose default behavior is to dump core.

After a write() to a regular file has successfully returned:

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

When attempting to write to a file descriptor (other than a pipe, a FIFO, a socket, or a stream) that supports nonblocking writes and cannot accept the data immediately:

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 streams files (see Intro(2) and streamio(7I)), 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 contained in the topmost stream module, and can not be set or tested from user level. 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 zero, write() breaks the buffer into maximum packet size segments prior to sending the data downstream (the last segment may be smaller than the maximum packet size). If nbyte does not fall within the range and the minimum value is non-zero, write() fails and sets errno to ERANGE. Writing a zero-length buffer (nbyte is zero) to a streams device sends a zero length message with zero returned. However, writing a zero-length buffer to a pipe or FIFO sends no message and zero is returned. The user program may issue the I_SWROPT ioctl(2) to enable zero-length messages to be sent across the pipe or FIFO (see streamio(7I)).

When writing to a stream, data messages are created with a priority band of zero. When writing to a socket or to a stream that is not a pipe or a FIFO:

The write() and writev() functions 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.

pwrite()

The pwrite() function is equivalent to write(), except that it writes into a given position and does not change the file offset (regardless of whether O_APPEND is set). 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.

writev()

The writev() function performs the same action as 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]. The iovcnt buffer is valid if greater than 0 and less than or equal to {IOV_MAX}. See Intro(2) for a definition of {IOV_MAX}.

The iovec structure contains the following members:

void    *iov_base;
size_t  iov_len;

Each iovec entry specifies the base address and length of an area in memory from which data should be written. The writev() function always writes all data from an 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 behavior is unspecified.

If the sum of the iov_len values is greater than SSIZE_MAX, the operation fails and no data is transferred.

Return Values

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

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

Errors

The write(), pwrite(), and writev() functions will fail if:

EAGAIN

Mandatory file/record locking is set, O_NDELAY or O_NONBLOCK is set, and there is a blocking record lock; an attempt is made to write to a stream that can not accept data with the O_NDELAY or O_NONBLOCK flag set; or a write to a pipe or FIFO of PIPE_BUF bytes or less is requested and less than nbytes of free space is available.

EBADF

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

EDEADLK

The write was going to go to sleep and cause a deadlock situation to occur.

EDQUOT

The user's quota of disk blocks on the file system containing the file has been exhausted.

EFBIG

An attempt is made to write a file that exceeds the process's file size limit or the maximum file size (see getrlimit(2) and ulimit(2)).

EFBIG

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

EINTR

A signal was caught during the write operation and no data was transferred.

EIO

The process is in the background and is attempting to write to its controlling terminal whose TOSTOP flag is set, or the process is neither ignoring nor blocking SIGTTOU signals and the process group of the process is orphaned.

ENOLCK

Enforced record locking was enabled and {LOCK_MAX} regions are already locked in the system, or the system record lock table was full and the write could not go to sleep until the blocking record lock was removed.

ENOLINK

The fildes argument is on a remote machine and the link to that machine is no longer active.

ENOSPC

During a write to an ordinary file, there is no free space left on the device.

ENOSR

An attempt is made to write to a streams with insufficient streams memory resources available in the system.

ENXIO

A hangup occurred on the stream being written to.

EPIPE

An attempt is made to write to a pipe or a FIFO that is not open for reading by any process, or that has only one end open (or to a file descriptor created by socket(3SOCKET), using type SOCK_STREAM that is no longer connected to a peer endpoint). A SIGPIPE signal will also be sent to the thread. The process dies unless special provisions were taken to catch or ignore the signal.

ERANGE

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

The write() and pwrite() functions will fail if:

EFAULT

The buf argument points to an illegal address.

EINVAL

The nbyte argument overflowed an ssize_t.

The pwrite() function fails and the file pointer remains unchanged if:

ESPIPE

The fildes argument is associated with a pipe or FIFO.

The write() and writev() functions may fail if:

EINVAL

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

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.

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.

The writev() function may fail if:

EINVAL

The iovcnt argument was less than or equal to 0 or greater than {IOV_MAX}; one of the iov_len values in the iov array was negative; or the sum of the iov_len values in the iov array overflowed an ssize_t.

Usage

The pwrite() function has a transitional interface for 64-bit file offsets. See lf64(5).

Attributes

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
write() is Async-Signal-Safe
Standard

See Also

Intro(2), chmod(2), creat(2), dup(2), fcntl(2), getrlimit(2), ioctl(2), lseek(2), open(2), pipe(2), ulimit(2), send(3SOCKET), socket(3SOCKET), attributes(5), lf64(5), standards(5), streamio(7I)