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
.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.

123
fold.c
View File

@ -4,78 +4,17 @@
#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
usage(void)
{
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)
foldline(const char *str, size_t width)
{
int space;
long col, j;
size_t i = 0, n = 0;
int c;
size_t i = 0, n = 0, col, j;
char c;
do {
space = 0;
@ -111,3 +50,59 @@ foldline(const char *str, long width)
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] [-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;
}