summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-08-19 00:28:27 +0200
committerAki <please@ignore.pl>2023-08-19 00:28:27 +0200
commita3b048cdd0dae509ac8fafa8ee6d4536bf8e48c7 (patch)
tree59f0629986db4c330e96a7ce9a3606ff1701df3c
downloadnt-a3b048cdd0dae509ac8fafa8ee6d4536bf8e48c7.zip
nt-a3b048cdd0dae509ac8fafa8ee6d4536bf8e48c7.tar.gz
nt-a3b048cdd0dae509ac8fafa8ee6d4536bf8e48c7.tar.bz2
Overengineered a program that repeats things in a line
-rw-r--r--.gitignore1
-rw-r--r--Makefile18
-rw-r--r--nt.c48
3 files changed, 67 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ad13429
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+nt
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..73f8388
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+CFLAGS=-Wall -Wextra -Wpedantic -O3 -D_POSIX_C_SOURCE=200809L
+
+PREFIX=/usr/local
+BINDIR=$(PREFIX)/bin
+
+all: nt
+
+clean:
+ rm -f nt
+
+install: all
+ install -Dm755 nt -t $(DESTDIR)$(BINDIR)/
+
+uninstall:
+ rm -f $(DESTDIR)$(BINDIR)/nt
+
+.POSIX:
+.PHONY: clean install uninstall
diff --git a/nt.c b/nt.c
new file mode 100644
index 0000000..bc00216
--- /dev/null
+++ b/nt.c
@@ -0,0 +1,48 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+/// Prints *str* *times* to standard output.
+int
+repeat(const int times, const char* str)
+{
+ for (int i = 0; i < times; ++i)
+ printf("%s", str);
+ return printf("\n");
+}
+
+
+/// Concatenates strings from *strv*. User needs to free allocated string.
+char*
+concat(const int strc, const char* strv[])
+{
+ assert(0 < strc);
+ int i;
+ int len = 0;
+ for (i = 0; i < strc; ++i)
+ len += 1 + strlen(strv[i]);
+ char* const str = malloc(len);
+ str[0] = 0;
+ char* ptr = stpcpy(str, strv[0]);
+ for (int i = 1; i < strc; ++i) {
+ *ptr = ' ';
+ *++ptr = 0;
+ ptr = stpcpy(ptr, strv[i]);
+ }
+ return str;
+}
+
+
+int
+main(const int argc, const char* argv[])
+{
+ if (3 > argc)
+ return 1;
+ const int times = atoi(argv[1]);
+ if (1 > times)
+ return 1;
+ const char* str = concat(argc - 2, &argv[2]);
+ return repeat(times, str);
+}