s6

Mirror/fork of https://skarnet.org/software/s6/
git clone https://ccx.te2000.cz/git/s6
Log | Files | Refs | README | LICENSE

ftrigr_checksa.c (1118B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <skalibs/stralloc.h>
      5 #include <skalibs/gensetdyn.h>
      6 #include <s6/ftrigr.h>
      7 
      8 int ftrigr_checksa (ftrigr_t *a, uint16_t id, stralloc *sa)
      9 {
     10   ftrigr1_t *p ;
     11   if (!id--) return (errno = EINVAL, -1) ;
     12   p = GENSETDYN_P(ftrigr1_t, &a->data, id) ;
     13   if (!p) return (errno = EINVAL, -1) ;
     14   switch (p->state)
     15   {
     16     case FR1STATE_WAITACKDATA :
     17     {
     18       if (!stralloc_catb(sa, p->what.s, p->what.len)) return -1 ;
     19       stralloc_free(&p->what) ;
     20       *p = ftrigr1_zero ;
     21       gensetdyn_delete(&a->data, id) ;
     22       return 1 ;
     23     }
     24     case FR1STATE_LISTENING :
     25     {
     26       if (!p->what.len) break ;
     27       if (!stralloc_catb(sa, p->what.s, p->what.len)) return -1 ;
     28       p->what.len = 0 ;
     29       return 1 ;
     30     }
     31     case FR1STATE_WAITACK :
     32     {
     33       int e ;
     34       if (!stralloc_catb(sa, p->what.s, p->what.len - 1)) return -1 ;
     35       e = p->what.s[p->what.len - 1] ;
     36       stralloc_free(&p->what) ;
     37       *p = ftrigr1_zero ;
     38       gensetdyn_delete(&a->data, id) ;
     39       errno = e ;
     40       return -1 ;
     41     }
     42     default: return (errno = EINVAL, -1) ;
     43   }
     44   return 0 ;
     45 }