2008-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / sm / misc.c
blob38994725ebfe9407766cb1a7b03807c76d057223
1 /* misc.c - Miscellaneous fucntions
2 * Copyright (C) 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #ifdef HAVE_LOCALE_H
28 #include <locale.h>
29 #endif
31 #include "gpgsm.h"
32 #include "i18n.h"
33 #include "setenv.h"
35 /* Setup the environment so that the pinentry is able to get all
36 required information. This is used prior to an exec of the
37 protect-tool. */
38 void
39 setup_pinentry_env (void)
41 #ifndef HAVE_W32_SYSTEM
42 char *lc;
44 if (opt.display)
45 setenv ("DISPLAY", opt.display, 1);
47 /* Try to make sure that GPG_TTY has been set. This is needed if we
48 call for example the protect-tools with redirected stdin and thus
49 it won't be able to ge a default by itself. Try to do it here
50 but print a warning. */
51 if (opt.ttyname)
52 setenv ("GPG_TTY", opt.ttyname, 1);
53 else if (!(lc=getenv ("GPG_TTY")) || !*lc)
55 log_error (_("GPG_TTY has not been set - "
56 "using maybe bogus default\n"));
57 lc = ttyname (0);
58 if (!lc)
59 lc = "/dev/tty";
60 setenv ("GPG_TTY", lc, 1);
63 if (opt.ttytype)
64 setenv ("TERM", opt.ttytype, 1);
66 if (opt.lc_ctype)
67 setenv ("LC_CTYPE", opt.lc_ctype, 1);
68 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
69 else if ( (lc = setlocale (LC_CTYPE, "")) )
70 setenv ("LC_CTYPE", lc, 1);
71 #endif
73 if (opt.lc_messages)
74 setenv ("LC_MESSAGES", opt.lc_messages, 1);
75 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
76 else if ( (lc = setlocale (LC_MESSAGES, "")) )
77 setenv ("LC_MESSAGES", lc, 1);
78 #endif
80 if (opt.xauthority)
81 setenv ("XAUTHORITY", opt.xauthority, 1);
83 if (opt.pinentry_user_data)
84 setenv ("PINENTRY_USER_DATA", opt.pinentry_user_data, 1);
86 #endif /*!HAVE_W32_SYSTEM*/