1 /* posix-util.c - Utility functions for Posix
2 * Copyright (C) 2001 Werner Koch (dd9jn)
3 * Copyright (C) 2001, 2002, 2004 g10 Code GmbH
5 * This file is part of GPGME.
7 * GPGME is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * GPGME is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <https://gnu.org/licenses/>.
19 * SPDX-License-Identifier: LGPL-2.1-or-later
34 /* These variables store the malloced name of alternative default
35 binaries. The are set only once by gpgme_set_global_flag. */
36 static char *default_gpg_name;
37 static char *default_gpgconf_name;
39 /* Set the default name for the gpg binary. This function may only be
40 called by gpgme_set_global_flag. Returns 0 on success. Leading
41 directories are removed from NAME. */
43 _gpgme_set_default_gpg_name (const char *name)
47 s = strrchr (name, '/');
51 if (!default_gpg_name)
52 default_gpg_name = strdup (name);
53 return !default_gpg_name;
56 /* Set the default name for the gpgconf binary. This function may
57 only be called by gpgme_set_global_flag. Returns 0 on success.
58 Leading directories are removed from NAME. */
60 _gpgme_set_default_gpgconf_name (const char *name)
64 s = strrchr (name, '/');
68 if (!default_gpgconf_name)
69 default_gpgconf_name = strdup (name);
70 return !default_gpgconf_name;
74 /* Dummy function - see w32-util.c for the actual code. */
76 _gpgme_set_override_inst_dir (const char *dir)
82 /* Find an executable program in the colon seperated paths. */
84 walk_path_str (const char *path_str, const char *pgm)
89 fname = malloc (strlen (path_str) + 1 + strlen (pgm) + 1);
96 for (s=path, p=fname; *s && *s != ':'; s++, p++)
98 if (p != fname && p[-1] != '/')
101 if (!access (fname, X_OK))
112 /* Find an executable program PGM. */
114 find_executable (const char *pgm)
116 const char *orig_path;
119 #ifdef FIXED_SEARCH_PATH
120 orig_path = FIXED_SEARCH_PATH;
122 orig_path = getenv ("PATH");
124 orig_path = "/bin:/usr/bin";
126 ret = walk_path_str (orig_path, pgm);
130 _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
131 "gpgme-walk_path: '%s' not found in '%s'",
135 /* On apple, especially when started through gpgme-json via
136 the browser interface we should look into some additional
138 const char *additional_path = "/usr/local/bin:/usr/local/MacGPG2/bin";
141 ret = walk_path_str (additional_path, pgm);
145 _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
146 "gpgme-walk_path: '%s' not found in '%s'",
147 pgm, additional_path);
155 /* Return the full file name of the GPG binary. This function is used
156 if gpgconf was not found and thus it can be assumed that gpg2 is
157 not installed. This function is only called by get_gpgconf_item
158 and may not be called concurrently. */
160 _gpgme_get_gpg_path (void)
162 return find_executable (default_gpg_name? default_gpg_name : "gpg");
166 /* This function is only called by get_gpgconf_item and may not be
167 called concurrently. */
169 _gpgme_get_gpgconf_path (void)
171 return find_executable (default_gpgconf_name? default_gpgconf_name : "gpgconf");
176 _gpgme_get_conf_int (const char *key, int *value)
184 _gpgme_allow_set_foreground_window (pid_t pid)
192 _gpgme_access (const char *path, int mode)
194 return access (path, mode);