[PATCH 1/2] fs: implement pushd/popd chdir wrappers
Ahmad Fatoum
a.fatoum at pengutronix.de
Sat Jan 8 09:15:54 PST 2022
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 at 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
More information about the barebox
mailing list