Where does the tmpname function write temporary files in a Sun Solaris environment?

217 views Asked by At

I am in a Unix environment (Sun Ultra 5 with gcc v.2.95.3) and I am trying to understand where my compiler writes temporary files using the tmpnam function. I am aware that this works is deprecated but mine was only curiosity. I acted like this: I ran this source in c:

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

int main(void) {
    char nomefile[L_tmpnam];

    strcpy(nomefile, "my_file.txt");
    tmpnam(nomefile);

    getchar();

    return(0);
}

while waiting for the return key to be pressed, the program should create a temporary file (somewhere in the file system) named my_file.txt. So from another terminal I start the search in this way (as superuser):

find / -name my_file.txt -print

unfortunately it does not find it. Yet in the book I'm reading it says that this function (tmpnam) unlike the tmpfile function, you can specify the name of the temporary file and write it somewhere in the filesystem.

1

There are 1 answers

0
Roberto Rocco On

The correct source for identifying where temporary files are written is the following. As was suggested to me in the comments.

#include <stdio.h>

int main(void) {
    char buffer[L_tmpnam] = "my_file.txt";
    char *ptr;

    tmpnam(buffer);
    printf("Temporary name 1: %s\n", buffer);

    ptr = tmpnam(NULL);
    printf("Temporary name 2: %s\n", ptr);

    return(0);
}

Output:

Temporary name 1: /var/tmp/aaagXay5b
Temporary name 2: /var/tmp/baahXay5b