2006-12-21 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / sm / misc.c
blob86cb506d62730fdf526b38043ce38241ab1c1956
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <unistd.h>
29 #ifdef HAVE_LOCALE_H
30 #include <locale.h>
31 #endif
33 #include "gpgsm.h"
34 #include "i18n.h"
35 #include "setenv.h"
37 /* Setup the environment so that the pinentry is able to get all
38 required information. This is used prior to an exec of the
39 protect-tool. */
40 void
41 setup_pinentry_env (void)
43 #ifndef HAVE_W32_SYSTEM
44 char *lc;
46 if (opt.display)
47 setenv ("DISPLAY", opt.display, 1);
49 /* Try to make sure that GPG_TTY has been set. This is needed if we
50 call for example the protect-tools with redirected stdin and thus
51 it won't be able to ge a default by itself. Try to do it here
52 but print a warning. */
53 if (opt.ttyname)
54 setenv ("GPG_TTY", opt.ttyname, 1);
55 else if (!(lc=getenv ("GPG_TTY")) || !*lc)
57 log_error (_("GPG_TTY has not been set - "
58 "using maybe bogus default\n"));
59 lc = ttyname (0);
60 if (!lc)
61 lc = "/dev/tty";
62 setenv ("GPG_TTY", lc, 1);
65 if (opt.ttytype)
66 setenv ("TERM", opt.ttytype, 1);
68 if (opt.lc_ctype)
69 setenv ("LC_CTYPE", opt.lc_ctype, 1);
70 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
71 else if ( (lc = setlocale (LC_CTYPE, "")) )
72 setenv ("LC_CTYPE", lc, 1);
73 #endif
75 if (opt.lc_messages)
76 setenv ("LC_MESSAGES", opt.lc_messages, 1);
77 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
78 else if ( (lc = setlocale (LC_MESSAGES, "")) )
79 setenv ("LC_MESSAGES", lc, 1);
80 #endif
81 #endif /*!HAVE_W32_SYSTEM*/