diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 18 | ||||
-rw-r--r-- | nt.c | 48 |
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 @@ -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); +} |