I have some legacy code that creates and writes to a file. The problem is, I have no idea where this file is being saved to. I'm trying to write something that will give me the path of where the file is being saved, but I'm failing.
I thought readlink seemed like the best way to go about this, but I can't even get readlink to return the location of my test program:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
char* savefm(char* savename)
{
FILE *tfp;
char buf[1024];
ssize_t len;
tfp = fopen(savename,"w");
fprintf(tfp,"Write to file\n");
len = readlink("proc/self/fd", buf, 1023);
buf[len]='\0';
if(len == -1)
perror("readlink");
else
printf("The path to this file is: %s\n", buf);
if (tfp == NULL)
return("INVALID");
else
return("VALID");
}
void main(int argc, char *argv[])
{
char *filename;
char buf[1024];
ssize_t len = readlink("proc/self/exe", buf, 1023);
buf[len]='\0';
if(len == -1)
perror("readlink");
else
printf("The path to this process is: %s\n", buf);
filename = savefm("file1");
if (filename == "VALID")
printf("VALID\n");
else
printf("INVALID\n");
}
And the output is:
readlink: No such file or directory
readlink: No such file or directory
VALID
This is confusing to me, as 1) The program is printing VALID, meaning the file descriptor was successfully created 2) In my test directory where I'm running this, the file "file1" is created and written to, so I KNOW the file exists, because it's right there.
Any assistance is appreciated, either with fixing readlink or other methods for getting the path of the file. I'm running Solaris 11.3.