Embedding files in C programs with koio May 29, 2018 on Drew DeVault's blog

Quick blog post today to introduce a new tool I wrote: koio. This is a small tool which takes a list of files and embeds them in a C file. A library provides an fopen shim which checks the list of embedded files before resorting to the real filesystem.

I made this tool for chopsui, where I eventually want to be able to bundle up sui markup, stylesheets, images, and so on in a statically linked chopsui program. Many projects have small tools which serve a similar purpose, but it was simple enough and useful enough that I chose to make something generic so it could be used on several projects.

The usage is pretty simple. I can embed ko_fopen.c in a C file with this command:

$ koio -o bundle.c ko_fopen.c://ko_fopen.c

I can compile and link with bundle.c and do something like this:

#include <koio.h>

void koio_load_assets(void);
void koio_unload_assets(void);

int main(int argc, char **argv) {
    FILE *src = ko_fopen("//ko_fopen.c", "r");
    int c;
    while ((c = fgetc(src)) != EOF) {
    return 0;

The generated bundle.c looks like this:

#include <koio.h>

static struct {
	const char *path;
	size_t len;
	char *data;
} files[] = {
		.path = "//ko_fopen.c",
		.len = 408,
		.data =
"#define _POSIX_C_SOURCE 200809L\n#include <errno.h>\n#include <stdlib.h>\n#inc"
"lude <stdio.h>\n#include \"koio_private.h\"\n\nFILE *ko_fopen(const char *path"
", const char *mode) {\n\tstruct file_entry *entry = hashtable_get(&koio_vfs, p"
"ath);\n\tif (entry) {\n\t\tif (mode[0] != 'r' || mode[1] != '\\0') {\n\t\t\ter"
"rno = ENOTSUP;\n\t\t\treturn NULL;\n\t\t}\n\t\treturn fmemopen(entry->data, en"
"try->len, \"r\");\n\t}\n\treturn fopen(path, mode);\n}\n",

void koio_load_assets(void) {
	ko_add_file(files[0].path, files[0].data, files[0].len);

void koio_unload_assets(void) {

A very simple tool, but one that I hope people will find useful. It’s very lightweight:


Have a comment on one of my posts? Start a discussion in my public inbox by sending an email to ~sircmpwn/public-inbox@lists.sr.ht [mailing list etiquette]

Articles from blogs I read Generated by openring

Command PATH security in Go

Today’s Go security release fixes an issue involving PATH lookups in untrusted directories that can lead to remote execution during the go get command. We expect people to have questions about what exactly this means and whether they might h…

via The Go Programming Language Blog January 19, 2021

Status update, January 2021

Hi all! This month again, my main focus has been wlroots. I’ve focused on the internal renderer refactoring (the so-called “renderer v6"). A lot of the work has now been completed, and all backends now use the new interfaces under-the-hood. With the help …

via emersion January 18, 2021

What's cooking on Sourcehut? January 2021

Another year begins, and hopefully with better prospects for us all. SourceHut has emerged from 2020 relatively unscathed, thankfully, and I hope the same is true of most of our users. A body which, by the way, today numbers 19,647 strong, up 623 from Decemb…

via Blogs on Sourcehut January 15, 2021