commit 942c3613bc0a6b2d7fe9cd79790884111fe27313
parent 3fa85f0f5e9480d9ded4f7ebc535634d79389987
Author: FRIGN <dev@frign.de>
Date: Fri, 13 Mar 2015 23:47:41 +0100
Audit fold(1)
1) Use num-wording in the manpage, remove offensive remark against
the beloved -num-syntax <3.
2) Style changes.
3) Report errors of getline.
4) argv-argc-centric argument loop.
5) Rename r to ret for consistency.
Diffstat:
M | README | | | 2 | +- |
M | fold.1 | | | 15 | +++++++-------- |
M | fold.c | | | 49 | ++++++++++++++++++++++++++----------------------- |
3 files changed, 34 insertions(+), 32 deletions(-)
diff --git a/README b/README
@@ -33,7 +33,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
#* expr yes none
=*| false yes none
= find yes none
-#* fold yes none
+#*| fold yes none
=* grep yes none
=*| head yes none
=*| hostname non-posix none
diff --git a/fold.1 b/fold.1
@@ -1,4 +1,4 @@
-.Dd March 5, 2015
+.Dd March 13, 2015
.Dt FOLD 1
.Os sbase
.Sh NAME
@@ -7,7 +7,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl bs
-.Op Fl w Ar width | Fl Ns Ar width
+.Op Fl w Ar num | Fl Ns Ar num
.Op Ar file ...
.Sh DESCRIPTION
.Nm
@@ -24,11 +24,10 @@ reads from stdin.
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 Ns Ar width
+at the last space within width.
+.It Fl w Ar num | Fl Ns Ar num
Break at
-.Ar width
+.Ar num
characters. Default is 80.
.El
.Sh STANDARDS
@@ -38,6 +37,6 @@ utility is compliant with the
.St -p1003.1-2008
specification.
.Pp
-The obsolete
-.Op Fl Ns Ar width
+The
+.Op Fl Ns Ar num
syntax is an extension to that specification.
diff --git a/fold.c b/fold.c
@@ -6,14 +6,15 @@
#include "util.h"
-static int bflag = 0;
-static int sflag = 0;
+static int bflag = 0;
+static int sflag = 0;
+static size_t width = 80;
static void
-foldline(const char *str, size_t width)
+foldline(const char *str)
{
- int space;
size_t i = 0, n = 0, col, j;
+ int space;
char c;
do {
@@ -25,11 +26,11 @@ foldline(const char *str, size_t width)
if (sflag && isspace(c)) {
space = 1;
n = j + 1;
- }
- else if (!space)
+ } else if (!space) {
n = j;
+ }
- if (!bflag && iscntrl(c))
+ if (!bflag && iscntrl(c)) {
switch(c) {
case '\b':
col--;
@@ -41,24 +42,27 @@ foldline(const char *str, size_t width)
col += (col + 1) % 8;
break;
}
- else
+ } else {
col++;
+ }
}
- if (fwrite(&str[i], 1, n - i, stdout) != n - i)
- eprintf("<stdout>: write error:");
+ if (fwrite(str + i, 1, n - i, stdout) != n - i)
+ eprintf("fwrite <stdout>:");
if (str[n])
putchar('\n');
} while (str[i = n] && str[i] != '\n');
}
static void
-fold(FILE *fp, size_t width)
+fold(FILE *fp, const char *fname)
{
char *buf = NULL;
size_t size = 0;
- while (getline(&buf, &size, fp) != -1)
- foldline(buf, width);
+ while (getline(&buf, &size, fp) >= 0)
+ foldline(buf);
+ if (ferror(fp))
+ eprintf("getline %s:", fname);
free(buf);
}
@@ -71,9 +75,8 @@ usage(void)
int
main(int argc, char *argv[])
{
- size_t width = 80;
FILE *fp;
- int r = 0;
+ int ret = 0;
ARGBEGIN {
case 'b':
@@ -92,19 +95,19 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- if (argc == 0) {
- fold(stdin, width);
+ if (!argc) {
+ fold(stdin, "<stdin>");
} else {
- for (; argc > 0; argc--, argv++) {
- if (!(fp = fopen(argv[0], "r"))) {
- weprintf("fopen %s:", argv[0]);
- r = 1;
+ for (; *argv; argc--, argv++) {
+ if (!(fp = fopen(*argv, "r"))) {
+ weprintf("fopen %s:", *argv);
+ ret = 1;
continue;
}
- fold(fp, width);
+ fold(fp, *argv);
fclose(fp);
}
}
- return r;
+ return ret;
}