sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit f5b7e549b89630be8affc5e65277eb7525836440
parent 6f73cd1c97438bd0be090078d83cf0f4f161ef4d
Author: FRIGN <dev@frign.de>
Date:   Sun, 25 Jan 2015 21:22:17 +0100

Add mandoc-manpage for fold(1) and refactor code

and mark it as finished in the README.
In the code, use size_t rather than long.

Diffstat:
Mfold.1 | 69++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mfold.c | 119++++++++++++++++++++++++++++++++++++++-----------------------------------------
2 files changed, 101 insertions(+), 87 deletions(-)

diff --git a/fold.1 b/fold.1 @@ -1,25 +1,44 @@ -.TH FOLD 1 sbase\-VERSION -.SH NAME -fold \- wrap lines to width -.SH SYNOPSIS -.B fold -.RB [ \-bs ] -.RB [ \-w -.IR width ] -.RI [ file ...] -.SH DESCRIPTION -.B fold -reads each file in sequence and prints its lines, broken such that no line -exceeds 80 UTF-8 characters. If no file is given, fold reads from stdin. -.SH OPTIONS -.TP -.B \-b -counts bytes rather than characters. -.TP -.B \-s -breaks only at spaces. -.TP -.BI \-w " width" -breaks at -.I width -characters, instead of 80. +.Dd January 25th, 2015 +.Dt FOLD 1 sbase\-VERSION +.Sh NAME +.Nm fold +.Nd wrap lines to width +.Sh SYNOPSIS +.Nm fold +.Op Fl bs +.Op Fl w Ar width +.Op Fl N +.Op Ar file ... +.Sh DESCRIPTION +.Nm +reads each +.Ar file +and prints its lines wrapped such that no line +exceeds a certain width. +If no file is given, +.Nm +reads from stdin. +.Sh OPTIONS +.Bl -tag -width Ds +.It Fl b +Count bytes rather than characters. +.It Fl s +If a line contains spaces, break +at the last space within +.Ar width . +.It Fl w Ar width | Fl N +Break at +.Ar width +.Sy | N +characters. Default is 80. +.El +.Sh STANDARDS +The +.Nm +utility is compliant with the +.St -p1003.1-2008 +specification. +.Pp +The +.Op Fl N +flag is an extension to that specification. diff --git a/fold.c b/fold.c @@ -4,25 +4,74 @@ #include <stdlib.h> #include <unistd.h> -#include "text.h" #include "util.h" -static void fold(FILE *, long); -static void foldline(const char *, long); - static int bflag = 0; static int sflag = 0; static void +foldline(const char *str, size_t width) +{ + int space; + size_t i = 0, n = 0, col, j; + char c; + + do { + space = 0; + for (j = i, col = 0; str[j] && col <= width; j++) { + c = str[j]; + if (!UTF8_POINT(c) && !bflag) + continue; + if (sflag && isspace(c)) { + space = 1; + n = j + 1; + } + else if (!space) + n = j; + + if (!bflag && iscntrl(c)) + switch(c) { + case '\b': + col--; + break; + case '\r': + col = 0; + break; + case '\t': + col += (col + 1) % 8; + break; + } + else + col++; + } + if (fwrite(&str[i], 1, n - i, stdout) != n - i) + eprintf("<stdout>: write error:"); + if (str[n]) + putchar('\n'); + } while (str[i = n] && str[i] != '\n'); +} + +static void +fold(FILE *fp, long width) +{ + char *buf = NULL; + size_t size = 0; + + while (getline(&buf, &size, fp) != -1) + foldline(buf, width); + free(buf); +} + +static void usage(void) { - eprintf("usage: %s [-bs] [-w width] [FILE...]\n", argv0); + eprintf("usage: %s [-bs] [-w width] [-N] [FILE...]\n", argv0); } int main(int argc, char *argv[]) { - long width = 80; + size_t width = 80; FILE *fp; ARGBEGIN { @@ -42,9 +91,9 @@ main(int argc, char *argv[]) usage(); } ARGEND; - if (argc == 0) { + if (argc == 0) fold(stdin, width); - } else { + else { for (; argc > 0; argc--, argv++) { if (!(fp = fopen(argv[0], "r"))) { weprintf("fopen %s:", argv[0]); @@ -57,57 +106,3 @@ main(int argc, char *argv[]) return 0; } - -static void -fold(FILE *fp, long width) -{ - char *buf = NULL; - size_t size = 0; - - while (getline(&buf, &size, fp) != -1) - foldline(buf, width); - free(buf); -} - -static void -foldline(const char *str, long width) -{ - int space; - long col, j; - size_t i = 0, n = 0; - int c; - - do { - space = 0; - for (j = i, col = 0; str[j] && col <= width; j++) { - c = str[j]; - if (!UTF8_POINT(c) && !bflag) - continue; - if (sflag && isspace(c)) { - space = 1; - n = j+1; - } - else if (!space) - n = j; - - if (!bflag && iscntrl(c)) - switch(c) { - case '\b': - col--; - break; - case '\r': - col = 0; - break; - case '\t': - col += (col+1) % 8; - break; - } - else - col++; - } - if (fwrite(&str[i], 1, n-i, stdout) != n-i) - eprintf("<stdout>: write error:"); - if (str[n]) - putchar('\n'); - } while (str[i = n] && str[i] != '\n'); -}