1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bk_32
  • 483
  • 1
  • 4
  • 17
  • By default the file will be opening the same directory that the binary file is being executed in. The path can just be retrieved from argv[0] – Irelia Oct 18 '19 at 15:45
  • I tried that, but when I ran `printf("Process path: %s\n",argv[0]);` all it printed was "./a.out", not the full path of where I ran the test program. – bk_32 Oct 18 '19 at 15:53
  • Try `getcwd` in `unistd.h` – Irelia Oct 18 '19 at 15:59
  • Thank you, getcwd does exactly what I need. Any ideas on why readlink was not working? – bk_32 Oct 18 '19 at 16:05
  • `proc` should be `/proc` – Barmar Oct 18 '19 at 16:07
  • 1
    Also, `/proc/self/fd` is a directory, not a link. What you want is `/proc/self/fd/%d` where `%d` is replaced with `fileno(tfp)` – Barmar Oct 18 '19 at 16:10
  • I did as you suggested above, but I am still getting the same results. Also can you please explain how the question you marked mine a duplicate of is even related to my question? I do not know the sym link path in order to convert it to a real path. I have absolutely no information about the path, which is why I need to do this. – bk_32 Oct 18 '19 at 18:28
  • 1
    @Barmar `/proc/self/fd/%d` is not a link on Solaris. You can use `realpath()` as noted in the duplicate question, or you can `readlink()` against `/proc/self/path/%d` for a Linux-style link. – Andrew Henle Oct 21 '19 at 21:18
  • The path is in the `savename` variable, otherwise you wouldn't be able to open the file. – Barmar Oct 21 '19 at 23:32
  • @AndrewHenle I tried `readlink("/proc/self/path/fileno(tfp)")` as you suggested, but still got `readlink: No such file or directory` as a result. Can you please explain how I can use realpath() without knowing the symbolic link? – bk_32 Oct 22 '19 at 12:53

0 Answers0