Blame src/posix-util.c

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