suitcase

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

leapsecs_read.c (935B)


      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 #include <fcntl.h>
      4 #include <errno.h>
      5 // extern int errno; // XXX Thread safety
      6 #include <tai/tai.h>
      7 #include <tai/leapsecs.h>
      8 
      9 struct tai *leapsecs = 0;
     10 int leapsecs_num = 0;
     11 
     12 int leapsecs_read()
     13 {
     14   int fd;
     15   struct stat st;
     16   struct tai *t;
     17   int n;
     18   int i;
     19   struct tai u;
     20 
     21   fd = open("/etc/leapsecs.dat",O_RDONLY | O_NDELAY);
     22   if (fd == -1) {
     23     if (errno != ENOENT) return -1;
     24     if (leapsecs) free(leapsecs);
     25     leapsecs = 0;
     26     leapsecs_num = 0;
     27     return 0;
     28   }
     29 
     30   if (fstat(fd,&st) == -1) { close(fd); return -1; }
     31 
     32   t = (struct tai *) malloc(st.st_size);
     33   if (!t) { close(fd); return -1; }
     34 
     35   n = read(fd,(char *) t,st.st_size);
     36   close(fd);
     37   if (n != st.st_size) { free(t); return -1; }
     38 
     39   n /= sizeof(struct tai);
     40 
     41   for (i = 0;i < n;++i) {
     42     tai_unpack((char *) &t[i],&u);
     43     t[i] = u;
     44   }
     45 
     46   if (leapsecs) free(leapsecs);
     47 
     48   leapsecs = t;
     49   leapsecs_num = n;
     50 }