From patchwork Thu Apr 3 04:28:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: P J P X-Patchwork-Id: 394 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx21.g.dreamhost.com (peon2454.g.dreamhost.com [208.113.200.127]) by wilcox.dreamhost.com (Postfix) with ESMTP id B4F7636007C for ; Wed, 2 Apr 2014 21:31:04 -0700 (PDT) Received: by homiemail-mx21.g.dreamhost.com (Postfix, from userid 14307373) id 3E52BDB2B87; Wed, 2 Apr 2014 21:31:04 -0700 (PDT) X-Original-To: glibc@patchwork.siddhesh.in Delivered-To: x14307373@homiemail-mx21.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx21.g.dreamhost.com (Postfix) with ESMTPS id 14E7ADB2BB8 for ; Wed, 2 Apr 2014 21:31:04 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:references:message-id:date:from:reply-to :subject:to:mime-version:content-type:content-transfer-encoding; q=dns; s=default; b=qtpQEw0Nc1DNBdB9xRUz5BxSGBPO5yk3tyESi8VPmvY 8oV/TVzwl1sh5qkFdJ/4wVAA1ykJwjfZagQSvtfDKVFdg+n2qOJ2z6ZLAdDBtE1y 6bkvvxfDGZ80HnWD/bvclkyX/3y/WUYTdNM5D9pJqLk8aDQdnXRlrrLUoLTlcCM8 = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:references:message-id:date:from:reply-to :subject:to:mime-version:content-type:content-transfer-encoding; s=default; bh=V9WPjMQIY7Ym19PNsjTzAaedSCc=; b=QRrnEGN1Y9pVyChIl rNXOMEGzny1tavajkRfmqfEVMkRxkgXl8GwvnYs+2h/EYL68nGAgnx1H1KMu1zWs gcqdmqniPS5Is5rl10ZqhYQimxIl1ZV0dtGxYXuQjS5NLECBs+QUalLYuv6gC/L7 1aR3EjASQ/IgI5HtvNQSwGd2F8= Received: (qmail 1912 invoked by alias); 3 Apr 2014 04:31:01 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 32584 invoked by uid 89); 3 Apr 2014 04:31:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-HELO: nm43.bullet.mail.ne1.yahoo.com References: Message-ID: <1396499286.85118.YahooMailNeo@web192405.mail.sg3.yahoo.com> Date: Thu, 3 Apr 2014 12:28:06 +0800 (SGT) From: P J P Reply-To: P J P Subject: RFC [PATCH] BZ#1077902: New API gettimezone To: "libc-alpha@sourceware.org" MIME-Version: 1.0 X-DH-Original-To: glibc@patchwork.siddhesh.in Hello, WRT BZ#1077902 [1], please see the patch below to introduce a new API gettimezone(). It builds successfully with the glibc Makefile, though I could not test the API from the new library. It uses TZ_VERSION & TZ_FILE macros because they aren't defined in timezone/tzfile.h. There is #defines TZDEFAULT "localtime", but that is not sufficient to access '/etc/localtime'. There is also #define TZDIR /usr/local/etc/zoneinfo, which seems misplaced. On f19 machine, the zoneinfo is stored under /usr/share/zoneinfo. === --- [1] https://bugzilla.redhat.com/show_bug.cgi?id=1077902#c2 Thank you. --- Regards    -Prasad http://feedmug.com === diff --git a/ChangeLog b/ChangeLog index 7e530ef..e2cdbcf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2014-04-03  P J P   + +       [BZ #1077902] +       * time/gettimezone.c: new API to query POSIX TZ variable string. +  2014-04-02  Joseph Myers           [BZ #16799] diff --git a/time/Makefile b/time/Makefile index b7f3dba..8bcc5ca 100644 --- a/time/Makefile +++ b/time/Makefile @@ -31,7 +31,7 @@ routines := offtime asctime clock ctime ctime_r difftime \             stime dysize timegm ftime                    \             getdate strptime strptime_l                  \             strftime wcsftime strftime_l wcsftime_l      \ -           timespec_get +           timespec_get gettimezone  aux :=     era alt_digit lc-time-cleanup  tests  := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ diff --git a/time/Versions b/time/Versions index fd83818..957d2bf 100644 --- a/time/Versions +++ b/time/Versions @@ -25,7 +25,7 @@ libc {      ftime;      # g* -    getitimer; gettimeofday; gmtime; gmtime_r; +    getitimer; gettimeofday; gmtime; gmtime_r; gettimezone;      # l*      localtime; localtime_r; diff --git a/time/gettimezone.c b/time/gettimezone.c new file mode 100644 index 0000000..88b2141 --- /dev/null +++ b/time/gettimezone.c @@ -0,0 +1,109 @@ +/* Copyright (C) 1991-2014 Free Software Foundation, Inc. +   This file is part of the GNU C Library. + +   The GNU C Library is free software; you can redistribute it and/or +   modify it under the terms of the GNU Lesser General Public +   License as published by the Free Software Foundation; either +   version 2.1 of the License, or (at your option) any later version. + +   The GNU C Library is distributed in the hope that it will be useful, +   but WITHOUT ANY WARRANTY; without even the implied warranty of +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU +   Lesser General Public License for more details. + +   You should have received a copy of the GNU Lesser General Public +   License along with the GNU C Library; if not, see +   .  */ + +#include +#include +#include +#include +#include +#include +#include + +#define TZ_VERSION '2' +#define TZ_FILE    "/etc/"TZDEFAULT + + +/* gettimezone: reads local timezone definition from '/etc/localtime' +   and returns a POSIX TZ environment variable string or NULL in case +   of an error; See: tzfile(5), tzset(3). + +       std offset dst [offset],start[/time],end[/time] + +   Ex: TZ="NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0"  */ +char * +gettimezone (const char *tzname) +{ +    int n = 0; +    FILE *fp = NULL; +    char *tz = NULL, s[4]; + +    errno = 0; +    if (!tzname) +        tzname = TZ_FILE; + +    fp = fopen (tzname, "rce"); +    if (!fp) +        return tz; + +    if (fread (s, sizeof (char), 4, fp) != 4) +        goto err; +    if (strncmp (TZ_MAGIC, s, 4)) +        goto err; + +    if (fread (s, sizeof (char), 1, fp) != 1) +        goto err; +    if (TZ_VERSION != *s && TZ_VERSION != (*s - 1)) +        goto err; + +    if (fseek (fp, -1, SEEK_END) < 0) +        goto err; +    if (fread (s, sizeof (char), 1, fp) != 1) +        goto err; +    if ('\n' != *s) +        goto err; + +    n = -2; +    while (!fseek (fp, n, SEEK_END)) +    { +        if (fread (s, sizeof (char), 1, fp) != 1) +            goto err; +        if ('\n' == *s) +            break; + +        n--; +    } +    fseek (fp, ++n, SEEK_END); + +    n = -(n + 1); +    tz = calloc (n, sizeof (char)); +    if (!tz) +        goto err; + +    if (fread (tz, sizeof (char), n, fp) != n) +    { +        free (tz); +        tz = NULL; +    } + +err: +    fclose (fp); +    return tz; +} + + +/* int +main (int argc, char *argv[]) +{ +    char *tz = NULL; + +    tz = gettimezone (NULL); +    printf ("tz: %s\n", tz); +    if (tz) +        free (tz); + +    return 0; +} */