aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/echo/echo.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/echo/echo.c')
-rw-r--r--sys/cmd/echo/echo.c39
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);
+}