Skip to content

Commit

Permalink
Fix handling of %TEMP% & %TMP% in tmpnam()/tmpfile()
Browse files Browse the repository at this point in the history
The TEMP and TMP environment variables in DOS/Windows
were not properly handled in tmpnam()/tmpfile() w.r.t.
slashes in file names resulting in fallback to C:\ as
the location for temporary files.
While in most cases this was OK, this posed a problem
for write-protected C: drives (e.g. USB sticks).
  • Loading branch information
alexfru committed Aug 10, 2016
1 parent b5c3226 commit 0c184cd
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 2 deletions.
Binary file modified v0100/bind/n2f.exe
Binary file not shown.
Binary file modified v0100/bind/smlrcc.exe
Binary file not shown.
Binary file modified v0100/bindp/n2f.exe
Binary file not shown.
Binary file modified v0100/bindp/smlrcc.exe
Binary file not shown.
Binary file modified v0100/binw/n2f.exe
Binary file not shown.
Binary file modified v0100/lib/lcdh.a
Binary file not shown.
Binary file modified v0100/lib/lcdp.a
Binary file not shown.
Binary file modified v0100/lib/lcds.a
Binary file not shown.
Binary file modified v0100/lib/lcw.a
Binary file not shown.
30 changes: 28 additions & 2 deletions v0100/srclib/tmpnam.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,32 @@ static
void TryPath(char* path)
{
unsigned attrOrError;
int trailingSlash = 0;

if ((plen = strlen(path)) >= L_tmpnam - (1/*slash*/ + 8+1+3/*8.3 name*/))
return;

strcpy(name, path);
if (name[plen - 1] != '\\' && name[plen - 1] != '/' && name[plen - 1] != ':')
strcat(name, "\\"), plen++;

if (name[plen - 1] == '\\' || name[plen - 1] == '/')
trailingSlash = name[plen - 1];
// The name of the directory whose existence/attribute we're about to check
// should end with a slash IFF it's a root directory. Valid: C:\, C:\FOO.
// Invalid: C:, C:\FOO\.
if (trailingSlash)
{
if (plen > 1 && name[plen - 2] != ':')
name[--plen] = '\0';
else
trailingSlash = 0;
}
else
{
if (name[plen - 1] == ':')
strcat(name, "\\"), plen++;
else
trailingSlash = '\\';
}

// Check if name exists in the file system and is a directory
#ifdef _DOS
Expand All @@ -120,6 +139,13 @@ void TryPath(char* path)
if (attrOrError == INVALID_FILE_ATTRIBUTES || !(attrOrError & FILE_ATTRIBUTE_DIRECTORY))
*name = '\0';
#endif

// If it's a directory, append a slash if needed, so a file name can be appended after it
if (*name && trailingSlash)
{
name[plen++] = trailingSlash;
name[plen] = '\0';
}
}

static
Expand Down

0 comments on commit 0c184cd

Please sign in to comment.