• tmpfile()

    From Paul Edwards@3:633/10 to All on Sat May 23 17:57:57 2026
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?

    Thanks. Paul.



    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Janis Papanagnou@3:633/10 to All on Sat May 23 12:27:15 2026
    On 2026-05-23 11:57, Paul Edwards wrote:
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?

    The behavior you fear makes no sense to me. I read the man page

    "The tmpfile() function opens a unique temporary file in binary
    read/write (w+b) mode. The file will be automatically deleted
    when it is closed or the program terminates."

    as not one single file (for all) but a unique file for every call.
    The POSIX pages tell us equivalent things.

    No proof, but a quick test seems to confirm the described behavior:

    #include <stdlib.h>
    #include <stdio.h>

    int main (void)
    {
    FILE * f1 = tmpfile();
    system ("sleep 1");
    FILE * f2 = tmpfile();
    system ("sleep 1");
    fclose (f1);
    system ("sleep 1");
    fclose (f2);
    }


    # output of: inotifywait /tmp -m
    ...
    /tmp/ OPEN #786626
    /tmp/ OPEN #786713
    /tmp/ CLOSE_WRITE,CLOSE #786626
    /tmp/ CLOSE_WRITE,CLOSE #786713


    Concerning the C89 standard specifically, others will certainly
    provide the respective quotes.

    Janis


    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Pierre Asselin@3:633/10 to All on Sat May 23 19:14:07 2026
    Paul Edwards <mutazilah@gmail.com> wrote:
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?


    I won't bother to dig up the ANSI standard, but the Linux man page says:

    The tmpfile() function opens a unique temporary file
    in binary read/write (w+b) mode. The file will be automatically
    deleted when it is closed or the program terminates.
    [ ... ]
    STANDARDS
    C11, POSIX.1-2008.

    My reading is that a second call to tmpfile() would be just fine
    and would open *another* unique temporary file. Also, I would expect
    the deferred deletion to be done by unlinking the file immediately
    after creation, leaving an anonymous file that disappears on last
    close.

    (If the OS doesn't have anonymous files the implementation might
    have to register a cleanup function with atexit() or similar.)

    This is what happens on Linux. Here's a test program: --------------------------------------------------
    #include <stdio.h>
    #include <stdlib.h> /* system() */

    int main(int argc, char *argv[])
    {
    FILE *f1= tmpfile();
    FILE *f2= tmpfile();
    int fd1= fileno(f1);
    int fd2= fileno(f2);
    char buf[128];
    int count;

    /* FIXME error checks */
    count= snprintf(buf, 128, "file /proc/self/fd/%d", fd1);
    system(buf);
    count= snprintf(buf, 128, "file /proc/self/fd/%d", fd2);
    system(buf);

    fclose(f2);
    fclose(f1);

    return 0;
    }
    --------------------------------------------------

    Output:
    --------------------------------------------------
    /proc/self/fd/3: symbolic link to /tmp/#524307 (deleted)
    /proc/self/fd/4: symbolic link to /tmp/#524308 (deleted) --------------------------------------------------

    --
    pa at panix dot com

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From James Kuyper@3:633/10 to All on Sat May 23 17:14:40 2026
    On 2026-05-23 11:57, Paul Edwards wrote:
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?

    In C89, it says that the call to tmpfile() creates the corresponding
    file - this implies that two separate successful calls must each create
    a file. However, there's no guarantee on how many times it can
    successfully be called.

    The current standard is clearer on this issue:

    "The tmpfile function creates a temporary binary file that is different
    from any other existing file ..."

    and

    "Recommended practice
    3
    It should be possible to open at least TMP_MAX temporary files during
    the lifetime of the program (this limit may be shared with tmpnam) and
    there should be no limit on the number simultaneously open other than
    this limit and any limit on the number of open files (FOPEN_MAX)"

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From James Kuyper@3:633/10 to All on Sat May 23 16:58:41 2026
    On 2026-05-23 11:57, Paul Edwards wrote:
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?

    In C89, it says that the call to tmpfile() creates the corresponding
    file - this implies that two separate successful calls must each create
    a file. However, there's no guarantee on how many times it can
    successfully be called.

    The current standard is clearer on this issue:

    "The tmpfile function creates a temporary binary file that is different
    from any other existing file ..."

    and

    "Recommended practice
    3
    It should be possible to open at least TMP_MAX temporary files during
    the lifetime of the program (this limit may be shared with tmpnam) and
    there should be no limit on the number simultaneously open other than
    this limit and any limit on the number of open files (FOPEN_MAX)"

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Keith Thompson@3:633/10 to All on Sat May 23 20:43:15 2026
    James Kuyper <jameskuyper@alumni.caltech.edu> writes:
    On 2026-05-23 11:57, Paul Edwards wrote:
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?

    In C89, it says that the call to tmpfile() creates the corresponding
    file - this implies that two separate successful calls must each create
    a file. However, there's no guarantee on how many times it can
    successfully be called.

    The current standard is clearer on this issue:

    "The tmpfile function creates a temporary binary file that is different
    from any other existing file ..."

    and

    "Recommended practice
    3
    It should be possible to open at least TMP_MAX temporary files during
    the lifetime of the program (this limit may be shared with tmpnam) and
    there should be no limit on the number simultaneously open other than
    this limit and any limit on the number of open files (FOPEN_MAX)"

    TMP_MAX is defined in all editions of the C standard, and is required
    to be at least 25. C89/C90 only associates TMP_MAX with the tmpnam()
    function. C99 and later apply it to both tmpnam() and tmpfile().

    There is no implication in C89 or later that tmpfile() can only
    create a single file. A conforming C89 implementation could allow
    tmpfile() to create arbitrarily many files. tmpfile() returns a
    null pointer if the file cannot be created for any reason.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Sun May 24 04:00:08 2026
    On Sat, 23 May 2026 19:14:07 -0000 (UTC), Pierre Asselin wrote:

    Also, I would expect the deferred deletion to be done by unlinking
    the file immediately after creation, leaving an anonymous file that disappears on last close.

    Linux also offers the option to go the other way <https://manpages.debian.org/open(2)#O_TMPFILE>: create a file that is initially anonymous, but which can be given a name later.

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From James Kuyper@3:633/10 to All on Sat May 23 17:19:05 2026
    On 2026-05-23 11:57, Paul Edwards wrote:
    My reading of the ANSI C89 draft is that tmpfile() only
    opens a single file. ie you're not expected to be able to do
    two separate calls to tmpfile and get two separate files.

    Unlike tmpnam, there is no mention of TMP_MAX to
    suggest multiple opens are expected.

    So what's the situation? Is my C library only expected to
    support a single tmpfile() call until the file is closed?

    In C89, it says that the call to tmpfile() creates the corresponding
    file - this implies that two separate successful calls must each create
    a file. However, there's no guarantee on how many times it can
    successfully be called.

    The current standard is clearer on this issue:

    "The tmpfile function creates a temporary binary file that is different
    from any other existing file ..."

    and

    "Recommended practice
    3
    It should be possible to open at least TMP_MAX temporary files during
    the lifetime of the program (this limit may be shared with tmpnam) and
    there should be no limit on the number simultaneously open other than
    this limit and any limit on the number of open files (FOPEN_MAX)"

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Paul Edwards@3:633/10 to Unknown on Mon May 25 12:43:58 2026
    Thanks all for your comments on this issue.

    I modified my C library to provide an algorithm to give me
    up to 25 scratch files. xcc was making use of that ability,
    but that logic is being changed anyway.

    xcc wasn't self-reproducing, but it is nearly there now.

    BFN. Paul.



    --- PyGate Linux v1.5.15
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to Unknown on Mon May 25 05:54:27 2026
    On Mon, 25 May 2026 12:43:58 +0800, Paul Edwards wrote:

    I modified my C library to provide an algorithm to give me up to 25
    scratch files.

    If I need more than one temporary file, that?s usually the point where
    I decide I need to create a temporary directory instead, and put the
    files in there.

    <https://manpages.debian.org/mkdtemp(3)>

    --- PyGate Linux v1.5.15
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)