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.
This commit is contained in:
FRIGN 2015-01-25 21:22:17 +01:00
parent 6f73cd1c97
commit f5b7e549b8
2 changed files with 101 additions and 87 deletions

69
fold.1
View File

@ -1,25 +1,44 @@
.TH FOLD 1 sbase\-VERSION .Dd January 25th, 2015
.SH NAME .Dt FOLD 1 sbase\-VERSION
fold \- wrap lines to width .Sh NAME
.SH SYNOPSIS .Nm fold
.B fold .Nd wrap lines to width
.RB [ \-bs ] .Sh SYNOPSIS
.RB [ \-w .Nm fold
.IR width ] .Op Fl bs
.RI [ file ...] .Op Fl w Ar width
.SH DESCRIPTION .Op Fl N
.B fold .Op Ar file ...
reads each file in sequence and prints its lines, broken such that no line .Sh DESCRIPTION
exceeds 80 UTF-8 characters. If no file is given, fold reads from stdin. .Nm
.SH OPTIONS reads each
.TP .Ar file
.B \-b and prints its lines wrapped such that no line
counts bytes rather than characters. exceeds a certain width.
.TP If no file is given,
.B \-s .Nm
breaks only at spaces. reads from stdin.
.TP .Sh OPTIONS
.BI \-w " width" .Bl -tag -width Ds
breaks at .It Fl b
.I width Count bytes rather than characters.
characters, instead of 80. .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.

123
fold.c
View File

@ -4,78 +4,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "text.h"
#include "util.h" #include "util.h"
static void fold(FILE *, long);
static void foldline(const char *, long);
static int bflag = 0; static int bflag = 0;
static int sflag = 0; static int sflag = 0;
static void static void
usage(void) foldline(const char *str, size_t width)
{
eprintf("usage: %s [-bs] [-w width] [FILE...]\n", argv0);
}
int
main(int argc, char *argv[])
{
long width = 80;
FILE *fp;
ARGBEGIN {
case 'b':
bflag = 1;
break;
case 's':
sflag = 1;
break;
case 'w':
width = estrtol(EARGF(usage()), 0);
break;
ARGNUM:
width = ARGNUMF(0);
break;
default:
usage();
} ARGEND;
if (argc == 0) {
fold(stdin, width);
} else {
for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]);
continue;
}
fold(fp, width);
fclose(fp);
}
}
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; int space;
long col, j; size_t i = 0, n = 0, col, j;
size_t i = 0, n = 0; char c;
int c;
do { do {
space = 0; space = 0;
@ -111,3 +50,59 @@ foldline(const char *str, long width)
putchar('\n'); putchar('\n');
} while (str[i = n] && str[i] != '\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] [-N] [FILE...]\n", argv0);
}
int
main(int argc, char *argv[])
{
size_t width = 80;
FILE *fp;
ARGBEGIN {
case 'b':
bflag = 1;
break;
case 's':
sflag = 1;
break;
case 'w':
width = estrtol(EARGF(usage()), 0);
break;
ARGNUM:
width = ARGNUMF(0);
break;
default:
usage();
} ARGEND;
if (argc == 0)
fold(stdin, width);
else {
for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]);
continue;
}
fold(fp, width);
fclose(fp);
}
}
return 0;
}