Blame src/get-env.c

Packit d7e8d0
/* get_env.c - A getenv() replacement.
Packit Service 30b792
 * Copyright (C) 2003, 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
#if HAVE_CONFIG_H
Packit d7e8d0
#include <config.h>
Packit d7e8d0
#endif
Packit d7e8d0
#include <stdlib.h>
Packit d7e8d0
#include <errno.h>
Packit d7e8d0
#include <string.h>
Packit d7e8d0
Packit d7e8d0
#include "util.h"
Packit d7e8d0
Packit d7e8d0

Packit d7e8d0
/* Retrieve the environment variable NAME and return a copy of it in a
Packit d7e8d0
   malloc()'ed buffer in *VALUE.  If the environment variable is not
Packit d7e8d0
   set, return NULL in *VALUE.  */
Packit Service 30b792
Packit Service 30b792
#ifdef HAVE_GETENV_R
Packit Service 30b792
#define INITIAL_GETENV_SIZE 32
Packit Service 30b792
Packit Service 30b792
gpgme_error_t
Packit Service 30b792
_gpgme_getenv (const char *name, char **value)
Packit Service 30b792
{
Packit Service 30b792
  size_t len = INITIAL_GETENV_SIZE;
Packit Service 30b792
  char *env_value;
Packit Service 30b792
Packit Service 30b792
  env_value = malloc (len);
Packit Service 30b792
Packit Service 30b792
  while (1)
Packit Service 30b792
    {
Packit Service 30b792
      *value = env_value;
Packit Service 30b792
      if (!env_value)
Packit Service 30b792
        return gpg_error_from_syserror ();
Packit Service 30b792
Packit Service 30b792
      if (getenv_r (name, env_value, len) == 0)
Packit Service 30b792
        break;
Packit Service 30b792
Packit Service 30b792
      if (errno == ERANGE)
Packit Service 30b792
        {
Packit Service 30b792
          len *= 2;
Packit Service 30b792
          env_value = realloc (env_value, len);
Packit Service 30b792
        }
Packit Service 30b792
      else
Packit Service 30b792
        {
Packit Service 30b792
          int saved = errno;
Packit Service 30b792
Packit Service 30b792
          free (env_value);
Packit Service 30b792
          *value = NULL;
Packit Service 30b792
          if (errno == ENOENT)
Packit Service 30b792
            return 0;
Packit Service 30b792
          else
Packit Service 30b792
          return gpg_error_from_errno (saved);
Packit Service 30b792
        }
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
  return 0;
Packit Service 30b792
}
Packit Service 30b792
#else
Packit Service 30b792
#ifndef HAVE_THREAD_SAFE_GETENV
Packit Service 30b792
GPGRT_LOCK_DEFINE (environ_lock);
Packit Service 30b792
#endif
Packit Service 30b792
Packit d7e8d0
gpgme_error_t
Packit d7e8d0
_gpgme_getenv (const char *name, char **value)
Packit d7e8d0
{
Packit d7e8d0
  char *env_value;
Packit Service 30b792
  gpgme_error_t err = 0;
Packit d7e8d0
Packit Service 30b792
#ifndef HAVE_THREAD_SAFE_GETENV
Packit Service 30b792
  gpg_err_code_t rc;
Packit Service 30b792
Packit Service 30b792
  rc= gpgrt_lock_lock (&environ_lock);
Packit Service 30b792
  if (rc)
Packit Service 30b792
    {
Packit Service 30b792
      err = gpg_error (rc);
Packit Service 30b792
      goto leave;
Packit Service 30b792
    }
Packit Service 30b792
#endif
Packit d7e8d0
  env_value = getenv (name);
Packit d7e8d0
  if (!env_value)
Packit d7e8d0
    *value = NULL;
Packit d7e8d0
  else
Packit d7e8d0
    {
Packit d7e8d0
      *value = strdup (env_value);
Packit d7e8d0
      if (!*value)
Packit Service 30b792
	err = gpg_error_from_syserror ();
Packit d7e8d0
    }
Packit Service 30b792
#ifndef HAVE_THREAD_SAFE_GETENV
Packit Service 30b792
  rc = gpgrt_lock_unlock (&environ_lock);
Packit Service 30b792
  if (rc)
Packit Service 30b792
    err = gpg_error (rc);
Packit Service 30b792
 leave:
Packit Service 30b792
#endif
Packit Service 30b792
  return err;
Packit d7e8d0
}
Packit d7e8d0
#endif