readlink() error while reading /proc/self/exefile on QNX

652 views Asked by At

I am working on QNX platform, in which I need to get the path of executable which is running.

I have wrote a small peice of code, which is returning always -1:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

extern int errno;

int main( int argc, char** argv )
{
    char buf[512] = {0};
    const char mypath[100] = "/proc/self/exefile";
    errno = 0;
    printf("The value readlink:: %d %s\n",readlink(mypath, buf, 512 ), strerror( errno ));    
    return( 0 );
}

When I ran above code then I get following output:

The value readlink:: -1 No such file or directory

Am I missing anything? What needs to be done to get my current exe path in QNX?

1

There are 1 answers

0
Ilyas H On

In QNX /proc/self/exefile is not a symbolic link; It's a regular file.

Try:

#include <fstream>
#include <iostream>
#include <string>

int main(int argc, char **argv) {

  std::ifstream file("/proc/self/exefile");
  std::string path;
  std::getline(file, path);

  std::cout << path << "\n";

  return 0;
}