I am wondering how to best improve on `mktemp` for use with encrypted containers or file systems.

The issue that I am dealing with is that I would like my shell scripts to store temporary files inside the file system that contains the working directory, if possible.

The normal behaviour of `mktemp` seems to be to use a root path specified in an environment variables or `/tmp`. This will, however, routinely leak temporary data to unencrypted locations if I am working with files inside encrypted containers. 

The idea is to first check the presence of a `tmp` directory in the mount point of the current file system and to use `/tmp`only as a last resort. How can I reliably (and efficiently) realize that.

**Edit**

A possible way to identify the mount directory of a given path is the following 

```
dir=`realpath [path]`; 
res=1; 
while [ $res -ne 0 ]; do 
  dir="${dir%/*}"; 
  mountpoint -q "$dir/"; 
  res=$?; 
done; 
echo "$dir";

```

I am not sure, however, if that is the most efficient one.