-int pgp_use_gpg_agent (void)
-{
- return option (OPTUSEGPGAGENT) && getenv ("GPG_TTY")
- && getenv ("GPG_AGENT_INFO");
+int pgp_use_gpg_agent (void) {
+ char *tty, *ttybuf;
+
+ if (!option (OPTUSEGPGAGENT) || !getenv ("GPG_AGENT_INFO"))
+ return 0;
+
+ if ((tty = ttyname(0)) &&
+ ((ttybuf = mem_malloc (sizeof("GPG_TTY") + strlen(tty) + 1)))) {
+ snprintf (ttybuf, sizeof (ttybuf), "GPG_TTY=%s", tty);
+ putenv (ttybuf);
+ }
+
+ return 1;