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/echo/echo.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'bin/echo/echo.c') 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; } -- cgit v1.2.3