WRITEV(3POSIX)

🫢 PROLOG 🫢

okay so real talk before anything else β€” this page lives in the POSIX Programmer's Manual. That's the whole vibe of it. But heads up bestie: the Linux implementation of this interface may differ (so if you want the Linux tea specifically, go consult the corresponding Linux manual page for details of Linux behavior), or honestly the interface may not even be implemented on Linux at all. πŸ’€ Not me warning you up front. Anyway.


✨ NAME ✨

writev β€” write a vector πŸ‘‘


🌈 SYNOPSIS 🌈

#include <sys/uio.h>

ssize_t writev(int fildes, const struct iovec *iov, int iovcnt);

πŸ’– DESCRIPTION πŸ’–

So writev() shall be equivalent to write(), except for the stuff we're about to talk about below. That's the deal.

What it does is gather output data from the iovcnt buffers that are specified by the members of the iov array: iov[0], iov[1], ..., iov[iovcnt-1]. Like, all of them, in order, no skipping. ✨

The iovcnt argument is valid if it's 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. And here's the part that's lowkey the whole point: writev() shall always write a complete area before proceeding to the next. No half-sends, no crumbs. πŸ’…

If fildes refers to a regular file and all of the iov_len members in the array pointed to by iov are 0, then writev() shall return 0 and have no other effect. But for other file types? The behavior is unspecified. πŸ™ˆ So don't come for it, it said what it said.

If the sum of the iov_len values is greater than {SSIZE_MAX}, the operation shall fail and no data shall be transferred. Periodt.


πŸŽ€ RETURN VALUE πŸŽ€

Upon successful completion, writev() shall return the number of bytes actually written. Iconic.

Otherwise, it shall return a value of -1, the file-pointer shall remain unchanged, and errno shall be set to indicate an error. 😭


πŸ”₯ ERRORS πŸ”₯

Refer to write().

In addition, the writev() function shall fail if:

  • EINVAL β€” the sum of the iov_len values in the iov array would overflow an ssize_t.

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}.
The following sections are informative. Which is a fancy way of saying: from here on it's ✨lore✨, not law. Still read it tho.

🌸 EXAMPLES 🌸

πŸ’œ Writing Data from an Array

This example writes data from the buffers specified by members of the iov array to the file associated with the file descriptor fd. Slay.

#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
...
ssize_t bytes_written;
int fd;
char *buf0 = "short string\n";
char *buf1 = "This is a longer string\n";
char *buf2 = "This is the longest string in this example\n";
int iovcnt;
struct iovec iov[3];

iov[0].iov_base = buf0;
iov[0].iov_len = strlen(buf0);
iov[1].iov_base = buf1;
iov[1].iov_len = strlen(buf1);
iov[2].iov_base = buf2;
iov[2].iov_len = strlen(buf2);
...
iovcnt = sizeof(iov) / sizeof(struct iovec);

bytes_written = writev(fd, iov, iovcnt);
...

πŸ¦‹ APPLICATION USAGE πŸ¦‹

None.


⭐ RATIONALE ⭐

Refer to write().


🧚 FUTURE DIRECTIONS 🧚

None.


πŸ“ SEE ALSO πŸ“

  • readv()
  • write()

The Base Definitions volume of POSIX.1‐2017, <limits.h>, <sys_uio.h>


So a chunk of this text is reprinted and reproduced in electronic form straight from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.

And fr fr: if there's any discrepancy between this version and the original IEEE and The Open Group Standard, then the original IEEE and The Open Group Standard is the referee document. It's the main character here, not us. You can grab the original Standard online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that show up in this page are most likely to have been introduced during the conversion of the source files to man page format β€” not a vibe, just a fact. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page bestie πŸ’– this one came to you from IEEE/The Open Group, dated 2017, and it's giving WRITEV(3POSIX) energy all the way through 🫢✨