2

I want to open a number of files (log4cxx configs, other logs etc) relative to binary's location.

Unfortunately, both getwd() and getcwd() are giving me the directory, from which I try to run binary at known path, instead of giving me the path where the binary is located (and where the data is).

How to get the app's path to use it with chdir()? Any methods besides argv[0] and without trying to parse /proc/$PID/ (that's not portable enough)?

kagali-san
  • 2,964
  • 7
  • 48
  • 87

2 Answers2

3

Walk the PATH and find an executable of the same name as argv[0]?

However, it would probably be better to provide the user a way to configure where the data is. An env var or config file or CL parameter or something. It's very frustrating dealing with programs that try to be helpful but are actually just stupid.

drysdam
  • 8,341
  • 1
  • 20
  • 23
  • 2
    Well. big bosses love simple stuff :) if there's no portable solution, a simple .sh/.bat wrapper will be used. – kagali-san Mar 29 '11 at 00:04
  • 2
    why are your logs relative to binary location. Logs should be in /var/log/. A normal user will not have the appropriate permissions to modify files that are local to the binary. – Martin York Mar 29 '11 at 00:07
  • 1
    persistent logs&data are indeed stored in /var/, but some temp files are used at the debugging stage, and changing this will reintroduce costs of redeploying the whole app after each svn commit -- too much time to spend at other things. – kagali-san Mar 29 '11 at 00:16
  • unfortunately, 'which' doesn't work the same on all unices. I think it's Solaris that does some weird thing. – drysdam Mar 29 '11 at 10:00
  • True, but in that case won't argv hold the path? Maybe not. – drysdam Mar 30 '11 at 14:30
1

This is exactly the kind of thing autoconf lives for, and supporting those standard directories is pretty much mandatory if you ever want anyone other than the programmers who wrote your software to use it. Once set up properly, to debug out of your home directory all you have to do is pass a different --prefix= value to configure.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94