TMP/TEMP environment variable and /tmp

Brian Inglis Brian.Inglis@SystematicSw.ab.ca
Fri Sep 18 03:56:09 GMT 2020


On 2020-09-17 20:17, Ken Brown via Cygwin wrote:
> On 9/17/2020 7:58 PM, Doug Henderson via Cygwin wrote:
>> On Thu, 17 Sep 2020 at 15:56, Ken Brown via Cygwin <> wrote:
>>>
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> int
>> main ()
>> {
>>      char *temp_nam;
>>      char *p_tmp_nam;
>>
>>      printf ("$TMP      is '%s'\n", getenv ("TMP"));
>>      printf ("$TMPDIR   is '%s'\n", getenv ("TMPDIR"));
>>      printf ("$TEMP     is '%s'\n", getenv ("TEMP"));
>>      printf ("P_tmpdir  is '%s'\n", P_tmpdir);
>>      p_tmp_nam = tmpnam(0);
>>      printf ("tmpnam()  is '%s'\n", p_tmp_nam);
>>      temp_nam = tempnam(0, 0);
>>      printf ("tempnam() is '%s'\n", temp_nam);
>>      free(temp_nam);
>> }
>>
>>
>> # start a new shell
>> $ sh
>> $ TMP= TEMP= ./show_tmp
>> $TMP      is ''
>> $TMPDIR   is '(null)'
>> $TEMP     is ''
>> P_tmpdir  is '/tmp'
>> tmpnam()  is '/tmp/t707.0'
>> tempnam() is '/tmp/ffffd187.2'
>>
>> # start cmd.exe
>> $ /cygdrive/c/windows/system32/cmd.exe
>> Microsoft Windows [Version 10.0.18363.1082]
>> (c) 2019 Microsoft Corporation. All rights reserved.
>>> set TMP=
>>> set TEMP=
>>> show_tmp
>> $TMP      is '(null)'
>> $TMPDIR   is '(null)'
>> $TEMP     is '(null)'
>> P_tmpdir  is '/tmp'
>> tmpnam()  is '/tmp/t709.0'
>> tempnam() is '/tmp/ffffd189.2'
>>
>> P_tmpdir is defined in <stdio.h>
> 
> Sorry, but I'm missing your point.  How is this related to Kristian's claim that
> Cygwin is changing the value of the TMP environment variable to "/tmp"?

It demonstrates that Cygwin programs don't themselves change TEMP/TMP, unless
they call an API routine that generates a filename in a directory, when the
filesystem handling may translate a nonexistent or unmounted /tmp/ to ../tmp/.

That jives with the defaults I see in the registry and in the cmd environments:

$ regtool list -v
/proc/registry/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session\
Manager/Environment | grep 'T.*MP'
TEMP (REG_EXPAND_SZ) = "%SystemRoot%\TEMP"
TMP (REG_EXPAND_SZ) = "%SystemRoot%\TEMP"

$ regtool list -v /proc/registry/HKEY_CURRENT_USER/Environment | grep 'T.*MP'
TEMP (REG_EXPAND_SZ) = "%UserProfile%\AppData\Local\TEMP"
TMP (REG_EXPAND_SZ) = "%UserProfile%\AppData\Local\TEMP"

C:\Windows\system32>set | grep "T.*MP"
TEMP=C:\Windows\TEMP
TMP=C:\Windows\TEMP
C:\Windows\system32>which env
/usr/bin/env
C:\Windows\system32>env | grep "T.*MP"
TEMP=/cygdrive/c/Windows/TEMP
TMP=/cygdrive/c/Windows/TEMP

C:\Users\...>set | grep "T.*MP"
TEMP=C:\Users\...\AppData\Local\Temp
TMP=C:\Users\...\AppData\Local\Temp
C:\Users\...>env | grep "T.*MP"
TEMP=/cygdrive/c/Users/.../AppData/Local/Temp
TMP=/cygdrive/c/Users/.../AppData/Local/Temp

and I know you can just delete those environment variables and registry keys at
the risk of upsetting any Windows programs that need temp space and do not
provide an accessible fallback.

The same goes for any Cygwin script or program: each application needs to check
it has access to any output locations: the directory is writable and the file
can be created e.g. in /var/log/ or /tmp/, or try to create a directory, or try
elsewhere /usr/tmp/, /var/tmp/, or create that, or try the current directory ./
or home directory $HOME/, known paths $USERPROFILE/ or $LOCALAPPDATA/Temp/, or
exit with a failure condition.

I think we need to see the environment or registry and application code around
creating the file to diagnose the cause, but I expect the environment may not
define TMPDIR/TMP/TEMP, the application provides no accessible fallback(s), and
Cygwin defaults the path.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.
[Data in IEC units and prefixes, physical quantities in SI.]


More information about the Cygwin mailing list