URI: 
       tavoid redefining sched_yield (Christian Pfeil) - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 5db07ba9422804c9de1e8fbda5486722cb731e03
   DIR parent c77223280246ddb86def848bb3e24bf6ebe90185
  HTML Author: rsc <devnull@localhost>
       Date:   Fri,  3 Mar 2006 01:39:10 +0000
       
       avoid redefining sched_yield (Christian Pfeil)
       
       Diffstat:
         M src/lib9/date.c                     |      97 +++++++++++--------------------
         M src/lib9/sleep.c                    |       2 ++
       
       2 files changed, 37 insertions(+), 62 deletions(-)
       ---
   DIR diff --git a/src/lib9/date.c b/src/lib9/date.c
       t@@ -1,30 +1,28 @@
       -#include <u.h>
       -#include <stdlib.h> /* setenv etc. */
        #define NOPLAN9DEFINES
       +#include <u.h>
        #include <libc.h>
       +#include <stdlib.h> /* setenv etc. */
        #include <time.h>
        
       -#define _HAVETIMEGM 1
       -#define _HAVETMZONE 1
       -#define _HAVETMGMTOFF 1
       -
       -#if defined(__linux__)
       -#        undef _HAVETMZONE
       -
       -#elif defined(__sun__)
       -#        undef _HAVETIMEGM
       -#        undef _HAVETMZONE
       -#        undef _HAVETMGMTOFF
       +static int didtz;
       +static int tzdelta;
       +static char tzone[4];
        
       -#endif
       +static void
       +dotz(void)
       +{
       +        time_t t;
        
       -static Tm bigtm;
       +        if(didtz)
       +                return;
       +        t = time(0);
       +        tzdelta = t - mktime(gmtime(&t));
       +        strftime(tzone, sizeof tzone, "%Z", localtime(&t));
       +}
        
        static void
       -tm2Tm(struct tm *tm, Tm *bigtm)
       +tm2Tm(struct tm *tm, Tm *bigtm, int gmt)
        {
       -        char *s;
       -
                memset(bigtm, 0, sizeof *bigtm);
                bigtm->sec = tm->tm_sec;
                bigtm->min = tm->tm_min;
       t@@ -33,17 +31,13 @@ tm2Tm(struct tm *tm, Tm *bigtm)
                bigtm->mon = tm->tm_mon;
                bigtm->year = tm->tm_year;
                bigtm->wday = tm->tm_wday;
       -        strftime(bigtm->zone, sizeof bigtm->zone, "%Z", tm);
       -#ifdef _HAVETMGMTOFF
       -        bigtm->tzoff = tm->tm_gmtoff;
       -#endif
       -        
       -        if(bigtm->zone[0] == 0){
       -                s = getenv("TIMEZONE");
       -                if(s){
       -                        strecpy(bigtm->zone, bigtm->zone+4, s);
       -                        free(s);
       -                }
       +        if(gmt){
       +                strcpy(bigtm->zone, "GMT");
       +                bigtm->tzoff = 0;
       +        }else{
       +                dotz();
       +                strcpy(bigtm->zone, tzone);
       +                bigtm->tzoff = tzdelta;
                }
        }
        
       t@@ -58,12 +52,6 @@ Tm2tm(Tm *bigtm, struct tm *tm)
                tm->tm_mon = bigtm->mon;
                tm->tm_year = bigtm->year;
                tm->tm_wday = bigtm->wday;
       -#ifdef _HAVETMZONE
       -        tm->tm_zone = bigtm->zone;
       -#endif
       -#ifdef _HAVETMGMTOFF
       -        tm->tm_gmtoff = bigtm->tzoff;
       -#endif
        }
        
        Tm*
       t@@ -71,10 +59,11 @@ p9gmtime(long x)
        {
                time_t t;
                struct tm tm;
       -
       +        static Tm bigtm;
       +        
                t = (time_t)x;
                tm = *gmtime(&t);
       -        tm2Tm(&tm, &bigtm);
       +        tm2Tm(&tm, &bigtm, 1);
                return &bigtm;
        }
        
       t@@ -83,42 +72,26 @@ p9localtime(long x)
        {
                time_t t;
                struct tm tm;
       +        static Tm bigtm;
        
                t = (time_t)x;
                tm = *localtime(&t);
       -        tm2Tm(&tm, &bigtm);
       +        tm2Tm(&tm, &bigtm, 0);
                return &bigtm;
        }
        
       -#if !defined(_HAVETIMEGM)
       -static time_t
       -timegm(struct tm *tm)
       -{
       -        time_t ret;
       -        char *tz;
       -        char *s;
       -
       -        tz = getenv("TZ");
       -        putenv("TZ=");
       -        tzset();
       -        ret = mktime(tm);
       -        if(tz){
       -                s = smprint("TZ=%s", tz);
       -                if(s)
       -                        putenv(s);
       -        }
       -        return ret;
       -}
       -#endif
       -
        long
        p9tm2sec(Tm *bigtm)
        {
       +        time_t t;
                struct tm tm;
        
                Tm2tm(bigtm, &tm);
       -        if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)
       -                return timegm(&tm);
       -        return mktime(&tm);        /* local time zone */
       +        t = mktime(&tm);
       +        if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){
       +                dotz();
       +                t += tzdelta;
       +        }
       +        return t;
        }
        
   DIR diff --git a/src/lib9/sleep.c b/src/lib9/sleep.c
       t@@ -6,6 +6,7 @@
        #include <libc.h>
        
        #if defined(__NetBSD__) || (defined(__OpenBSD__) && OpenBSD <= 200511)
       +#if !defined(sched_yield)
        #        define sched_yield() \
                        do{ struct timespec ts; \
                                ts.tv_sec = 0; \
       t@@ -13,6 +14,7 @@
                                nanosleep(&ts, 0); \
                        }while(0)
        #endif
       +#endif
        
        int
        p9sleep(long milli)