SETVBUF(3POSIX)

๐ŸŒธ SETVBUF โ€” ok so you wanna control your stream's buffering ๐Ÿ’…

setvbuf, from the POSIX Programmer's Manual. Buckle up bestie, this one's lowkey iconic.

โœจ PROLOG โœจ

Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. Not me warning you upfront ๐Ÿ’€


๐Ÿ‘‘ NAME ๐Ÿ‘‘

setvbuf โ€” assign buffering to a stream. That's it. That's the job. Periodt.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <stdio.h>

int setvbuf(FILE *restrict stream, char *restrict buf, int type,
    size_t size);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

Okay so the functionality described on this reference page is aligned with the ISO C standard. Any conflict between what's described here and the ISO C standard is unintentional โ€” no drama intended. This volume of POSIX.1โ€2017 defers to the ISO C standard. Respect the elders ๐Ÿ’–

So, the setvbuf() function may be used after the stream pointed to by stream is associated with an open file, but before any other operation (other than an unsuccessful call to setvbuf()) is performed on the stream. Timing is everything, iykyk.

The argument type determines how stream shall be buffered, as follows:

  • {_IOFBF} โ€” shall cause input/output to be fully buffered. All the buffering. Main character energy.
  • {_IOLBF} โ€” shall cause input/output to be line buffered. One line at a time, cute and tidy.
  • {_IONBF} โ€” shall cause input/output to be unbuffered. Raw. Unfiltered. No cap.

If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by setvbuf(), and the argument size specifies the size of the array; otherwise, size may determine the size of a buffer allocated by the setvbuf() function. And heads up: the contents of the array at any time are unspecified. ๐Ÿ™ˆ

For info about streams, see Section 2.5, Standard I/O Streams.


โญ RETURN VALUE โญ

Upon successful completion, setvbuf() shall return 0. Otherwise, it shall return a non-zero value if an invalid value is given for type or if the request cannot be honored, and may set errno to indicate the error. It's giving "something went wrong, good luck figuring out what" ๐Ÿ˜ญ


๐Ÿ’€ ERRORS ๐Ÿ’€

The setvbuf() function may fail if:

  • EBADF โ€” The file descriptor underlying stream is not valid.
The following sections are informative. (Yes, that's the actual disclaimer, slay.)

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

None. Yeah. Nothing. Empty. The void. ๐Ÿซถ


๐Ÿ“ APPLICATION USAGE ๐Ÿ“

A common source of error is allocating buffer space as an ``automatic'' variable in a code block, and then failing to close the stream in the same block. Bestie. Please. Do not do this.

With setvbuf(), allocating a buffer of size bytes does not necessarily imply that all of size bytes are used for the buffer area. Rude, but true.

Applications should note that many implementations only provide line buffering on input from terminal devices. So don't come crying when your line buffering doesn't line buffer everywhere. ๐Ÿงš


๐Ÿง  RATIONALE ๐Ÿง 

None. Nothing to explain, we said what we said. ๐Ÿ’…


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

None. The future is unspecified. Very on brand. โœจ


๐Ÿซถ SEE ALSO ๐Ÿซถ

  • Section 2.5, Standard I/O Streams
  • fopen()
  • setbuf()

The Base Definitions volume of POSIX.1โ€2017, <stdio.h> ๐Ÿ‘‘


Portions of this text are reprinted and reproduced in electronic form 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. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


okay that's the whole page, love u, go buffer responsibly ๐ŸŽ€

Brought to you by IEEE/The Open Group, 2017, on the SETVBUF(3POSIX) page. That's a wrap bestie ๐Ÿ’œ

๐Ÿ“ฆ where this page comes from