You can't pass a NULL value to setenv(3). The "put things back how they were"
intent of this code is best achieved by calling unsetenv(3) in the NULL case.
(This causes a crash with "hwclock -u" on Android. glibc silently corrupts the
environment instead.)
char *xtzset(char *new)
{
- char *tz = getenv("TZ");
+ char *old = getenv("TZ");
- if (tz) tz = xstrdup(tz);
- if (setenv("TZ", new, 1)) perror_exit("setenv");
+ if (old) old = xstrdup(old);
+ if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
tzset();
- return tz;
+ return old;
}
// Set a signal handler