Please consider the attached patch.

Regards,
Vito Caputo
>From 475efd760e495dd810a6c4fe3ca4082b8466a781 Mon Sep 17 00:00:00 2001
From: Vito Caputo <vcap...@gnugeneration.com>
Date: Sun, 15 Nov 2015 20:51:57 -0800
Subject: [PATCH] Process: implement RC_ADOPTWD for adopting the current
 working directory

This is an RC_CHDIR convenience helper which discovers the working directory
of the foreground window.
---
 src/comm.c    |  1 +
 src/process.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/src/comm.c b/src/comm.c
index 0744ff0..4ea10f8 100644
--- a/src/comm.c
+++ b/src/comm.c
@@ -42,6 +42,7 @@ struct comm comms[RC_LAST + 1] =
   { "aclumask",		ARGS_1|ARGS_ORMORE,		{NULL} },
   { "activity",		ARGS_1,				{NULL} },
   { "addacl",		ARGS_1234,			{NULL} },
+  { "adoptwd",		NEED_FORE|ARGS_0,		{NULL} },
   { "allpartial",	NEED_DISPLAY|ARGS_1,		{NULL} },
   { "altscreen",	ARGS_01,			{NULL} },
   { "at",		ARGS_2|ARGS_ORMORE,		{NULL} },
diff --git a/src/process.c b/src/process.c
index d450785..16c2c96 100644
--- a/src/process.c
+++ b/src/process.c
@@ -465,6 +465,7 @@ void InitKeytab()
 	ktab['Z'].nr = RC_RESET;
 	ktab['H'].nr = RC_LOG;
 	ktab['M'].nr = RC_MONITOR;
+	ktab['@'].nr = RC_ADOPTWD;
 	ktab['?'].nr = RC_HELP;
 	ktab['*'].nr = RC_DISPLAYS;
 	{
@@ -2095,6 +2096,24 @@ void DoAction(struct action *act, int key)
 		if (chdir(s) == -1)
 			OutputMsg(errno, "%s", s);
 		break;
+	case RC_ADOPTWD:
+		{
+			char proc[MAXPATHLEN];
+			char buf[MAXPATHLEN];
+
+			/* this sets the working directory of the screen process to that of the process @ the end of the pty */
+			if (!display || !fore)
+				break;
+			snprintf(proc, sizeof(proc), "/proc/%i/cwd", fore->w_pid);
+			bzero(buf, sizeof(buf));
+			if (readlink(proc, buf, sizeof(buf)) == -1)
+				Msg(0, "Unable to readlink \"%s\".", proc);
+			else if (chdir(buf) == -1)
+				Msg(0, "Unable to chdir to \"%s\".", buf);
+			else
+				Msg(0, "WD for future windows is now \"%s\".", buf);
+		}
+		break;
 	case RC_SHELL:
 	case RC_DEFSHELL:
 		if (ParseSaveStr(act, &ShellProg) == 0)
-- 
2.1.4

Reply via email to