Blame src/posix-util.c

Packit Service 672cf4
/* posix-util.c - Utility functions for Posix
Packit Service 6c01f9
   Copyright (C) 2001 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2002, 2004 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, write to the Free Software
Packit Service 6c01f9
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 6c01f9
   02111-1307, USA.  */
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
#include <stdio.h>
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <assert.h>
Packit Service 672cf4
Packit Service 672cf4
#include "util.h"
Packit Service 672cf4
#include "sys-util.h"
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
Packit Service 672cf4
/* These variables store the malloced name of alternative default
Packit Service 672cf4
   binaries.  The are set only once by gpgme_set_global_flag.  */
Packit Service 672cf4
static char *default_gpg_name;
Packit Service 672cf4
static char *default_gpgconf_name;
Packit Service 672cf4
Packit Service 672cf4
/* Set the default name for the gpg binary.  This function may only be
Packit Service 672cf4
   called by gpgme_set_global_flag.  Returns 0 on success.  Leading
Packit Service 672cf4
   directories are removed from NAME.  */
Packit Service 672cf4
int
Packit Service 672cf4
_gpgme_set_default_gpg_name (const char *name)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *s;
Packit Service 672cf4
Packit Service 672cf4
  s = strrchr (name, '/');
Packit Service 672cf4
  if (s)
Packit Service 672cf4
    name = s + 1;
Packit Service 672cf4
Packit Service 672cf4
  if (!default_gpg_name)
Packit Service 672cf4
    default_gpg_name = strdup (name);
Packit Service 672cf4
  return !default_gpg_name;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Set the default name for the gpgconf binary.  This function may
Packit Service 672cf4
   only be called by gpgme_set_global_flag.  Returns 0 on success.
Packit Service 672cf4
   Leading directories are removed from NAME.  */
Packit Service 672cf4
int
Packit Service 672cf4
_gpgme_set_default_gpgconf_name (const char *name)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *s;
Packit Service 672cf4
Packit Service 672cf4
  s = strrchr (name, '/');
Packit Service 672cf4
  if (s)
Packit Service 672cf4
    name = s + 1;
Packit Service 672cf4
Packit Service 672cf4
  if (!default_gpgconf_name)
Packit Service 672cf4
    default_gpgconf_name = strdup (name);
Packit Service 672cf4
  return !default_gpgconf_name;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Dummy function - see w32-util.c for the actual code.  */
Packit Service 672cf4
int
Packit Service 672cf4
_gpgme_set_override_inst_dir (const char *dir)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)dir;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Find an executable program PGM along the envvar PATH.  */
Packit Service 672cf4
static char *
Packit Service 672cf4
walk_path (const char *pgm)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *orig_path, *path, *s;
Packit Service 672cf4
  char *fname, *p;
Packit Service 672cf4
Packit Service 672cf4
#ifdef FIXED_SEARCH_PATH
Packit Service 672cf4
  orig_path = FIXED_SEARCH_PATH;
Packit Service 672cf4
#else
Packit Service 672cf4
  orig_path = getenv ("PATH");
Packit Service 672cf4
  if (!orig_path)
Packit Service 672cf4
    orig_path = "/bin:/usr/bin";
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
  fname = malloc (strlen (orig_path) + 1 + strlen (pgm) + 1);
Packit Service 672cf4
  if (!fname)
Packit Service 672cf4
    return NULL;
Packit Service 672cf4
Packit Service 672cf4
  path = orig_path;
Packit Service 672cf4
  for (;;)
Packit Service 672cf4
    {
Packit Service 672cf4
      for (s=path, p=fname; *s && *s != ':'; s++, p++)
Packit Service 672cf4
        *p = *s;
Packit Service 672cf4
      if (p != fname && p[-1] != '/')
Packit Service 672cf4
        *p++ = '/';
Packit Service 672cf4
      strcpy (p, pgm);
Packit Service 672cf4
      if (!access (fname, X_OK))
Packit Service 672cf4
        return fname;
Packit Service 672cf4
      if (!*s)
Packit Service 672cf4
        break;
Packit Service 672cf4
      path = s + 1;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 6c01f9
  _gpgme_debug (DEBUG_ENGINE, "gpgme-walk_path: '%s' not found in '%s'",
Packit Service 672cf4
                pgm, orig_path);
Packit Service 672cf4
Packit Service 672cf4
  free (fname);
Packit Service 672cf4
  return NULL;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Return the full file name of the GPG binary.  This function is used
Packit Service 672cf4
   if gpgconf was not found and thus it can be assumed that gpg2 is
Packit Service 672cf4
   not installed.  This function is only called by get_gpgconf_item
Packit Service 672cf4
   and may not be called concurrently.  */
Packit Service 672cf4
char *
Packit Service 672cf4
_gpgme_get_gpg_path (void)
Packit Service 672cf4
{
Packit Service 672cf4
  return walk_path (default_gpg_name? default_gpg_name : "gpg");
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* This function is only called by get_gpgconf_item and may not be
Packit Service 672cf4
   called concurrently.  */
Packit Service 672cf4
char *
Packit Service 672cf4
_gpgme_get_gpgconf_path (void)
Packit Service 672cf4
{
Packit Service 672cf4
  return walk_path (default_gpgconf_name? default_gpgconf_name : "gpgconf");
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* See w32-util.c */
Packit Service 672cf4
int
Packit Service 672cf4
_gpgme_get_conf_int (const char *key, int *value)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)key;
Packit Service 672cf4
  (void)value;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_allow_set_foreground_window (pid_t pid)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)pid;
Packit Service 672cf4
  /* Not needed.  */
Packit Service 672cf4
}