mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>,
	"Claude Sonnet 4.5" <noreply@anthropic.com>
Subject: [PATCH] commands: cd: implement support for cd - and $OLDPWD
Date: Tue, 20 Jan 2026 13:27:35 +0100	[thread overview]
Message-ID: <20260120122741.634724-1-a.fatoum@pengutronix.de> (raw)

From: Ahmad Fatoum <a.fatoum@barebox.org>

We have no $PWD an we would have to populate it globally, because all of
barebox shares a single working directory. What we can easily do however
is populate $OLDPWD to give shell scripts a way to resolve some
artifacts relatively and then revert back to the original working
directory, so implement that as well as cd -.

Co-developed-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 commands/cd.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/commands/cd.c b/commands/cd.c
index 0238919bf26c..e42beb79b282 100644
--- a/commands/cd.c
+++ b/commands/cd.c
@@ -12,26 +12,48 @@
 #include <command.h>
 #include <fs.h>
 #include <errno.h>
+#include <environment.h>
 
 static int do_cd(int argc, char *argv[])
 {
+	const char *target_dir;
+	char *old_pwd;
 	int ret;
 
-	if (argc == 1)
-		ret = chdir("/");
-	else
-		ret = chdir(argv[1]);
+	if (argc == 1) {
+		target_dir = "/";
+	} else if (strcmp(argv[1], "-")) {
+		target_dir = argv[1];
+	} else {
+		/* cd - switches to previous directory */
+		target_dir = getenv("OLDPWD");
+		if (!target_dir) {
+			printf("cd: OLDPWD not set\n");
+			return 1;
+		}
+		/* Print the directory we're changing to, like bash does */
+		printf("%s\n", target_dir);
+	}
 
+	/* Save current directory before changing */
+	old_pwd = xstrdup(getcwd());
+
+	ret = chdir(target_dir);
 	if (ret) {
 		perror("chdir");
 		return 1;
 	}
 
+	setenv("OLDPWD", old_pwd);
+	free(old_pwd);
+
 	return 0;
 }
 
 BAREBOX_CMD_HELP_START(cd)
 BAREBOX_CMD_HELP_TEXT("If called without an argument, change to the root directory '/'.")
+BAREBOX_CMD_HELP_TEXT("Use 'cd -' to change to the previous directory.")
+BAREBOX_CMD_HELP_TEXT("Sets OLDPWD environment variables.")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(cd)
-- 
2.47.3




                 reply	other threads:[~2026-01-20 12:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260120122741.634724-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    --cc=noreply@anthropic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox