Subject: Re: Memory leak in setenv(3) ?
To: Simon Burge <simonb@wasabisystems.com>
From: Paul Goyette <paul@whooppee.com>
List: current-users
Date: 05/29/2003 19:57:24
On Fri, 30 May 2003, Simon Burge wrote:
8< snip >8
> I've got the following in a program I use (a multi-timezone hack for
> xclock). This is near the start of the program:
>
> /*
> * XXX: The longestlen, longest, setenv business is described
> * in Clock.c - look for
> * XXX - this is not pretty
> */
> int i, longestlen;
> char *longest;
>
> ntimezones = argc - 1;
> timezones = (char **)malloc(argc * sizeof(char *));
>
> longestlen = 0;
> for (i = 0; i < ntimezones; i++) {
> timezones[i] = argv[i + 1];
> if (strlen(timezones[i]) > longestlen) {
> longest = timezones[i];
> longestlen = strlen(timezones[i]);
> }
> }
> setenv("TZ", longest, 1);
>
> and this is in the loop where I want to call localtime with different
> timezones:
>
> #ifdef want_memory_leak
> setenv("TZ", tz, 1);
> #else
> /* XXX - this is not pretty! but setenv() leaks memory :-( */
> char *envptr;
> envptr = getenv("TZ");
> strcpy(envptr, tz);
> #endif
> tm = *localtime(&time_value);
I actually thought of that, but then I found the following in the man
page for setenv(3) under Return Values:
If getenv() is successful, the string returned should be
considered read-only.
It would be really nice to solve this in a portable manner! :)
> Not nice, but get's the job done. Having something like a
> localtime_tz(3) with an extra arg for the timezone would be nice...
Yup, that would be a Good Thing.
----------------------------------------------------------------------
| Paul Goyette | PGP DSS Key fingerprint: | E-mail addresses: |
| Network Engineer | 9DC5 05CF 1AE7 DC42 CCC6 | paul@whooppee.com |
| & World Cruiser | 6858 051E 7AD2 A6B2 4954 | pgoyette@juniper.net |
----------------------------------------------------------------------