diff options
author | Nicholas Noll <nbnoll@eml.cc> | 2020-06-18 19:49:01 -0700 |
---|---|---|
committer | Nicholas Noll <nbnoll@eml.cc> | 2020-06-18 19:49:01 -0700 |
commit | 81fed70379578f4fe6255fdf33699675179ffe91 (patch) | |
tree | 5518ee17eb5781ee7e50454f5805599fd8388fce /sys/cmd/echo/echo.c | |
parent | d62e5c1df92ee159d0203b75adebb50faf45badb (diff) |
feat: small coreutils added
Diffstat (limited to 'sys/cmd/echo/echo.c')
-rw-r--r-- | sys/cmd/echo/echo.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/sys/cmd/echo/echo.c b/sys/cmd/echo/echo.c new file mode 100644 index 0000000..adac611 --- /dev/null +++ b/sys/cmd/echo/echo.c @@ -0,0 +1,39 @@ +#include <u.h> +#include <libn.h> + +int +main(int argc, char *argv[]) +{ + char *s, *b, *c; + int i, nnl, len; + static char buf[2*1024]; + + nnl = (argc>1) && strcmp(argv[1], "-n")==0; + + len = 1; + for(i = 1+nnl; i < argc; i++) + len += strlen(argv[i])+1; + + if (len >= arrlen(buf)) { + s = b = calloc(len, sizeof *s); + if (!s) + exits("no memory"); + } else + s = b = buf; + + for (i = 1+nnl; i < argc; i++) { + c = argv[i]; + while(*c) + *b++ = *c++; + if (i < argc-1) + *b++ = ' '; + } + + if (!nnl) + *b++ = '\n'; + + if (write(1, s, b-s) < 0) + exits("write error"); + + exit(0); +} |