diff options
| author | Kacper <kacper@mail.openlinux.dev> | 2025-12-14 20:04:27 +0100 |
|---|---|---|
| committer | Kacper <kacper@mail.openlinux.dev> | 2025-12-14 20:04:27 +0100 |
| commit | 8edbc85a705d10e1d520ca7acf7d97d23d7b20d7 (patch) | |
| tree | ef632ef33e1118cd42443c6bebda03ec4d6df3a0 /bin/pwd/pwd.c | |
| parent | 4d80a0e029936f44b7af92144bda4f9c8d31c586 (diff) | |
Add pwd command utility
Diffstat (limited to 'bin/pwd/pwd.c')
| -rw-r--r-- | bin/pwd/pwd.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/bin/pwd/pwd.c b/bin/pwd/pwd.c new file mode 100644 index 00000000..564b22e7 --- /dev/null +++ b/bin/pwd/pwd.c @@ -0,0 +1,63 @@ +#include <err.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/uio.h> +#include <stdio.h> +#include <unistd.h> + +int main(int argc, char **argv) +{ + const char *out = NULL; + struct iovec iov[2]; + char opt, mode = 'L'; + char cwd[PATH_MAX]; + + while ((opt = getopt(argc, argv, "")) != -1) { + switch (opt) { + case 'L': + case 'P': + mode = opt; + break; + default: + write(STDERR_FILENO, "usage: pwd [-L|-P]\n", 20); + return 1; + } + } + + if (getcwd(cwd, sizeof(cwd)) == NULL) { + perror("pwd: getcwd"); + return 1; + } + + if (mode == 'L') { + const char *pwd; + struct stat cst, pst; + + if ((pwd = getenv("PWD")) == NULL || *pwd != '/' || + stat(pwd, &pst) < 0) { + out = cwd; + } + + if (stat(cwd, &cst) < 0) { + perror("pwd: stat"); + } + + out = (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) ? + pwd : + cwd; + } + + iov[0].iov_base = (char *)out; + iov[0].iov_len = strlen(out); + + iov[1].iov_base = "\n"; + iov[1].iov_len = 1; + + if (writev(STDOUT_FILENO, iov, 2) < 0) { + err(1, "pwd"); + } + + return 0; +} |
