Imported Upstream version 1.5.0
[platform/upstream/gpgme.git] / src / posix-util.c
1 /* posix-util.c - Utility functions for Posix
2    Copyright (C) 2001 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2004 g10 Code GmbH
4
5    This file is part of GPGME.
6
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.
11
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.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "util.h"
31 #include "sys-util.h"
32 #include "debug.h"
33
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;
38
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.  */
42 int
43 _gpgme_set_default_gpg_name (const char *name)
44 {
45   const char *s;
46
47   s = strrchr (name, '/');
48   if (s)
49     name = s + 1;
50
51   if (!default_gpg_name)
52     default_gpg_name = strdup (name);
53   return !default_gpg_name;
54 }
55
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.  */
59 int
60 _gpgme_set_default_gpgconf_name (const char *name)
61 {
62   const char *s;
63
64   s = strrchr (name, '/');
65   if (s)
66     name = s + 1;
67
68   if (!default_gpgconf_name)
69     default_gpgconf_name = strdup (name);
70   return !default_gpgconf_name;
71 }
72
73
74 /* Find an executable program PGM along the envvar PATH.  */
75 static char *
76 walk_path (const char *pgm)
77 {
78   const char *orig_path, *path, *s;
79   char *fname, *p;
80
81 #ifdef FIXED_SEARCH_PATH
82   orig_path = FIXED_SEARCH_PATH;
83 #else
84   orig_path = getenv ("PATH");
85   if (!orig_path)
86     orig_path = "/bin:/usr/bin";
87 #endif
88
89   fname = malloc (strlen (orig_path) + 1 + strlen (pgm) + 1);
90   if (!fname)
91     return NULL;
92
93   path = orig_path;
94   for (;;)
95     {
96       for (s=path, p=fname; *s && *s != ':'; s++, p++)
97         *p = *s;
98       if (p != fname && p[-1] != '/')
99         *p++ = '/';
100       strcpy (p, pgm);
101       if (!access (fname, X_OK))
102         return fname;
103       if (!*s)
104         break;
105       path = s + 1;
106     }
107
108   _gpgme_debug (DEBUG_ENGINE, "gpgme-walk_path: '%s' not found in '%s'",
109                 pgm, orig_path);
110
111   free (fname);
112   return NULL;
113 }
114
115
116 /* Return the full file name of the GPG binary.  This function is used
117    if gpgconf was not found and thus it can be assumed that gpg2 is
118    not installed.  This function is only called by get_gpgconf_item
119    and may not be called concurrently.  */
120 char *
121 _gpgme_get_gpg_path (void)
122 {
123   return walk_path (default_gpg_name? default_gpg_name : "gpg");
124 }
125
126
127 /* This function is only called by get_gpgconf_item and may not be
128    called concurrently.  */
129 char *
130 _gpgme_get_gpgconf_path (void)
131 {
132   return walk_path (default_gpgconf_name? default_gpgconf_name : "gpgconf");
133 }
134
135 /* See w32-util.c */
136 int
137 _gpgme_get_conf_int (const char *key, int *value)
138 {
139   return 0;
140 }
141
142 void
143 _gpgme_allow_set_foreground_window (pid_t pid)
144 {
145   (void)pid;
146   /* Not needed.  */
147 }