s6

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

s6-tai64nlocal.c (1554B)


      1 /* ISC license. */
      2 
      3 #include <sys/types.h>
      4 #include <errno.h>
      5 #include <skalibs/buffer.h>
      6 #include <skalibs/strerr.h>
      7 #include <skalibs/sgetopt.h>
      8 #include <skalibs/tai.h>
      9 #include <skalibs/djbtime.h>
     10 #include <skalibs/stralloc.h>
     11 #include <skalibs/skamisc.h>
     12 
     13 #define USAGE "s6-tai64nlocal [ -g ]"
     14 
     15 int main (int argc, char const *const *argv)
     16 {
     17   int islocal = 1 ;
     18   PROG = "s6-tai64nlocal" ;
     19   {
     20     subgetopt l = SUBGETOPT_ZERO ;
     21     for (;;)
     22     {
     23       int opt = subgetopt_r(argc, argv, "g", &l) ;
     24       if (opt == -1) break ;
     25       switch (opt)
     26       {
     27         case 'g' : islocal = 0 ; break ;
     28         default : strerr_dieusage(100, USAGE) ;
     29       }
     30     }
     31     argc -= l.ind ; argv += l.ind ;
     32   }
     33 
     34   for (;;)
     35   {
     36     unsigned int p = 0 ;
     37     int r = skagetln(buffer_0f1, &satmp, '\n') ;
     38     if (r == -1)
     39       if (errno != EPIPE)
     40         strerr_diefu1sys(111, "read from stdin") ;
     41       else r = 1 ;
     42     else if (!r) break ;
     43     if (satmp.len > TIMESTAMP)
     44     {
     45       tain a ;
     46       p = timestamp_scan(satmp.s, &a) ;
     47       if (p)
     48       {
     49         localtmn local ;
     50         if (localtmn_from_tain(&local, &a, islocal))
     51         {
     52           char fmt[LOCALTMN_FMT+1] ;
     53           size_t len = localtmn_fmt(fmt, &local) ;
     54           if (buffer_put(buffer_1, fmt, len) < (ssize_t)len)
     55             strerr_diefu1sys(111, "write to stdout") ;
     56         }
     57         else p = 0 ;
     58       }
     59     }
     60     if (buffer_put(buffer_1, satmp.s + p, satmp.len - p) < (ssize_t)(satmp.len - p))
     61       strerr_diefu1sys(111, "write to stdout") ;
     62     satmp.len = 0 ;
     63   }
     64   return 0 ;
     65 }