aboutsummaryrefslogtreecommitdiff
path: root/riemann.fmi.uni-sofia.bg/programs/nlogin-0.3-pre/get_tz.c
blob: 94b42e1d950ec3ce9e09c0abc1ef61fe6259976a (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
35
36
37
38
39
40
41
42
#include <unistd.h>
#include <fcntl.h>
#include "parselib.h"

static struct state s;

static int parse(unsigned char *x) 
  {return (x[0]<<24) | (x[1]<<16) | (x[2]<<8) | x[3];}

static time_t time_offset(time_t *t, int forward) {
  unsigned char *T = (unsigned char *)s.buffirst, *tmp,*x;
  size_t L = s.buflen;
  time_t i, k, res, tzh_timecnt;
  if (!T || L<44) return 0;
  tzh_timecnt = parse(T+32);
  tmp=T+44;
  x=tmp+tzh_timecnt*4;
  for (i=tzh_timecnt-1; 0<=i; --i) {
    k = parse(tmp+i*4);
    if (forward) if (*t < k) continue;

    res = parse(x +tzh_timecnt +6*(unsigned char)x[i]);

    if (forward) return res;
    else 
      if (k+res <= *t) return -res;
  }
  return 0;
}

time_t get_tz(time_t *t, int forward) /*EXTRACT_INCL*/{
  if (forward==-1) {
    __end_parse(&s);
    s.cur=0;
    return 0;
  }
  if (!s.cur) {
    __prepare_parse("/etc/localtime",&s);
    s.cur=1;    
  }
  return time_offset(t,forward);
}