From 8edbc85a705d10e1d520ca7acf7d97d23d7b20d7 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 14 Dec 2025 20:04:27 +0100 Subject: Add pwd command utility --- LICENSE | 19 +++++++++++++++++ README | 24 +++++++++++++++++++++ bin/Kbuild | 1 + bin/clear/clear | Bin 760 -> 0 bytes bin/echo/echo | Bin 51872 -> 0 bytes bin/false/false | Bin 616 -> 0 bytes bin/free/free | Bin 52936 -> 0 bytes bin/pwd/Kbuild | 3 +++ bin/pwd/pwd.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/sync/sync | Bin 608 -> 0 bytes bin/true/true | Bin 616 -> 0 bytes makefile | 4 ---- 12 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 LICENSE create mode 100644 README delete mode 100755 bin/clear/clear delete mode 100755 bin/echo/echo delete mode 100755 bin/false/false delete mode 100755 bin/free/free create mode 100644 bin/pwd/Kbuild create mode 100644 bin/pwd/pwd.c delete mode 100755 bin/sync/sync delete mode 100755 bin/true/true diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..5337b5fd --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2025 Kacper Fiedorowicz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 00000000..acfd2403 --- /dev/null +++ b/README @@ -0,0 +1,24 @@ +Introduction +============ + +Openlinux is lightweight linux userspace distribution designed to be small, +portable and fast. It is definitely a work in progress. + +Building +======== + +The real system build is not done yet, but you can build the tools and libs +for development using make in the root directory. + +Configuration +============= + +The configuration is defined in a ".config" file in the root directory. +You can use menuconfig target in make to create/edit it. + +Architecture support +==================== + + arm64: Not yet + x86-64: Working + ...: Not planned for now diff --git a/bin/Kbuild b/bin/Kbuild index 6e675c38..4a96c667 100644 --- a/bin/Kbuild +++ b/bin/Kbuild @@ -3,5 +3,6 @@ obj-y += echo/ obj-y += false/ obj-y += free/ obj-y += gzip/ +obj-y += pwd/ obj-y += sync/ obj-y += true/ diff --git a/bin/clear/clear b/bin/clear/clear deleted file mode 100755 index 27b775a0..00000000 Binary files a/bin/clear/clear and /dev/null differ diff --git a/bin/echo/echo b/bin/echo/echo deleted file mode 100755 index 95ff666c..00000000 Binary files a/bin/echo/echo and /dev/null differ diff --git a/bin/false/false b/bin/false/false deleted file mode 100755 index 627a3e2d..00000000 Binary files a/bin/false/false and /dev/null differ diff --git a/bin/free/free b/bin/free/free deleted file mode 100755 index 31c93e2f..00000000 Binary files a/bin/free/free and /dev/null differ diff --git a/bin/pwd/Kbuild b/bin/pwd/Kbuild new file mode 100644 index 00000000..7abb0973 --- /dev/null +++ b/bin/pwd/Kbuild @@ -0,0 +1,3 @@ +bin-y := pwd +obj-y += pwd.o +libs-y += $(srctree)/lib/libc/libc.a 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 +#include +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/bin/sync/sync b/bin/sync/sync deleted file mode 100755 index 95965cdc..00000000 Binary files a/bin/sync/sync and /dev/null differ diff --git a/bin/true/true b/bin/true/true deleted file mode 100755 index 8f690cd7..00000000 Binary files a/bin/true/true and /dev/null differ diff --git a/makefile b/makefile index 7f039714..5f42ac85 100644 --- a/makefile +++ b/makefile @@ -155,10 +155,6 @@ PHONY += menuconfig menuconfig: $(Q)$(MAKE) -f scripts/kconfig/makefile menuconfig -PHONY += defconfig -defconfig: - $(Q)$(MAKE) -f scripts/kconfig/makefile defconfig - include-what-you-use: compile_commands.json $(Q)iwyu_tool.py -p. -j4 -- -Xiwyu --update_comments -Xiwyu --transitive_includes_only -Xiwyu --no_internal_mappings | \ fix_includes.py --comments \ -- cgit v1.2.3