This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]