From ec945f352178470d39aa0f7c407fc4ae859b34b0 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 8 Jan 2022 18:15:54 +0100 Subject: fs: implement pushd/popd chdir wrappers 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 Link: https://lore.barebox.org/20220108171555.588426-1-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer --- fs/fs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'fs') diff --git a/fs/fs.c b/fs/fs.c index 7da3a050c1..60fdb29078 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; -- cgit v1.2.3