view src/util_linux.c @ 0:147b08bc7d64

Initial commit of a basic Application framework.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Mar 2015 12:41:52 +0100
parents
children 9daf778feaf1
line wrap: on
line source
#ifndef WIN32
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */

/**@file Linux implementation of functions declared in util.h */

#include "util.h"
#include "string.h"
#include "strhelp.h"
#include "unistd.h"
#include <sys/types.h>
#include <pwd.h>

char *
get_proc_install_dir(const char *proc)
{
  char *retval = NULL,
       *procpath = NULL,
       *p = NULL,
        buf[MAX_PATH_LINUX];
  ssize_t ret;
  size_t path_len = 0;

  if (!proc)
    {
      return NULL;
    }

  xasprintf(&procpath, "/proc/%s/exe", proc);

  ret = readlink (procpath, buf, MAX_PATH_LINUX);
  xfree(procpath);
  procpath = NULL;

  if (ret <= 0)
    {
      return NULL;
    }

  buf[ret] = '\0';

  /* cut off the filename */
  p = strrchr (buf, '/');
  if (p == NULL)
    {
      return NULL;
    }
  *(p + 1) = '\0';

  path_len = strlen (buf);
  retval = xmalloc (path_len + 1);
  strncpy (retval, buf, path_len);
  retval[path_len] = '\0';

  return retval;
}

char *
get_install_dir()
{
  return get_proc_install_dir("self");
}


#endif
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)