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
|
||||
=*| false yes none
|
||||
= find yes none
|
||||
#* fold yes none
|
||||
#*| fold yes none
|
||||
=* grep yes none
|
||||
=*| head yes 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
|
||||
.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.
|
||||
|
|
45
fold.c
45
fold.c
|
@ -8,12 +8,13 @@
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user