#include "uffer.h"

       buffer* buffer_0;   /* like stdio's stdin */
       buffer* buffer_1;   /* like stdio's stdout */
       buffer* buffer_2;   /* like stdio's stderr */

       void buffer_init(buffer &b,ssize_t (*op)(int,char*,size_t),
                       int fd, char* y, size_t ylen);
       ssize_t buffer_get(buffer* b,char* x,size_t len);

       int buffer_put(buffer* b,const char* x,size_t len);
       int buffer_putalign(buffer* b,char* x,unsigned int len);
       int buffer_putsalign(buffer* b,char* x);


       buffer.h describes a generic buffer interface that can be used for read
       and write buffering. Buffers must be initialized with buffer_init.

       A buffer can only be used for reading or writing at the same time, not

       Unlike stdio, these write buffers are not flushed automatically at
       program termination; you must manually call buffer_flush,
       buffer_putsflush, buffer_putflush or buffer_putnlflush.

       buffer_init prepares b to store a string in y[0], y[1], ..., y[ylen-1].
       Initially the string is empty.

       buffer_init also prepares b to use the read/write operation specified
       by op and fd.

       You can use

         buffer b = BUFFER_INIT(op,fd,y,ylen);

       to initialize b statically if op, fd, y, and ylen are compile-time

       You can call buffer_init again at any time. Note that this discards the
       currently buffered string.

       buffer_get copies data to x[0], x[1], ..., x[len-1] from the beginning
       of a string stored in preallocated space; removes these len bytes from
       the string; and returns len.

       If, however, the string has fewer than len (but more than 0) bytes,
       buffer_get copies only that many bytes, and returns that number.

       If the string is empty, buffer_get first uses a read operation to feed
       data into the string. The read operation may indicate end of input.
         #include <open.h>

         char buf[4096];
         int fd = open_read("/etc/services");
         buffer input;

         if (fd >= 0) {
           char x;
           buffer_init(&input,read,fd,buf,sizeof buf);
           while (buffer_get(&input,&x,1) == 1) {
             if (x == '\n') break;


       buffer_put and buffer_get return 0 if everything was fine, -1 on error
       (setting errno).




Man(1) output converted with man2html