1212
1313#define LOCALTIME_IMPLEMENTATION
1414#include "private.h"
15+ #include "tz.h"
1516
1617#include "tzfile.h"
1718#include <fcntl.h>
@@ -357,7 +358,46 @@ union input_buffer {
357358};
358359
359360/* TZDIR with a trailing '/' rather than a trailing '\0'. */
360- static char const tzdirslash [sizeof TZDIR ] = TZDIR "/" ;
361+ static char * s_tzdirslash = NULL ;
362+
363+ bool tzsetdir (const char * dir )
364+ {
365+ if (s_tzdirslash != NULL )
366+ free (s_tzdirslash );
367+
368+ size_t t_length = strlen (dir );
369+ s_tzdirslash = malloc (t_length + 2 );
370+ if (s_tzdirslash == NULL )
371+ return false;
372+
373+ strcpy (s_tzdirslash , dir );
374+ s_tzdirslash [t_length ] = '/' ;
375+ // Nul terminate
376+ s_tzdirslash [t_length + 1 ] = '\0' ;
377+ return true;
378+ }
379+
380+ static const char * tzgetdirslash ()
381+ {
382+ if (s_tzdirslash == NULL )
383+ tzsetdir (TZDIR );
384+
385+ return s_tzdirslash ;
386+ }
387+
388+ #define tzdirslash tzgetdirslash()
389+ #ifdef _WINDOWS
390+ #include <windows.h>
391+ #ifndef PATH_MAX
392+ #define PATH_MAX MAX_PATH
393+ #endif
394+ #endif
395+
396+ #if !defined(__HAVE_SSIZE_T__ )
397+ typedef intptr_t ssize_t ;
398+
399+ # define SSIZE_MAX INTPTR_MAX
400+ #endif
361401
362402/* Local storage needed for 'tzloadbody'. */
363403union local_storage {
@@ -372,7 +412,7 @@ union local_storage {
372412
373413 /* The file name to be opened. */
374414 char fullname [BIGGEST (sizeof (struct file_analysis ),
375- sizeof tzdirslash + 1024 )];
415+ PATH_MAX )];
376416};
377417
378418/* Load tz data from the file named NAME into *SP. Read extended
@@ -409,14 +449,14 @@ tzloadbody(char const *name, struct state *sp, bool doextend,
409449#endif
410450 if (!doaccess ) {
411451 size_t namelen = strlen (name );
412- if (sizeof lsp -> fullname - sizeof tzdirslash <= namelen )
452+ if (sizeof lsp -> fullname - strlen ( tzdirslash ) <= namelen )
413453 return ENAMETOOLONG ;
414454
415455 /* Create a string "TZDIR/NAME". Using sprintf here
416456 would pull in stdio (and would fail if the
417457 resulting string length exceeded INT_MAX!). */
418- memcpy (lsp -> fullname , tzdirslash , sizeof tzdirslash );
419- strcpy (lsp -> fullname + sizeof tzdirslash , name );
458+ memcpy (lsp -> fullname , tzdirslash , strlen ( tzdirslash ) );
459+ strcpy (lsp -> fullname + strlen ( tzdirslash ) , name );
420460
421461 /* Set doaccess if '.' (as in "../") shows up in name. */
422462 if (strchr (name , '.' ))
@@ -1316,7 +1356,7 @@ zoneinit(struct state *sp, char const *name)
13161356 }
13171357}
13181358
1319- static void
1359+ void
13201360tzsetlcl (char const * name )
13211361{
13221362 struct state * sp = lclptr ;
@@ -1353,7 +1393,8 @@ tzsetwall(void)
13531393static void
13541394tzset_unlocked (void )
13551395{
1356- tzsetlcl (getenv ("TZ" ));
1396+ // Control locale externally using tzsetlcl
1397+ //tzsetlcl(getenv("TZ"));
13571398}
13581399
13591400void
@@ -1536,11 +1577,13 @@ localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
15361577 return tmp ;
15371578}
15381579
1580+ #if 0
15391581struct tm *
15401582localtime (const time_t * timep )
15411583{
15421584 return localtime_tzset (timep , & tm , true);
15431585}
1586+ #endif
15441587
15451588struct tm *
15461589localtime_r (const time_t * timep , struct tm * tmp )
@@ -1582,11 +1625,13 @@ gmtime_r(const time_t *timep, struct tm *tmp)
15821625 return gmtsub (gmtptr , timep , 0 , tmp );
15831626}
15841627
1628+ #if 0
15851629struct tm *
15861630gmtime (const time_t * timep )
15871631{
15881632 return gmtime_r (timep , & tm );
15891633}
1634+ #endif
15901635
15911636#ifdef STD_INSPIRED
15921637
@@ -1731,6 +1776,7 @@ timesub(const time_t *timep, int_fast32_t offset,
17311776 return NULL ;
17321777}
17331778
1779+ #if 0
17341780char *
17351781ctime (const time_t * timep )
17361782{
@@ -1743,6 +1789,7 @@ ctime(const time_t *timep)
17431789 struct tm * tmp = localtime (timep );
17441790 return tmp ? asctime (tmp ) : NULL ;
17451791}
1792+ #endif
17461793
17471794char *
17481795ctime_r (const time_t * timep , char * buf )
@@ -2163,7 +2210,7 @@ mktime_z(struct state *sp, struct tm *tmp)
21632210#endif
21642211
21652212time_t
2166- mktime (struct tm * tmp )
2213+ mktime_tz (struct tm * tmp )
21672214{
21682215 time_t t ;
21692216 int err = lock ();
0 commit comments