diff options
| author | Kacper <kacper@mail.openlinux.dev> | 2025-12-14 18:10:13 +0100 |
|---|---|---|
| committer | Kacper <kacper@mail.openlinux.dev> | 2025-12-14 19:28:34 +0100 |
| commit | 872cf03f26c2801ae6c3008ce5fa0d7856f5f85d (patch) | |
| tree | 94809812b71ee286eb5b74c70e4d08fc4c8dc057 /bin/echo | |
| parent | ec769a83bde09c76bd6ad9ee7f391036dba5cd97 (diff) | |
libc: implement err/warn functions
Diffstat (limited to 'bin/echo')
| -rw-r--r-- | bin/echo/Kbuild | 4 | ||||
| -rwxr-xr-x | bin/echo/echo | bin | 0 -> 51872 bytes | |||
| -rw-r--r-- | bin/echo/echo.c | 29 |
3 files changed, 18 insertions, 15 deletions
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 Binary files differnew file mode 100755 index 00000000..95ff666c --- /dev/null +++ b/bin/echo/echo 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; } |
