KCGI_BUF_WRITE(3) Library Functions Manual KCGI_BUF_WRITE(3)


kcgi_buf_putc, kcgi_buf_puts, kcgi_buf_writebuffer functions for kcgi


library “libkcgi”


#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
enum kcgi_err
kcgi_buf_putc(struct kcgi_buf *buf, char c);
enum kcgi_err
kcgi_buf_puts(struct kcgi_buf *buf, const char *cp);
enum kcgi_err
kcgi_buf_write(const char *cp, size_t sz, void *arg);


The kcgi_buf_putc(), kcgi_buf_puts(), and kcgi_buf_write() functions append to a buffer object buf, whose data buffer buf->buf is guaranteed to be NUL terminated if buf->sz is greater than zero.
The kcgi_buf_putc() function appends a single character; kcgi_buf_puts() appends a NUL-terminated character string (not including the NUL terminator); and kcgi_buf_write() appends a binary buffer buf of size sz, where arg is cast to a struct kcgi_buf. The latter is often used in the khttp_templatex(3) family of functions.
The struct kcgi_buf structure has the following fields:
char *buf
Buffer contents. This is always NUL-terminated, but may contain NUL characters prior to the stated length.
size_t maxsz
Allocated buffer size.
size_t sz
Buffer current length not including the NUL terminator.
size_t growsz
Amount to grow the buffer when appending. If set to zero, defaults to 1 KB.
The struct kcgi_buf must be zeroed prior to usage, and the contained buffer passed to free(3) when no longer used.


The following creates a simple NUL-terminated buffer. It does not perform any error checking.
struct kcgi_buf buf; 
memset(&buf, 0, sizeof(struct kcgi_buf)); 
kcgi_buf_puts(&buf, "Hello,"); 
kcgi_buf_putc(&buf, ' '); 
kcgi_buf_puts(&buf, "world!"); 
puts(buf.buf); /* Emits "Hello, world!" */ 


These functions return an enum kcgi_err indicating the error state: KCGI_OK for no error, KCGI_ENOMEM for an internal memory allocation failure.




These functions were written by Kristaps Dzonsons <kristaps@bsd.lv>.
March 5, 2018 OpenBSD 6.2