DynDN.eS Blog

About DynDN.eS, eQmail, Gentoo & some other network stuff

User Tools

Site Tools


replace substdio with buffer

On later software djb changed some of its initial libraries and published it together with its software. So from round about the year 2000 on he replaced substdio with buffer. The differences are not as big at it seems on the first view. Low level functions are similar. Additional buffer provides some functions on a higher level which are easier to use in code:  buffer_0 ,  buffer_1  and  buffer_2 . The numbers stands for stdin, stdout and stderr, respective they could be used for read/write operations.

Now the time was going on, things changed but the routines from djb are still usefull. Below I will show some example how substdio could be reaplaced by buffer easily in older (djb) code. Beware to do  #include “buffer.h”  and link  buffer.a  in the Makefile.

First variables of type buffer have to be defined (in code):

 buffer bin;    // replacement for: substdio ss;
 buffer bout;   // replacement for: substdio ssout;

The types substdio and buffer are defined as  struct  but they are somewhat different!

Replacements

The first line has to be replaced by the second one. The last ones are surrounded by simple statements.

1. Initialize an input buffer (read):

 substdio_fdbuf(&ss, read,0,buf,sizeof(buf));
   buffer_init (&bin,read,0,buf,sizeof(buf));

2. Initialize an output buffer (write), where  fd  is the number (integer) of the file descriptor:

 substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));
   buffer_init (&bout, write,fd,outbuf,sizeof(outbuf));

3. Read from buffer:

 if (substdio_get(&ss ,dtline.s,dtline.len) == -1) goto fail;
 if   (buffer_get(&bin,dtline.s,dtline.len) == -1) goto fail;

4. Write to buffer:

 if (substdio_put(&ssout,dtline.s,dtline.len) == -1) goto fail;
 if   (buffer_put(&bout, dtline.s,dtline.len) == -1) goto fail;

5. Copy input buffer to output buffer:

 substdio_copy(&ssout,&ss);
   buffer_copy(&bout ,&bin);

6. The memory will not be released by automatically, so it have to be flushed:

 if (substdio_flush(&ssout) == -1) goto fail;
 if   (buffer_flush(&bout)  == -1) goto fail;

All these examples should cover most use cases. In some rarely cases - which are not covered here - it could be necessary to refer to  buffer.h  or further documentation.

References:


Page Tools