summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorKacper <kacper@mail.openlinux.dev>2025-12-14 18:10:13 +0100
committerKacper <kacper@mail.openlinux.dev>2025-12-14 19:28:34 +0100
commit872cf03f26c2801ae6c3008ce5fa0d7856f5f85d (patch)
tree94809812b71ee286eb5b74c70e4d08fc4c8dc057 /bin
parentec769a83bde09c76bd6ad9ee7f391036dba5cd97 (diff)
libc: implement err/warn functions
Diffstat (limited to 'bin')
-rw-r--r--bin/Kbuild2
-rw-r--r--bin/Kconfig46
-rwxr-xr-xbin/clear/clearbin0 -> 760 bytes
-rw-r--r--bin/clear/clear.c11
-rw-r--r--bin/echo/Kbuild4
-rwxr-xr-xbin/echo/echobin0 -> 51872 bytes
-rw-r--r--bin/echo/echo.c29
-rwxr-xr-xbin/false/falsebin0 -> 616 bytes
-rw-r--r--bin/free/Kbuild4
-rwxr-xr-xbin/free/freebin0 -> 52936 bytes
-rw-r--r--bin/free/free.c64
-rw-r--r--bin/sync/Kbuild3
-rw-r--r--bin/sync/arch/Kbuild1
-rw-r--r--bin/sync/arch/x86_64/Kbuild1
-rw-r--r--bin/sync/arch/x86_64/sync.s5
-rwxr-xr-xbin/sync/syncbin0 -> 608 bytes
-rwxr-xr-xbin/true/truebin0 -> 616 bytes
17 files changed, 146 insertions, 24 deletions
diff --git a/bin/Kbuild b/bin/Kbuild
index 5487a089..6e675c38 100644
--- a/bin/Kbuild
+++ b/bin/Kbuild
@@ -1,5 +1,7 @@
obj-y += clear/
obj-y += echo/
obj-y += false/
+obj-y += free/
obj-y += gzip/
+obj-y += sync/
obj-y += true/
diff --git a/bin/Kconfig b/bin/Kconfig
new file mode 100644
index 00000000..0dcab22e
--- /dev/null
+++ b/bin/Kconfig
@@ -0,0 +1,46 @@
+# bin/Kconfig
+
+menu "Binary Utilities Configuration"
+config BIN_CLEAR
+ bool "clear"
+ default y
+ help
+ Enable the 'clear' command to clear the terminal screen.
+
+config BIN_ECHO
+ bool "echo"
+ default y
+ help
+ Enable the 'echo' command to display a line of text.
+
+config BIN_FALSE
+ bool "false"
+ default y
+ help
+ Enable the 'false' command which returns a non-zero exit status.
+
+config BIN_FREE
+ bool "free"
+ default y
+ help
+ Enable the 'free' command to display memory usage.
+
+config BIN_GZIP
+ bool "gzip"
+ default n
+ help
+ Enable the 'gzip' command to compress files using the gzip algorithm.
+
+config BIN_SYNC
+ bool "sync"
+ default y
+ help
+ Enable the 'sync' command to flush filesystem buffers.
+
+config BIN_TRUE
+ bool "true"
+ default y
+ help
+ Enable the 'true' command which returns a zero exit status.
+
+endmenu
diff --git a/bin/clear/clear b/bin/clear/clear
new file mode 100755
index 00000000..27b775a0
--- /dev/null
+++ b/bin/clear/clear
Binary files differ
diff --git a/bin/clear/clear.c b/bin/clear/clear.c
index 9c371ea7..c5a7a7c0 100644
--- a/bin/clear/clear.c
+++ b/bin/clear/clear.c
@@ -1,14 +1,7 @@
-#include <sys/cdefs.h>
#include <unistd.h>
-int main(int argc, char **__unused argv)
+void _start(void)
{
- if (argc) {
- write(STDOUT_FILENO, "usage: clear\n", 13);
- return 0;
- }
-
write(STDOUT_FILENO, "\033[H\033[2J", 7);
-
- return 0;
+ _exit(0);
}
diff --git a/bin/echo/Kbuild b/bin/echo/Kbuild
index 919423fd..00d6ecdd 100644
--- a/bin/echo/Kbuild
+++ b/bin/echo/Kbuild
@@ -1,5 +1,3 @@
bin-y := echo
-
-libs-y += $(srctree)/lib/libc/libc.a
-
obj-y += echo.o
+libs-y += $(srctree)/lib/libc/libc.a
diff --git a/bin/echo/echo b/bin/echo/echo
new file mode 100755
index 00000000..95ff666c
--- /dev/null
+++ b/bin/echo/echo
Binary files differ
diff --git a/bin/echo/echo.c b/bin/echo/echo.c
index b5ceee1c..ee932898 100644
--- a/bin/echo/echo.c
+++ b/bin/echo/echo.c
@@ -1,30 +1,35 @@
+#include <alloca.h> // for alloca
+#include <err.h> // for errx
#include <string.h> // for strlen
#include <sys/uio.h> // for iovec, writev
#include <unistd.h> // for STDOUT_FILENO
int main(int argc, char **argv)
{
+ struct iovec *iov;
+ int n_iov;
+
if (argc <= 1)
return 0;
- struct iovec iov[2 * argc - 2];
- int iovcnt = 0;
+ iov = alloca(sizeof(struct iovec) * (int)(2 * argc));
+ n_iov = 0;
for (int i = 1; i < argc; i++) {
- iov[iovcnt].iov_base = argv[i];
- iov[iovcnt].iov_len = strlen(argv[i]);
- iovcnt++;
+ iov[n_iov].iov_base = argv[i];
+ iov[n_iov++].iov_len = strlen(argv[i]);
if (i < argc - 1) {
- iov[iovcnt].iov_base = " ";
- iov[iovcnt].iov_len = 1;
- iovcnt++;
+ iov[n_iov].iov_base = " ";
+ iov[n_iov++].iov_len = 1;
}
}
- iov[iovcnt].iov_base = "\n";
- iov[iovcnt].iov_len = 1;
- iovcnt++;
+ iov[n_iov].iov_base = "\n";
+ iov[n_iov++].iov_len = 1;
+
+ if (writev(STDOUT_FILENO, iov, n_iov) < 0)
+ err(1, "writev");
- return writev(STDOUT_FILENO, iov, iovcnt) < 0 ? 1 : 0;
+ return 0;
}
diff --git a/bin/false/false b/bin/false/false
new file mode 100755
index 00000000..627a3e2d
--- /dev/null
+++ b/bin/false/false
Binary files differ
diff --git a/bin/free/Kbuild b/bin/free/Kbuild
new file mode 100644
index 00000000..08613953
--- /dev/null
+++ b/bin/free/Kbuild
@@ -0,0 +1,4 @@
+bin-y := free
+obj-y += free.o
+libs-y += $(srctree)/lib/libc/libc.a
+
diff --git a/bin/free/free b/bin/free/free
new file mode 100755
index 00000000..31c93e2f
--- /dev/null
+++ b/bin/free/free
Binary files differ
diff --git a/bin/free/free.c b/bin/free/free.c
new file mode 100644
index 00000000..b42ad99a
--- /dev/null
+++ b/bin/free/free.c
@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include <sys/sysinfo.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+ int opt;
+ struct sysinfo info;
+ unsigned int shift;
+
+ while ((opt = getopt(argc, argv, "hmtbs")) != -1) {
+ switch (opt) {
+ case 'b': /* bytes */
+ shift = 0;
+ break;
+ case 'k': /* kibibytes */
+ shift = 10;
+ break;
+ case 'm': /* mebibytes */
+ shift = 20;
+ break;
+ case 'g': /* gibibytes */
+ shift = 30;
+ break;
+ default:
+ goto usage;
+ }
+ }
+
+ if (sysinfo(&info) < 0)
+ perror("sysinfo");
+
+ printf(" %13s%13s%13s%13s%13s\n", "total", "used", "free", "shared",
+ "buffers");
+
+ printf("Mem: %13llu%13llu%13llu%13llu%13llu\n",
+ (unsigned long long)((info.totalram * info.mem_unit) >> shift),
+ (unsigned long long)(((info.totalram - info.freeram) *
+ info.mem_unit) >>
+ shift),
+ (unsigned long long)((info.freeram * info.mem_unit) >> shift),
+ (unsigned long long)((info.sharedram * info.mem_unit) >> shift),
+ (unsigned long long)((info.bufferram * info.mem_unit) >> shift));
+
+ printf("-/+ buffers/cache:%13llu%13llu\n",
+ (unsigned long long)(((info.totalram - info.freeram -
+ info.bufferram) *
+ info.mem_unit) >>
+ shift),
+ (unsigned long long)(((info.freeram + info.bufferram) *
+ info.mem_unit) >>
+ shift));
+ printf("Swap:%13llu%13llu%13llu\n",
+ (unsigned long long)((info.totalswap * info.mem_unit) >> shift),
+ (unsigned long long)(((info.totalswap - info.freeswap) *
+ info.mem_unit) >>
+ shift),
+ (unsigned long long)((info.freeswap * info.mem_unit) >> shift));
+
+ return 0;
+usage:
+ write(STDOUT_FILENO, "usage: free [-hmtbs]\n", 21);
+ return 0;
+}
diff --git a/bin/sync/Kbuild b/bin/sync/Kbuild
new file mode 100644
index 00000000..14337916
--- /dev/null
+++ b/bin/sync/Kbuild
@@ -0,0 +1,3 @@
+bin-y := sync
+
+obj-y += arch/
diff --git a/bin/sync/arch/Kbuild b/bin/sync/arch/Kbuild
new file mode 100644
index 00000000..ad3a7486
--- /dev/null
+++ b/bin/sync/arch/Kbuild
@@ -0,0 +1 @@
+obj-y += $(ARCH)/
diff --git a/bin/sync/arch/x86_64/Kbuild b/bin/sync/arch/x86_64/Kbuild
new file mode 100644
index 00000000..7da95e2c
--- /dev/null
+++ b/bin/sync/arch/x86_64/Kbuild
@@ -0,0 +1 @@
+obj-y += sync.o
diff --git a/bin/sync/arch/x86_64/sync.s b/bin/sync/arch/x86_64/sync.s
new file mode 100644
index 00000000..f1fc7ffc
--- /dev/null
+++ b/bin/sync/arch/x86_64/sync.s
@@ -0,0 +1,5 @@
+.globl _start
+
+_start:
+ mov 162, %rax
+ syscall
diff --git a/bin/sync/sync b/bin/sync/sync
new file mode 100755
index 00000000..95965cdc
--- /dev/null
+++ b/bin/sync/sync
Binary files differ
diff --git a/bin/true/true b/bin/true/true
new file mode 100755
index 00000000..8f690cd7
--- /dev/null
+++ b/bin/true/true
Binary files differ