From 8e736672475377d63007b26b58b7a5c6af335e40 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Tue, 15 Mar 2011 13:59:34 +0000 Subject: initial commit --- .gitignore | 2 ++ Makefile | 14 +++++++++++++ lua-posixtz.c | 27 ++++++++++++++++++++++++ main.c | 23 ++++++++++++++++++++ posixtz.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ posixtz.h | 6 ++++++ 6 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 lua-posixtz.c create mode 100644 main.c create mode 100644 posixtz.c create mode 100644 posixtz.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52a68ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +posixtz +posixtz.so diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4370b38 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +LUALIBS := -llua + +TARGETS := posixtz posixtz.so +all: $(TARGETS) + +clean: + rm -f $(TARGETS) + +posixtz: main.c posixtz.c + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ + +posixtz.so: lua-posixtz.c posixtz.c + $(CC) -fPIC $(CFLAGS) $(LDFLAGS) $(LUALIBS) -shared -o $@ $^ + diff --git a/lua-posixtz.c b/lua-posixtz.c new file mode 100644 index 0000000..0754f97 --- /dev/null +++ b/lua-posixtz.c @@ -0,0 +1,27 @@ + +#include +#include +#include +#include "posixtz.h" + +#define MYNAME "posixtz" + +static int Pfrom_file(lua_State *L) +{ + const char *s = luaL_checkstring(L, 1); + lua_pushstring(L, posix_tz(s)); + return 1; +} + +static const luaL_reg R[] = +{ + {"from_file", Pfrom_file}, + NULL, NULL +}; + + +LUALIB_API int luaopen_posixtz(lua_State *L) +{ + luaL_register(L,MYNAME,R); + return 1; +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..9e78c04 --- /dev/null +++ b/main.c @@ -0,0 +1,23 @@ +#include +#include "posixtz.h" + +int usage() { + printf("Print POSIX TZ string from compiled tzdata\n" + "Usage: posixtz FILE...\n"); + return -1; +} + +int main(int argc, char *argv[]) +{ + int i=1; + if (argc <= 1) + return usage(); + + for (i = 1; i < argc; i++) { + char *s = posix_tz(argv[i++]); + if (s != NULL) + printf("%s\n", s); + } + return 0; +} + diff --git a/posixtz.c b/posixtz.c new file mode 100644 index 0000000..cddcb3e --- /dev/null +++ b/posixtz.c @@ -0,0 +1,67 @@ +/* ripped from uclibc +* +* Copyright (C) 2010 Denys Vlasenko +* Copyright (C) 2011 Natanael Copa +* +* GNU Library General Public License (LGPL) version 2 or later. +* +*/ + +#include +#include + +#include +#include +#include +#include + +#include "posixtz.h" + +#ifndef TZNAME_MAX +#define TZNAME_MAX _POSIX_TZNAME_MAX +#endif + +#define TZ_BUFLEN (2*TZNAME_MAX + 56) + +char *posix_tz(const char *filename) +{ + int fd, r; + static char buf[TZ_BUFLEN]; + char *p = NULL; + fd = open(filename, O_RDONLY); + if (fd < 0) + return NULL; + + r = read(fd, buf, TZ_BUFLEN); + if (r != TZ_BUFLEN + || strncmp(buf, "TZif", 4) != 0 + || (unsigned char)buf[4] < 2 + || lseek(fd, -TZ_BUFLEN, SEEK_END) < 0 + ) + goto ERROR; + + /* tzfile.h from tzcode database says about TZif2+ files: + ** + ** If tzh_version is '2' or greater, the above is followed by a second instance + ** of tzhead and a second instance of the data in which each coded transition + ** time uses 8 rather than 4 chars, + ** then a POSIX-TZ-environment-variable-style string for use in handling + ** instants after the last transition time stored in the file + ** (with nothing between the newlines if there is no POSIX representation for + ** such instants). + */ + r = read(fd, buf, TZ_BUFLEN); + if (r <= 0 || buf[--r] != '\n') + goto ERROR; + buf[r] = 0; + while (r != 0) { + if (buf[--r] == '\n') { + p = buf + r + 1; + break; + } + } /* else ('\n' not found): p remains NULL */ +ERROR: + close(fd); + return p; +} + diff --git a/posixtz.h b/posixtz.h new file mode 100644 index 0000000..3133723 --- /dev/null +++ b/posixtz.h @@ -0,0 +1,6 @@ +#ifndef POSIXTZ_H +#define POSIXTZ_H + +char *posix_tz(const char *filename); + +#endif -- cgit v1.2.3