From 8c7143823be42dbbf4e67d6868c8279c0711b7e8 Mon Sep 17 00:00:00 2001 From: alistair Date: Fri, 12 Feb 2021 14:50:41 +1000 Subject: [PATCH] replace tabs w spaces --- Makefile | 6 ++++++ cowsay.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/Makefile b/Makefile index 9703ab1..8261ed2 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,12 @@ main: cowsay.c static: cowsay.c gcc -O3 -static cowsay.c -o cowsay +cosmo: cowsay.c + gcc -g -O -DCOSMOPOLITAN -static -fno-pie -no-pie -mno-red-zone -nostdlib -nostdinc -o cowsay.com.dbg cowsay.c -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -fuse-ld=bfd -Wl,-T,ape.lds -include cosmopolitan.h crt.o ape.o cosmopolitan.a + objcopy -SO binary cowsay.com.dbg cowsay.com + + + clean: cowsay rm cowsay diff --git a/cowsay.c b/cowsay.c index 77548d6..633fb86 100644 --- a/cowsay.c +++ b/cowsay.c @@ -1,7 +1,13 @@ +#ifndef COSMOPOLITAN #include #include #include #include +#endif + +#ifdef COSMOPOLITAN +#include "cosmopolitan.h" +#endif int read_file(FILE* in, char **out) { int size_of_buffer = 20; @@ -274,6 +280,39 @@ void print_single_line_bubble(char *text, int len) { } +int replace_tabs(char **rtext) { + char *text = *rtext; + size_t len = strlen(text); + + char *tab = strstr(text, "\t"); + int numtabs = 0; + + while (tab != NULL) { + numtabs++; + tab = strstr(tab, "\t"); + } + + len += 3 * numtabs; + char * result = malloc(len); + + int resulti = 0; + for (int i = 0; i < len; i++) { + if (text[i] == '\t') { + for (int j = 0; j < 4; j++) { + result[resulti++] = ' '; + } + } else { + result[resulti++] = text[i]; + } + } + + free(text); + *rtext = result; + + return numtabs; + +}; + void print_speech(char *text, int width) { int len = strlen(text); @@ -284,6 +323,7 @@ void print_speech(char *text, int width) { } width -= 2; // for margins + wrap_text(text, width); int line_count = 0;