4

I'm trying to use Lua's os.tmpname() on Windows (which uses tmpnam() under the hood), and I'm a bit puzzled by the filenames it is returning.

> print(os.tmpname())
\s3e8.

If I feed it directly to Lua like io.open(os.tmpname(), "w"), it will attempt to create the file in the root directory of the current drive. This seems pretty inappropriate, since we often don't have the permission to do that.

But according to this thread:

https://mingw-users.narkive.com/L7VR1gxX/temporary-file-woes

This is apparently supposed to be a path relative to the current directory. They mentioned this snippet from a Microsoft documentation:

Note than when a file name is prepended with a back slash and no path information, such as \fname21, this indicates that the name is valid for the current working directory.

But I can no longer find the documentation they mentioned in the thread. I googled around, trying to find the latest documentation and found this: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tempnam-wtempnam-tmpnam-wtmpnam?view=msvc-160

This is the generation rules mentioned in the documentation above:

_tempnam will generate a unique file name for a directory chosen by the following rules:

If the TMP environment variable is defined and set to a valid directory name, unique file names will be generated for the directory specified by TMP.

If the TMP environment variable is not defined or if it is set to the name of a directory that does not exist, _tempnam will use the dir parameter as the path for which it will generate unique names.

If the TMP environment variable is not defined or if it is set to the name of a directory that does not exist, and if dir is either NULL or set to the name of a directory that does not exist, _tempnam will use the current working directory to generate unique names. Currently, if both TMP and dir specify names of directories that do not exist, the _tempnam function call will fail.

I do have the TMP environment variable defined, but Lua always generates the kind of paths I mentioned at the top.

So really, I have two questions:

  1. Is this actually supposed to be a relative path?
  2. If so why does it always generate a relative path, instead of using the standard TMP variables?

I'm testing this with Lua 5.2 from LuaBinaries and the Lua statically built into shinchiro's mpv builds.

1 Answer 1

1

According to this thread on lua-users.org you can try passing null.

well i solved it using tmpname(NULL,NULL)... it considers the temporary directory of the system. when i use tmpnam() on windows on a non-writable root, the filename generated (\something) is not writable... i guess it's an implementation bug

Also note the Lua 5.2 Reference Manual

When possible, you may prefer to use io.tmpfile, which automatically removes the file when the program ends.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.