Use gpgme interface for error handling to avoid linking with gpg-error.
[gpgme.git] / src / get-env.c
1 /* get_env.c - A getenv() replacement.
2    Copyright (C) 2003, 2004 g10 Code GmbH
3
4    This file is part of GPGME.
5  
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10    
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <string.h>
27
28 #include "util.h"
29
30 \f
31 #if defined(HAVE_THREAD_SAFE_GETENV) || !defined (HAVE_GETENV_R)
32 /* We prefer using getenv() if it is thread-safe.  */
33
34 /* Retrieve the environment variable NAME and return a copy of it in a
35    malloc()'ed buffer in *VALUE.  If the environment variable is not
36    set, return NULL in *VALUE.  */
37 gpgme_error_t
38 _gpgme_getenv (const char *name, char **value)
39 {
40   char *env_value;
41
42   env_value = getenv (name);
43   if (!env_value)
44     *value = NULL;
45   else
46     {
47       *value = strdup (env_value);
48       if (!*value)
49         return gpg_error_from_errno (errno);
50     }
51   return 0;
52 }
53
54 #else
55
56 /* FIXME: Implement this when we have the specification for it.  */
57 #error Use of getenv_r not implemented.
58
59 #endif