WRITE(2)
π WRITE(2) π β the System Calls Manual page, but make it cute
okay bestie, pull up a chair, we're talking about write(2) β the iconic one. this is the System Calls Manual entry, and by the end of this you're gonna be writing to file descriptors like it's nothing. periodt. β¨
πΈ NAME πΈ
write, writev, pwrite, pwritev β write output π
π LIBRARY π
Standard C Library (libc, -lc). yeah. the whole thing. iconic behavior.
β¨ SYNOPSIS β¨
#include <unistd.h>
ssize_t
write(int fd, const void *buf, size_t nbytes);
ssize_t
pwrite(int fd, const void *buf, size_t nbytes, off_t offset);
#include <sys/uio.h>
ssize_t
writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t
pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
look at them. four little function signatures. all of them ate. π
π¦ DESCRIPTION π¦
so the write() system call attempts to write nbytes of data to the object referenced by the descriptor fd, and that data comes from the buffer pointed to by buf. simple. clean. we love her.
the writev() system call does the exact same thing, but it gathers the output data from the iovcnt buffers specified by the members of the iov array β that's iov[0], iov[1], ..., all the way to iov[iovcnt-1]. it's giving β¨assembly lineβ¨.
and then pwrite() and pwritev()? they do the same functions, but they write to the specified position in the file without modifying the file pointer. so the file pointer is just... sitting there. unbothered. untouched. main character energy fr fr. π
for writev() and pwritev(), the iovec structure is defined as:
struct iovec {
void *iov_base; /* Base address. */
size_t iov_len; /* Length. */
};
every iovec entry specifies the base address and the length of an area in memory that data should be written from. and writev() will always write a complete area before proceeding to the next one. no skipping, no half-finished vibes. π«Ά
on objects capable of seeking, the write() starts at a position given by the pointer associated with fd β go check lseek(2), she explains it. and upon return from write(), that pointer is incremented by the number of bytes which were written. math. slay.
objects that are not capable of seeking always write from the current position. the value of the pointer associated with such an object is undefined. π
if the real user is not the super-user, then write() clears the set-user-id bit on a file. this prevents penetration of system security by a user who "captures" a writable set-user-id file owned by the super-user. so like, don't try it. π
when using non-blocking I/O on objects such as sockets that are subject to flow control, write() and writev() may write fewer bytes than requested. so you must note the return value, and the remainder of the operation should be retried when possible. this is the one where you have to actually pay attention, bestie. no vibing through this part. π
π₯ RETURN VALUES π₯
upon successful completion, the number of bytes which were written is returned. π
otherwise, a -1 is returned and the global variable errno is set to indicate the error. we hate to see it but we respect the honesty.
π ERRORS π
the write(), writev(), pwrite() and pwritev() system calls will fail and the file pointer will remain unchanged if:
[EBADF]β thefdargument is not a valid descriptor open for writing.[EPIPE]β an attempt is made to write to a pipe that is not open for reading by any process.[EPIPE]β an attempt is made to write to a socket of typeSOCK_STREAMthat is not connected to a peer socket.[EFBIG]β an attempt was made to write a file that exceeds the process's file size limit or the maximum file size.[EFAULT]β part ofiovor data to be written to the file points outside the process's allocated address space.[EINVAL]β the pointer associated withfdwas negative.[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.[EINTR]β a signal interrupted the write before it could be completed.[EAGAIN]β the file was marked for non-blocking I/O, and no data could be written immediately.[EROFS]β an attempt was made to write over a disk label area at the beginning of a slice. usedisklabel(8)-Wto enable writing on the disk label area.[EINVAL]β the valuenbytesis greater thanSSIZE_MAX(or greater thanINT_MAX, if the sysctldebug.iosize_max_clampis non-zero).[EINTEGRITY]β the backing store forfddetected corrupted data while reading. (for example, writing a partial filesystem block may require first reading the existing block which may trigger this error.)
in addition, writev() and pwritev() may return one of the following errors:
[EDESTADDRREQ]β the destination is no longer available when writing to a Unix domain datagram socket on whichconnect(2)had been used to set a destination address.[EINVAL]β theiovcntargument was less than or equal to 0, or greater thanIOV_MAX.[EINVAL]β one of theiov_lenvalues in theiovarray was negative.[EINVAL]β the sum of theiov_lenvalues in theiovarray overflowed a 32-bit integer.[ENOBUFS]β the mbuf pool has been completely exhausted when writing to a socket.
the pwrite() and pwritev() system calls may also return the following errors:
[EINVAL]β theoffsetvalue was negative.[ESPIPE]β the file descriptor is associated with a pipe, socket, or FIFO.
π SEE ALSO π
fcntl(2), lseek(2), open(2), pipe(2), select(2)
go read them. they're all in the group chat. π«Ά
π STANDARDS π
the write() system call is expected to conform to ISO/IEC 9945-1:1990 ("POSIX.1"). the writev() and pwrite() system calls are expected to conform to X/Open Portability Guide Issue 4, Version 2 ("XPG4.2"). certified. π
β HISTORY β
- the
pwritev()system call appeared inFreeBSD 6.0. - the
pwrite()function appeared inAT&T System V Release 4 UNIX. - the
writev()system call appeared in4.2BSD. - the
write()function appeared inVersion 1 AT&T UNIX.
like... the longevity?? the legacy?? we stan an OG. π
and that's the whole page, bestie. this came from the Debian build of the manual, dated March 30, 2020 β so if you're citing it, that's the version, and that's the date. now go forth and write() something gorgeous. π
β¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms