0

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.

Roberto Rocco
  • 450
  • 2
  • 11
  • use `strace your_program` to see what it does. BTW: the tempfile might not be visible in the mounted file systems. – wildplasser Feb 26 '20 at 21:10
  • unfortunately it gives me a mistake: # strace a.out -> ERROR: unable to open /dev/log... I tried as superuser and gives me another error: ERROR: tracer already exists – Roberto Rocco Feb 26 '20 at 21:15
  • 5
    *...the program should create a temporary file...*. Actually, `tmpnam` doesn't create a file. It creates a unique file name, using the string you give it as a base, and returns a pointer to that name which you can then use as a name for a temporary file. Looks like you didn't use it. Did you check `man tmpnam` on your system? – lurker Feb 26 '20 at 21:22
  • I saw the tmpnam manual as you recommended me @lurker. The directory is contained in the P_tmpdir macro (/var/tmp). You're right, the files are stored according to a unique name decided by the compiler, both if the argument is NULL, and if a name is used. In fact, a pointer is missing from my source. – Roberto Rocco Feb 26 '20 at 23:10
  • 2
    @wildplasser: Solaris's `strace` command does something different than what you're expecting. The Solaris command for tracing system calls, analogous to Linux's `strace`, is called `truss`. – Nate Eldredge Feb 26 '20 at 23:16
  • 1
    @lurker: Actually I don't think it uses the string as a base for the name. It generates the name from scratch and stores it in the given buffer, whose previous contents are ignored and overwritten. You might be thinking of `tempnam`. – Nate Eldredge Feb 26 '20 at 23:21
  • @NateEldredge You're right. Digital Unix had it too. – wildplasser Feb 27 '20 at 00:16
  • @NateEldredge right, thanks for catching that. I did get them mixed up. Nonetheless, the main issue still stands. – lurker Feb 27 '20 at 10:36

1 Answers1

0

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
Roberto Rocco
  • 450
  • 2
  • 11