2

I want to fix an old C program (got the source) that uses relative paths to load data files, expecting the executable is called from the directory where it is placed, such as LoadEx("./dataFile", dataFile); Of course calling the program from another directory fails since the dataFile cannot be loaded.

How can I modify the C program to load dataFiles relative to the executable's directory?

EDIT: My original question turned inapplicable and so is a duplicate to at least: - How do I find the location of the executable in C? - How to open a file with it's relative path in Linux? - Finding current executable's path without /proc/self/exe

Community
  • 1
  • 1
juanmirocks
  • 4,786
  • 5
  • 46
  • 46
  • 1
    The Java method you listed does not read resources relative to the executing Java class, but rather, relative to its classpath. – Perception Feb 04 '12 at 04:52
  • You're absolutely right. I misunderstood how `ClassLoader` in Java works. Being so, my original question turns inapplicable... Then I guess there is no other way but finding the absolute path with `proc` or setting the absolute path via configuration, in which case my question becomes a duplicate from the other one – juanmirocks Feb 04 '12 at 05:49
  • The argv[0] parameter to the main function in the C code should have the complete file name and path of the executable. Assuming you have a main function and haven't wiped your args. – JimR Feb 04 '12 at 06:32

1 Answers1

3

Why don't you want readlink("/proc/self/exe")? This is the way one finds the executable's path on linux.

The only other way is pull the PATH from the environment with getenv() and walk the PATH directories looking for a match for argv[0]. The first option is much easier.

Or write a script to initiate the program after first doing a which on the executable and passing the result in as a command line argument or some such hack.

Duck
  • 26,924
  • 5
  • 64
  • 92
  • Thanks Duck. I guess I find it quite ugly having to depend on so much detail of a specific OS instead of being able to use a more standard solution. If I wanted to run it on more OSes I would have to check `proc` or `argv[0]` or `$PATH` or `GetModuleFileName` as in http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c But probably this is the way how it's done? – juanmirocks Feb 04 '12 at 05:59
  • filesystems are OS dependent. Not all OSes even have filesystems! – ObscureRobot Feb 04 '12 at 06:19
  • What can you do, it's an imperfect world. If it makes it any easier to accept this is probably what Java is doing beneath the covers too. – Duck Feb 04 '12 at 06:28
  • @ObscureRobot of course, but, in my opinion, a language could easily provide a library for this for standard OSes – juanmirocks Feb 04 '12 at 06:39
  • 1
    The C language was designed for implementing OSes, so the very idea of a "standard" OS is nonsensical. Kernighan and Ritchie never expected Unix to become as popular as it did (nor did Linus, when he started out on Linux). – ObscureRobot Feb 04 '12 at 06:55