Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
5 replies Latest Post - ‏2013-07-15T06:21:27Z by 101Rani
PaulClarke
PaulClarke
3 Posts
ACCEPTED ANSWER

Pinned topic Querying program path

‏2013-06-09T08:44:01Z |

I have a program which runs on multiple platforms, windows, Linux, AIX etc, and the program reads data from a configuration file. By default I want the configuration file to be in the same directory as the program itself. On the other platforms I can easily determine where the program is running from but for the life of me I can't work out how to do it in AIX.

Can someone tell me how, in a running C program, you can ascertain the path to the program that is running ?

Thanks,

Paul

  • flodstrom
    flodstrom
    57 Posts
    ACCEPTED ANSWER

    Re: Querying program path

    ‏2013-06-10T15:55:45Z  in response to PaulClarke

    It's no different than that of other UNIX's. Look at the man page for getcwd.

     

    • PaulClarke
      PaulClarke
      3 Posts
      ACCEPTED ANSWER

      Re: Querying program path

      ‏2013-06-10T16:03:47Z  in response to flodstrom

      I don't want the current working directory. I want the directory where the currently running program came from.

      • flodstrom
        flodstrom
        57 Posts
        ACCEPTED ANSWER

        Re: Querying program path

        ‏2013-06-10T16:55:25Z  in response to PaulClarke

        Sorry, read that a bit to fast.

        I don't think there's any reliable way of finding out either the path or the name of a binary. The whole point is that a user shouldn't have to care about it. You can get some info from argv[0], but it's also deemed as unreliable by many programmers. Perhaps you can find something out from /proc (I have not looked into this on AIX though).

        I don't think there's an easy answer to this! I know not the answer you were looking for, but I would suggest to look at using some more conventional ways of finding that config file.

         

        • PaulClarke
          PaulClarke
          3 Posts
          ACCEPTED ANSWER

          Re: Querying program path

          ‏2013-06-10T17:00:40Z  in response to flodstrom

          Thanks for your response.

          Naturally I have tried argv[0] and looked in /proc and getprocs64() but none of them seem to tell me which program I am running.

          I agree that I could force the user to do some more configuration, setting environment variables or the like but it seemed unnecessary. Certainly on Windows and Linux etc one has no trouble finding out where the program is running from. It only seems on AIX that it is awkward (or in fact impossible).

          Cheers,

          Paul

          • 101Rani
            101Rani
            2 Posts
            ACCEPTED ANSWER

            Re: Querying program path

            ‏2013-07-15T06:21:27Z  in response to PaulClarke

            Hi,

            I am also doing the same thing..

            you can find attached file for reference.

            Attachments