This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
How to use a static environment
- From: Giuseppe Modugno <giuseppe dot modugno dot loqed at gmail dot com>
- To: newlib at sourceware dot org
- Date: Thu, 5 Apr 2018 16:49:35 +0200
- Subject: How to use a static environment
I need to use environment functionality in newlib to have a correct
result from localtime() with the western european timezone (I need TZ
environment variable). I also need to convert UTC struct tm to time_t
with mktime(), so I should delete TZ variable before calling mktime().
I know I can call setenv()/unsetenv(), but I would like to avoid using
dynamic allocation (that those functions use). So I'm thinking to have
two static const environments, such as:
static const char *const env_with_tz[] = {
"TZ=CET-1CEST,M3.5.0,M10.5.0/3", NULL };
static const char *const env_empty[] = { NULL };
and change the environment on-the-fly in the following way:
extern char **environ;
environ = env_with_tz; // or environ = env_empty;
Is it safe?
Of course I need to call tzset() before calling mktime()/localtime() if
the environment has changed.