Back to index

glibc  2.9
Functions
dl-origin.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <ldsodefs.h>
#include <sysdep.h>
#include <dl-dst.h>

Go to the source code of this file.

Functions

const char * _dl_get_origin (void)

Function Documentation

const char* _dl_get_origin ( void  )

Definition at line 36 of file dl-origin.c.

{
  char linkval[PATH_MAX];
  char *result;
  int len;
  INTERNAL_SYSCALL_DECL (err);

  len = INTERNAL_SYSCALL (readlink, err, 3, "/proc/self/exe", linkval,
                       sizeof (linkval));
  if (! INTERNAL_SYSCALL_ERROR_P (len, err) && len > 0 && linkval[0] != '[')
    {
      /* We can use this value.  */
      assert (linkval[0] == '/');
      while (len > 1 && linkval[len - 1] != '/')
       --len;
      result = (char *) malloc (len + 1);
      if (result == NULL)
       result = (char *) -1;
      else if (len == 1)
       memcpy (result, "/", 2);
      else
       *((char *) __mempcpy (result, linkval, len - 1)) = '\0';
    }
  else
    {
      result = (char *) -1;
      /* We use the environment variable LD_ORIGIN_PATH.  If it is set make
        a copy and strip out trailing slashes.  */
      if (GLRO(dl_origin_path) != NULL)
       {
         size_t len = strlen (GLRO(dl_origin_path));
         result = (char *) malloc (len + 1);
         if (result == NULL)
           result = (char *) -1;
         else
           {
             char *cp = __mempcpy (result, GLRO(dl_origin_path), len);
             while (cp > result + 1 && cp[-1] == '/')
              --cp;
             *cp = '\0';
           }
       }
    }

  return result;
}

Here is the call graph for this function: