From ef12b8c8456b15aecd6ac605571bf690e137638c Mon Sep 17 00:00:00 2001 From: abs Date: Fri, 15 Sep 2000 11:23:17 +0000 Subject: [PATCH] Add '-c' for center (from FreeBSD) --- usr.bin/fmt/fmt.1 | 8 +++++++- usr.bin/fmt/fmt.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/usr.bin/fmt/fmt.1 b/usr.bin/fmt/fmt.1 index ee099796dd8e..9e57c57d589b 100644 --- a/usr.bin/fmt/fmt.1 +++ b/usr.bin/fmt/fmt.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: fmt.1,v 1.4 1997/10/18 15:00:11 lukem Exp $ +.\" $NetBSD: fmt.1,v 1.5 2000/09/15 11:23:17 abs Exp $ .\" .\" Copyright (c) 1980, 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -41,6 +41,7 @@ .Nd simple text formatter .Sh SYNOPSIS .Nm +.Op Fl c .Oo .Ar goal .Op Ar maximum @@ -60,6 +61,11 @@ to 65 and the maximum to 75. The spacing at the beginning of the input lines is preserved in the output, as are blank lines and interword spacing. .Pp +.Fl c +instructs +.Nm fmt +to center the text. +.Pp .Nm is meant to format mail messages prior to sending, but may also be useful for other simple tasks. diff --git a/usr.bin/fmt/fmt.c b/usr.bin/fmt/fmt.c index b20a103a19bc..9a8178e803b1 100644 --- a/usr.bin/fmt/fmt.c +++ b/usr.bin/fmt/fmt.c @@ -1,4 +1,4 @@ -/* $NetBSD: fmt.c,v 1.11 1999/11/02 21:17:16 jwise Exp $ */ +/* $NetBSD: fmt.c,v 1.12 2000/09/15 11:23:17 abs Exp $ */ /* * Copyright (c) 1980, 1993 @@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ #if 0 static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93"; #endif -__RCSID("$NetBSD: fmt.c,v 1.11 1999/11/02 21:17:16 jwise Exp $"); +__RCSID("$NetBSD: fmt.c,v 1.12 2000/09/15 11:23:17 abs Exp $"); #endif /* not lint */ #include @@ -74,6 +74,7 @@ int max_length; /* Max line length in output */ int pfx; /* Current leading blank count */ int lineno; /* Current input line */ int mark; /* Last place we saw a head line */ +int center; char *headnames[] = {"To", "Subject", "Cc", 0}; @@ -116,6 +117,11 @@ main(argc, argv) /* * LIZ@UOM 6/18/85 -- Check for goal and max length arguments */ + if (argc > 1 && !strcmp(argv[1], "-c")) { + center++; + argc--; + argv++; + } if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) { argv++; argc--; @@ -162,6 +168,26 @@ fmt(fi) char *cp, *cp2; int c, col, add_space; + if (center) { + while (1) { + cp = fgets(linebuf, BUFSIZ, fi); + if (!cp) + return; + while (*cp && isspace(*cp)) + cp++; + cp2 = cp + strlen(cp) - 1; + while (cp2 > cp && isspace(*cp2)) + cp2--; + if (cp == cp2) + putchar('\n'); + col = cp2 - cp; + for (c = 0; c < (goal_length-col)/2; c++) + putchar(' '); + while (cp <= cp2) + putchar(*cp++); + putchar('\n'); + } + } c = getc(fi); while (c != EOF) { /*