From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] fs: implement pushd/popd chdir wrappers
Date: Sat, 8 Jan 2022 18:15:54 +0100 [thread overview]
Message-ID: <20220108171555.588426-1-a.fatoum@pengutronix.de> (raw)
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
next reply other threads:[~2022-01-08 17:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-08 17:15 Ahmad Fatoum [this message]
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
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=20220108171555.588426-1-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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