Blame src/posix-util.c

Packit Service 672cf4
/* posix-util.c - Utility functions for Posix
Packit Service 0ef63b
 * Copyright (C) 2001 Werner Koch (dd9jn)
Packit Service 0ef63b
 * Copyright (C) 2001, 2002, 2004 g10 Code GmbH
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * This file is part of GPGME.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 0ef63b
 * under the terms of the GNU Lesser General Public License as
Packit Service 0ef63b
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 0ef63b
 * the License, or (at your option) any later version.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 0ef63b
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0ef63b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 0ef63b
 * Lesser General Public License for more details.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * You should have received a copy of the GNU Lesser General Public
Packit Service 0ef63b
 * License along with this program; if not, see <https://gnu.org/licenses/>.
Packit Service 0ef63b
 * SPDX-License-Identifier: LGPL-2.1-or-later
Packit Service 0ef63b
 */
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 0ef63b
  _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
Packit Service 0ef63b
                "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
}
Packit Service 0ef63b
Packit Service 0ef63b
/* See w32-util.c */
Packit Service 0ef63b
int
Packit Service 0ef63b
_gpgme_access (const char *path, int mode)
Packit Service 0ef63b
{
Packit Service 0ef63b
  return access (path, mode);
Packit Service 0ef63b
}