summaryrefslogtreecommitdiff
path: root/src/libstddjb/leapsecs_init.c
blob: 41f9b892baf35a5ea6481a07928f7270c9848f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* ISC license. */

#include <errno.h>
#include <skalibs/uint64.h>
#include <skalibs/djbunix.h>
#include "djbtime-internal.h"

static unsigned int leapsecs_len (uint64 const *data)
{
  register unsigned int i = 1 ;
  while (data[i]) i++ ;
  return i ;
}

static int leapsecs_read (char const *file, uint64 *data)
{
  char s[LEAPSECS_MAX * sizeof(uint64)] ;
  register int n = openreadnclose(file, s, LEAPSECS_MAX * sizeof(uint64)) ;
  if (n < 0) return -1 ;
  if (n % sizeof(uint64)) return (errno = EINVAL, -1) ;
  n /= sizeof(uint64) ;
  {
    register unsigned int i = 0 ;
    for (; i < (unsigned int)n ; i++)
      uint64_unpack_big(s + i * sizeof(uint64), data + i) ;
  }
  data[n] = 0 ;
  return n ;
}

int leapsecs_init_r (char const *file, uint64 *data)
{
  return data[0] ? (int)leapsecs_len(data) : leapsecs_read(file, data) ;
}