From 872cf03f26c2801ae6c3008ce5fa0d7856f5f85d Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 14 Dec 2025 18:10:13 +0100 Subject: libc: implement err/warn functions --- bin/Kbuild | 2 ++ bin/Kconfig | 46 +++++++++++++++++++++++++++++++ bin/clear/clear | Bin 0 -> 760 bytes bin/clear/clear.c | 11 ++------ bin/echo/Kbuild | 4 +-- bin/echo/echo | Bin 0 -> 51872 bytes bin/echo/echo.c | 29 +++++++++++--------- bin/false/false | Bin 0 -> 616 bytes bin/free/Kbuild | 4 +++ bin/free/free | Bin 0 -> 52936 bytes bin/free/free.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ bin/sync/Kbuild | 3 +++ bin/sync/arch/Kbuild | 1 + bin/sync/arch/x86_64/Kbuild | 1 + bin/sync/arch/x86_64/sync.s | 5 ++++ bin/sync/sync | Bin 0 -> 608 bytes bin/true/true | Bin 0 -> 616 bytes 17 files changed, 146 insertions(+), 24 deletions(-) create mode 100644 bin/Kconfig create mode 100755 bin/clear/clear create mode 100755 bin/echo/echo create mode 100755 bin/false/false create mode 100644 bin/free/Kbuild create mode 100755 bin/free/free create mode 100644 bin/free/free.c create mode 100644 bin/sync/Kbuild create mode 100644 bin/sync/arch/Kbuild create mode 100644 bin/sync/arch/x86_64/Kbuild create mode 100644 bin/sync/arch/x86_64/sync.s create mode 100755 bin/sync/sync create mode 100755 bin/true/true (limited to 'bin') 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 Binary files /dev/null and b/bin/clear/clear 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 #include -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 Binary files /dev/null and b/bin/echo/echo 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 // for alloca +#include // for errx #include // for strlen #include // for iovec, writev #include // 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 Binary files /dev/null and b/bin/false/false 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 Binary files /dev/null and b/bin/free/free 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 +#include +#include + +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 Binary files /dev/null and b/bin/sync/sync differ diff --git a/bin/true/true b/bin/true/true new file mode 100755 index 00000000..8f690cd7 Binary files /dev/null and b/bin/true/true differ -- cgit v1.2.3