nightmaremail

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

substdi.c (1525B)


      1 #include "substdio.h"
      2 #include "byte.h"
      3 #include "error.h"
      4 
      5 static ssize_t oneread(ssize_t (*op)(), int fd, char *buf, size_t len)
      6 {
      7   for (;;) {
      8     ssize_t r = op(fd,buf,len);
      9     if (r == -1) if (errno == error_intr) continue;
     10     return r;
     11   }
     12 }
     13 
     14 static int getthis(substdio *s, char *buf, int len)
     15 {
     16   int r;
     17   int q;
     18  
     19   r = s->p;
     20   q = r - len;
     21   if (q > 0) { r = len; s->p = q; } else s->p = 0;
     22   byte_copy(buf,r,s->x + s->n);
     23   s->n += r;
     24   return r;
     25 }
     26 
     27 ssize_t substdio_feed(substdio *s)
     28 {
     29   ssize_t r;
     30   int q;
     31 
     32   if (s->p) return s->p;
     33   q = s->n;
     34   r = oneread(s->op,s->fd,s->x,q);
     35   if (r == 0 || r == -1) return r;
     36   s->p = r;
     37   q -= r;
     38   s->n = q;
     39   if (q > 0) /* damn, gotta shift */ byte_copyr(s->x + q,r,s->x);
     40   return r;
     41 }
     42 
     43 #ifdef DEPRECATED_FUNCTIONS_AVAILABLE
     44 // might read fewer bytes than _get(); otherwise identical
     45 ssize_t substdio_bget(substdio *s, char *buf, size_t len)
     46 {
     47   ssize_t r;
     48  
     49   if (s->p > 0) return getthis(s,buf,len);
     50   r = s->n; if (r <= len) return oneread(s->op,s->fd,buf,r);
     51   r = substdio_feed(s);
     52   if (r == 0 || r == -1) return r;
     53   return getthis(s,buf,len);
     54 }
     55 #endif
     56 
     57 ssize_t substdio_get(substdio *s, char *buf, size_t len)
     58 {
     59   ssize_t r;
     60  
     61   if (s->p > 0) return getthis(s,buf,len);
     62   r = s->n; if (r <= len) return oneread(s->op,s->fd,buf,len);
     63   r = substdio_feed(s);
     64   if (r == 0 || r == -1) return r;
     65   return getthis(s,buf,len);
     66 }
     67 
     68 char *substdio_peek(substdio *s)
     69 {
     70   return s->x + s->n;
     71 }
     72 
     73 void substdio_seek(substdio *s, int len)
     74 {
     75   s->n += len;
     76   s->p -= len;
     77 }