From 781716c240da330d57f27bf22f48017f9132a489 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 10 Jun 2019 00:22:48 +0200 Subject: Add pushd/popd utility Signed-off-by: David Oberhollenzer --- lib/util/dirstack.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lib/util/dirstack.c (limited to 'lib/util') diff --git a/lib/util/dirstack.c b/lib/util/dirstack.c new file mode 100644 index 0000000..7bfd1a2 --- /dev/null +++ b/lib/util/dirstack.c @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" + +#define STACK_DEPTH 128 + +static int dirstack[STACK_DEPTH]; +static int stacktop = 0; + +int pushd(const char *path) +{ + int fd; + + assert(stacktop < STACK_DEPTH); + + fd = open(".", O_DIRECTORY | O_PATH | O_RDONLY | O_CLOEXEC); + + if (fd < 0) { + perror("open ./"); + return -1; + } + + if (chdir(path)) { + perror(path); + close(fd); + return -1; + } + + dirstack[stacktop++] = fd; + return 0; +} + +int pushdn(const char *path, size_t len) +{ + char *temp; + int ret; + + temp = strndup(path, len); + if (temp == NULL) { + perror("pushd"); + return -1; + } + + ret = pushd(temp); + + free(temp); + return ret; +} + +int popd(void) +{ + int fd; + + assert(stacktop > 0); + + fd = dirstack[stacktop - 1]; + + if (fchdir(fd)) { + perror("popd"); + return -1; + } + + --stacktop; + close(fd); + return 0; +} -- cgit v1.2.3