mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] fs: implement pushd/popd chdir wrappers
@ 2022-01-08 17:15 Ahmad Fatoum
  2022-01-08 17:15 ` [PATCH 2/2] commands: add new tutorial command Ahmad Fatoum
  2022-01-12 10:29 ` [PATCH 1/2] fs: implement pushd/popd chdir wrappers Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-01-08 17:15 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We don't have dirfd's, so running operations relative to the current
working directory always involves some allocation/freeing boilerplate.

Add pushd/popd helpers to move the boilerplate out of the callsites.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/fs.c          | 30 ++++++++++++++++++++++++++++++
 include/unistd.h |  2 ++
 2 files changed, 32 insertions(+)

diff --git a/fs/fs.c b/fs/fs.c
index 7da3a050c1cb..60fdb29078d6 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2846,6 +2846,36 @@ out:
 }
 EXPORT_SYMBOL(chdir);
 
+char *pushd(const char *dir)
+{
+	char *oldcwd;
+	int ret;
+
+	oldcwd = strdup(getcwd());
+	if (!oldcwd)
+		return NULL;
+
+	ret = chdir(dir);
+	if (ret) {
+		free(oldcwd);
+		return NULL;
+	}
+
+	return oldcwd;
+}
+
+int popd(char *oldcwd)
+{
+	int ret;
+
+	if (!oldcwd)
+		return 0;
+
+	ret = chdir(oldcwd);
+	free(oldcwd);
+	return ret;
+}
+
 static char *get_linux_mmcblkdev(struct fs_device_d *fsdev)
 {
 	struct cdev *cdevm, *cdev;
diff --git a/include/unistd.h b/include/unistd.h
index 06ce3558099f..f7fe737d002b 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -20,6 +20,8 @@ int rmdir (const char *pathname);
 int symlink(const char *pathname, const char *newpath);
 int readlink(const char *path, char *buf, size_t bufsiz);
 int chdir(const char *pathname);
+char *pushd(const char *dir);
+int popd(char *dir);
 const char *getcwd(void);
 int ftruncate(int fd, loff_t length);
 
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-12 10:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-08 17:15 [PATCH 1/2] fs: implement pushd/popd chdir wrappers Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 2/2] commands: add new tutorial command Ahmad Fatoum
2022-01-12 10:29 ` [PATCH 1/2] fs: implement pushd/popd chdir wrappers Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox