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.
This commit is contained in:
parent
3fa85f0f5e
commit
942c3613bc
2
README
2
README
|
@ -33,7 +33,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
||||||
#* expr yes none
|
#* expr yes none
|
||||||
=*| false yes none
|
=*| false yes none
|
||||||
= find yes none
|
= find yes none
|
||||||
#* fold yes none
|
#*| fold yes none
|
||||||
=* grep yes none
|
=* grep yes none
|
||||||
=*| head yes none
|
=*| head yes none
|
||||||
=*| hostname non-posix none
|
=*| hostname non-posix none
|
||||||
|
|
15
fold.1
15
fold.1
|
@ -1,4 +1,4 @@
|
||||||
.Dd March 5, 2015
|
.Dd March 13, 2015
|
||||||
.Dt FOLD 1
|
.Dt FOLD 1
|
||||||
.Os sbase
|
.Os sbase
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
|
@ -7,7 +7,7 @@
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl bs
|
.Op Fl bs
|
||||||
.Op Fl w Ar width | Fl Ns Ar width
|
.Op Fl w Ar num | Fl Ns Ar num
|
||||||
.Op Ar file ...
|
.Op Ar file ...
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
.Nm
|
.Nm
|
||||||
|
@ -24,11 +24,10 @@ reads from stdin.
|
||||||
Count bytes rather than characters.
|
Count bytes rather than characters.
|
||||||
.It Fl s
|
.It Fl s
|
||||||
If a line contains spaces, break
|
If a line contains spaces, break
|
||||||
at the last space within
|
at the last space within width.
|
||||||
.Ar width .
|
.It Fl w Ar num | Fl Ns Ar num
|
||||||
.It Fl w Ar width | Fl Ns Ar width
|
|
||||||
Break at
|
Break at
|
||||||
.Ar width
|
.Ar num
|
||||||
characters. Default is 80.
|
characters. Default is 80.
|
||||||
.El
|
.El
|
||||||
.Sh STANDARDS
|
.Sh STANDARDS
|
||||||
|
@ -38,6 +37,6 @@ utility is compliant with the
|
||||||
.St -p1003.1-2008
|
.St -p1003.1-2008
|
||||||
specification.
|
specification.
|
||||||
.Pp
|
.Pp
|
||||||
The obsolete
|
The
|
||||||
.Op Fl Ns Ar width
|
.Op Fl Ns Ar num
|
||||||
syntax is an extension to that specification.
|
syntax is an extension to that specification.
|
||||||
|
|
49
fold.c
49
fold.c
|
@ -6,14 +6,15 @@
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int bflag = 0;
|
static int bflag = 0;
|
||||||
static int sflag = 0;
|
static int sflag = 0;
|
||||||
|
static size_t width = 80;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
foldline(const char *str, size_t width)
|
foldline(const char *str)
|
||||||
{
|
{
|
||||||
int space;
|
|
||||||
size_t i = 0, n = 0, col, j;
|
size_t i = 0, n = 0, col, j;
|
||||||
|
int space;
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
@ -25,11 +26,11 @@ foldline(const char *str, size_t width)
|
||||||
if (sflag && isspace(c)) {
|
if (sflag && isspace(c)) {
|
||||||
space = 1;
|
space = 1;
|
||||||
n = j + 1;
|
n = j + 1;
|
||||||
}
|
} else if (!space) {
|
||||||
else if (!space)
|
|
||||||
n = j;
|
n = j;
|
||||||
|
}
|
||||||
|
|
||||||
if (!bflag && iscntrl(c))
|
if (!bflag && iscntrl(c)) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case '\b':
|
case '\b':
|
||||||
col--;
|
col--;
|
||||||
|
@ -41,24 +42,27 @@ foldline(const char *str, size_t width)
|
||||||
col += (col + 1) % 8;
|
col += (col + 1) % 8;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
} else {
|
||||||
col++;
|
col++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (fwrite(&str[i], 1, n - i, stdout) != n - i)
|
if (fwrite(str + i, 1, n - i, stdout) != n - i)
|
||||||
eprintf("<stdout>: write error:");
|
eprintf("fwrite <stdout>:");
|
||||||
if (str[n])
|
if (str[n])
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
} while (str[i = n] && str[i] != '\n');
|
} while (str[i = n] && str[i] != '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
fold(FILE *fp, size_t width)
|
fold(FILE *fp, const char *fname)
|
||||||
{
|
{
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
while (getline(&buf, &size, fp) != -1)
|
while (getline(&buf, &size, fp) >= 0)
|
||||||
foldline(buf, width);
|
foldline(buf);
|
||||||
|
if (ferror(fp))
|
||||||
|
eprintf("getline %s:", fname);
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,9 +75,8 @@ usage(void)
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
size_t width = 80;
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int r = 0;
|
int ret = 0;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'b':
|
case 'b':
|
||||||
|
@ -92,19 +95,19 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (argc == 0) {
|
if (!argc) {
|
||||||
fold(stdin, width);
|
fold(stdin, "<stdin>");
|
||||||
} else {
|
} else {
|
||||||
for (; argc > 0; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if (!(fp = fopen(argv[0], "r"))) {
|
if (!(fp = fopen(*argv, "r"))) {
|
||||||
weprintf("fopen %s:", argv[0]);
|
weprintf("fopen %s:", *argv);
|
||||||
r = 1;
|
ret = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fold(fp, width);
|
fold(fp, *argv);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user