From ba2f3b5d6277ae6aa0c5ff104c5048a71d06458f Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 4 Oct 2022 10:49:27 +0200 Subject: Fix bug when width is less than line length If the length of a line is longer than the width of the display we want to center on, `width - len` becomes negative which causes an infinite loop of printing spaces. This fixes that bug. --- center.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'center.c') diff --git a/center.c b/center.c index 06141b8..2c4ac0f 100644 --- a/center.c +++ b/center.c @@ -61,7 +61,7 @@ main(int argc, char **argv) char *endptr; void (*centerfunc)(FILE *) = center; - while ((opt = getopt(argc, argv, ":ew:")) != -1) { + while ((opt = getopt(argc, argv, ":elw:")) != -1) { switch (opt) { case 'e': lenfunc = utf8len; @@ -127,7 +127,7 @@ center(FILE *fp) } len = lenfunc(line) + tabs * 8 - tabs; - for (int i = (width - len) / 2; i; i--) + for (int i = (width - len) / 2; i >= 0; i--) putchar(' '); fputs(line, stdout); } @@ -183,7 +183,7 @@ center_by_longest(FILE *fp) line = STAILQ_FIRST(&list_head); while (line != NULL) { int len = longest; - for (int i = ((width - len) / 2); i; i--) + for (int i = ((width - len) / 2); i >= 0; i--) putchar(' '); fputs(line->buffer, stdout); -- cgit v1.2.3