BIO_S_FILE(3ossl)
πΈ BIO_S_FILE(3ossl) πΈ
okay bestie so this is the BIO_s_file page from the OpenSSL manual, section 3ossl, and it's literally the whole vibe of "file BIOs" β like, the wrapper that makes a stdio FILE play nice inside OpenSSL's BIO world. ngl it's giving "small but mighty" π
β¨ 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 β the FILE bio fam π«Ά
π 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);
BIO_set_fp(BIO *b, FILE *fp, int flags);
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);
π¦ DESCRIPTION π¦
So BIO_s_file() hands you back the BIO file method. Like the name is literally telling you: it's a wrapper round the stdio FILE structure, and it's a source/sink BIO β so it can both feed data in and take data out π
Calls to BIO_read_ex() and BIO_write_ex() read and write data to the underlying stream. And yes, BIO_gets() and BIO_puts() are supported on file BIOs too. slay.
BIO_flush() on a file BIO calls the fflush() function on the wrapped stream. get that buffer outta here β¨
BIO_reset() attempts to change the file pointer to the start of file using fseek(stream, 0, 0). lowkey iconic behavior.
BIO_seek() sets the file pointer to position ofs from start of file using fseek(stream, ofs, 0).
BIO_eof() calls feof(). that's it, that's the whole function π
Setting the BIO_CLOSE flag calls fclose() on the stream when the BIO is freed. so like, cleanup duties handled π
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, no cap.
BIO_new_fp() creates a file BIO wrapping stream. Flags can be: BIO_CLOSE, BIO_NOCLOSE (the close flag), and BIO_FP_TEXT (sets the underlying stream to text mode, and default is binary β this only has any effect under Win32, iykyk).
BIO_set_fp() sets the fp of a file BIO to fp. The flags has the same meaning as in BIO_new_fp(), and heads up: it is a macro π
BIO_get_fp() retrieves the fp of a file BIO β also a macro, bestie.
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. main character energy for real.
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. pick your vibe π
π 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. like pls don't close stdin, that's so rude π
Because the file BIO calls the underlying stdio functions, any quirks in stdio behaviour will be mirrored by the corresponding BIO. inherited trauma 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 a 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 (although the current implementation never return0β cute).BIO_seek()returns0for success or negative values for failure.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. like, obviously π
π₯ EXAMPLES π₯
File BIO "hello world":
BIO *bio_out;
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_printf(bio_out, "Hello World\n");
Alternative technique:
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:
BIO *out;
out = BIO_new_file("filename.txt", "w");
if (!out)
/* Error */
BIO_printf(out, "Hello World\n");
BIO_free(out);
Alternative technique:
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 β and 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, inconsistent vibes, not me being dramatic, that's just the actual situation π
π 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)
π COPYRIGHT π
Copyright 2000-2020 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 the whole page, bestie π§ this was OpenSSL version 3.5.6, dated 2026-04-07, manual page BIO_S_FILE(3ossl) β go forth and wrap those FILEs π
β¨π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms