BIO_S_FILE(3SSL)
π BIO_S_FILE(3SSL) β the FILE bio glow-up π
okay bestie, sit down, hydrate, and let me tell you about BIO_s_file and its whole friend group, because this is the OpenSSL manual page about FILE bios and it's lowkey iconic. πΈ
β¨ NAME β¨
BIO_s_file, BIO_new_file, BIO_new_fp, BIO_set_fp, BIO_get_fp, BIO_read_filename, BIO_write_filename, BIO_append_filename, BIO_rw_filename β it's giving FILE bio, periodt. π
β¨ SYNOPSIS β¨
#include <openssl/bio.h>
const BIO_METHOD *BIO_s_file(void);
BIO *BIO_new_file(const char *filename, const char *mode);
BIO *BIO_new_fp(FILE *stream, int flags);
long BIO_set_fp(BIO *b, FILE *fp, int flags);
long BIO_get_fp(BIO *b, FILE **fpp);
int BIO_read_filename(BIO *b, char *name);
int BIO_write_filename(BIO *b, char *name);
int BIO_append_filename(BIO *b, char *name);
int BIO_rw_filename(BIO *b, char *name);
like look at that little list of functions, so organized, so cunty, we love to see it π«Ά
β¨ DESCRIPTION β¨
BIO_s_file() hands you back the BIO file method. and like, the name is not lying to you bestie β it's a wrapper round the stdio FILE structure, and it is a source/sink BIO. π
calls to BIO_read_ex() and BIO_write_ex() read and write data to the underlying stream. BIO_gets() and BIO_puts() are supported on file BIOs too. slay. β¨
BIO_flush()on a file BIO calls thefflush()function on the wrapped stream.BIO_reset()attempts to change the file pointer to the start of file usingfseek(stream, 0, 0).BIO_seek()sets the file pointer to positionofsfrom start of file usingfseek(stream, ofs, 0).BIO_eof()callsfeof().- setting the
BIO_CLOSEflag callsfclose()on the stream when the BIO is freed. π
BIO_new_file() creates a new file BIO with mode mode β and the meaning of mode is the same as the stdio function fopen(). the BIO_CLOSE flag is set on the returned BIO. β
BIO_new_fp() creates a file BIO wrapping stream. the flags can be:
BIO_CLOSEBIO_NOCLOSEβ that's the close flag, iykykBIO_FP_TEXTβ sets the underlying stream to text mode, default is binary; this only has any effect under Win32 π
BIO_set_fp() sets the fp of a file BIO to fp. flags has the same meaning as in BIO_new_fp(), and bestie it is a macro. π
BIO_get_fp() retrieves the fp of a file BIO β also a macro. π
BIO_seek() is a macro that sets the position pointer to offset bytes from the start of file.
BIO_tell() returns the value of the position pointer. π
BIO_read_filename(), BIO_write_filename(), BIO_append_filename() and BIO_rw_filename() set the file BIO b to use file name for reading, writing, append or read write respectively. one function per vibe, we stan a specialization moment. π
β¨ NOTES β¨
when you're wrapping stdout, stdin or stderr, the underlying stream should not normally be closed, so the BIO_NOCLOSE flag should be set. don't be out here closing your own stdout bestie π
because the file BIO calls the underlying stdio functions, any quirks in stdio behaviour will be mirrored by the corresponding BIO. so like, whatever stdio is going through, your BIO is going through it too. fr fr. π
On Windows, BIO_new_files reserves for the filename argument to be UTF-8 encoded. in other words, if you have to make it work in multi- lingual environment, encode filenames in UTF-8. π
β¨ RETURN VALUES β¨
BIO_s_file()returns the file BIO method.BIO_new_file()andBIO_new_fp()return a file BIO orNULLif an error occurred. π«BIO_set_fp()andBIO_get_fp()return1for success or<=0for failure.BIO_seek()returns0for success or negative values for failure. (yes,0, we'll get to that, don't worry)BIO_tell()returns the current file position or negative values for failure.BIO_read_filename(),BIO_write_filename(),BIO_append_filename()andBIO_rw_filename()return1for success or<=0for failure. an error is also returned if the file does not exist. π
β¨ EXAMPLES β¨
file BIO "hello world", the classic, the blueprint π
BIO *bio_out;
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_printf(bio_out, "Hello World\n");
alternative technique, because we love options bestie β¨
BIO *bio_out;
bio_out = BIO_new(BIO_s_file());
if (bio_out == NULL)
/* Error */
if (BIO_set_fp(bio_out, stdout, BIO_NOCLOSE) <= 0)
/* Error */
BIO_printf(bio_out, "Hello World\n");
write to a file, main character energy only π
BIO *out;
out = BIO_new_file("filename.txt", "w");
if (!out)
/* Error */
BIO_printf(out, "Hello World\n");
BIO_free(out);
alternative technique, part two, the sequel π¦
BIO *out;
out = BIO_new(BIO_s_file());
if (out == NULL)
/* Error */
if (BIO_write_filename(out, "filename.txt") <= 0)
/* Error */
BIO_printf(out, "Hello World\n");
BIO_free(out);
β¨ BUGS β¨
BIO_reset() and BIO_seek() are implemented using fseek() on the underlying stream. the return value for fseek() is 0 for success or -1 if an error occurred β this differs from other types of BIO which will typically return 1 for success and a non positive value if an error occurred. so yeah, don't get got by the 0-means-success thing, it's a whole entirely different vibe from the rest of the BIOs. π not a drill.
β¨ SEE ALSO β¨
BIO_seek(3), BIO_tell(3), BIO_reset(3), BIO_flush(3), BIO_read_ex(3), BIO_write_ex(3), BIO_puts(3), BIO_gets(3), BIO_printf(3), BIO_set_close(3), BIO_get_close(3)
go read those too, the lore is deep π
β¨ COPYRIGHT β¨
Copyright 2000-2026 The OpenSSL Project Authors. All Rights Reserved. π
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>. π
and that's a wrap bestie π this was OpenSSL 4.0.3, dated 2026-09-29, the BIO_S_FILE(3SSL) era β thanks for reading the whole thing, you ate, no crumbs. π
β¨π«Ά
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms