| www.delorie.com/archives/browse.cgi | search |
| Xref: | news-dnh.mv.net comp.os.msdos.djgpp:1683 |
| Newsgroups: | comp.os.msdos.djgpp |
| Path: | news-dnh.mv.net!mv!news.sprintlink.net!EU.net!sun4nl!phcoms4.seri.philips.nl!newssvr!kunst |
| From: | kunst AT prl DOT philips DOT nl (Pieter Kunst) |
| Subject: | Re: Patch for tmpnam() (v2) |
| Sender: | news AT prl DOT philips DOT nl (USENET News System) |
| Organization: | Philips Research Laboratories, Eindhoven, The Netherlands |
| References: | <DDMwL1 DOT Iw5 AT jade DOT mv DOT net> |
| Date: | Mon, 21 Aug 1995 14:14:01 GMT |
| Lines: | 48 |
| To: | djgpp AT sun DOT soe DOT clarkson DOT edu |
| Dj-Gateway: | from newsgroup comp.os.msdos.djgpp |
In article <DDMwL1 DOT Iw5 AT jade DOT mv DOT net> Marc Singer <nbn.nbn.com!sapien!elf> writes:
>Ok. It isn;t really a `patch'. Here is a version of tmpname() that
>does not bomb if $TMP doesn't end with a path separator.
>
>-------------------- cut --------------------
>char *
>tmpnam(char *s)
>{
> static char static_buf[L_tmpnam];
> static char tmpcount[] = "dj000000";
> static const char *tmp_dir=0;
> static int tmp_len;
> int i;
> char* pchCount;
>
> if (tmp_dir == 0)
> {
> tmp_dir = getenv("TMPDIR");
> if (!tmp_dir || access(tmp_dir, D_OK))
> {
> tmp_dir = getenv("TEMP");
> if (!tmp_dir || access(tmp_dir, D_OK))
> {
> tmp_dir = getenv("TMP");
> if (!tmp_dir || access(tmp_dir, D_OK))
> tmp_dir = "c:/";
> }
> }
> tmp_len = strlen(tmp_dir);
> }
>
> if (!s)
> s = static_buf;
> strcpy(s, tmp_dir);
[rest of code deleted...]
This code might not work properly with the following sequence:
tmpnam(); /* tmp_dir points to getenv() static data buffer */
getenv("SOMEVAR"); /* subsequent call to getenv() fills static data */
tmpnam(); /* might fail due to static data overwrite */
/* i.e. tmp_dir points to SOMEVAR getenv() result */
Better make a local copy of the getenv() result.
Pieter.
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |