Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Understanding Lua's os.tmpname() on Windows

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://docs.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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!