[hackers] [sbase] Audit fold(1) || FRIGN

From: <git_AT_suckless.org>
Date: Sat, 14 Mar 2015 00:23:32 +0100 (CET)

commit 1acc411516d6b87859ce01328ff316f5cbb15c85
Author: FRIGN <dev_AT_frign.de>
Date: Fri Mar 13 23:47:41 2015 +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.

diff --git a/README b/README
index 167844f..24d71d6 100644
--- a/README
+++ b/README
_AT_@ -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
index 46f7004..84b295d 100644
--- a/fold.1
+++ b/fold.1
_AT_@ -1,4 +1,4 @@
-.Dd March 5, 2015
+.Dd March 13, 2015
 .Dt FOLD 1
 .Os sbase
 .Sh NAME
_AT_@ -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
_AT_@ -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
_AT_@ -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
index 85eb3b1..020d429 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -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 {
_AT_@ -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--;
_AT_@ -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);
 }
 
_AT_@ -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':
_AT_@ -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;
 }
Received on Sat Mar 14 2015 - 00:23:32 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 14 2015 - 00:24:11 CET