runit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

buffer_get.c (1272B)


      1 /* Public domain. */
      2 
      3 #include "buffer.h"
      4 #include "byte.h"
      5 #include "error.h"
      6 
      7 static int oneread(buffer_op op,int fd,char *buf,unsigned int len)
      8 {
      9   int r;
     10 
     11   for (;;) {
     12     r = op(fd,buf,len);
     13     if (r == -1) if (errno == error_intr) continue;
     14     return r;
     15   }
     16 }
     17 
     18 static int getthis(buffer *s,char *buf,unsigned int len)
     19 {
     20   if (len > s->p) len = s->p;
     21   s->p -= len;
     22   byte_copy(buf,len,s->x + s->n);
     23   s->n += len;
     24   return len;
     25 }
     26 
     27 int buffer_feed(buffer *s)
     28 {
     29   int r;
     30 
     31   if (s->p) return s->p;
     32   r = oneread(s->op,s->fd,s->x,s->n);
     33   if (r <= 0) return r;
     34   s->p = r;
     35   s->n -= r;
     36   if (s->n > 0) byte_copyr(s->x + s->n,r,s->x);
     37   return r;
     38 }
     39 
     40 int buffer_bget(buffer *s,char *buf,unsigned int len)
     41 {
     42   int r;
     43  
     44   if (s->p > 0) return getthis(s,buf,len);
     45   if (s->n <= len) return oneread(s->op,s->fd,buf,s->n);
     46   r = buffer_feed(s); if (r <= 0) return r;
     47   return getthis(s,buf,len);
     48 }
     49 
     50 int buffer_get(buffer *s,char *buf,unsigned int len)
     51 {
     52   int r;
     53  
     54   if (s->p > 0) return getthis(s,buf,len);
     55   if (s->n <= len) return oneread(s->op,s->fd,buf,len);
     56   r = buffer_feed(s); if (r <= 0) return r;
     57   return getthis(s,buf,len);
     58 }
     59 
     60 char *buffer_peek(buffer *s)
     61 {
     62   return s->x + s->n;
     63 }
     64 
     65 void buffer_seek(buffer *s,unsigned int len)
     66 {
     67   s->n += len;
     68   s->p -= len;
     69 }