Re: Some cleanups and tweaks for wc(1)

2016-09-07 Thread Joerg Jung

> On 04 Sep 2016, at 00:06, Frederic Cambus <f...@statdns.com> wrote:
> 
> Hi tech@,
> 
> Some cleanups and tweaks for wc(1):
> 
> - Removed unnecessary string.h include
> - Changed 'format_and_print' argument type to int64_t and casting
>  inside the function
> - Declaring 'print_counts', 'format_and_print', and 'cnt' as static
> - Remove unnecessary cast for NULL, and (void) casts from printfs,
>  'mbtowc' and 'format_and_print' calls
> - In 'cnt', change bufsz type from ssize_t to size_t to avoid
>  converting between pointers to integer types with different sign
>  when calling getline (catched when compiling with Clang)
> - Use return instead of exit in main

ok jung@

> Index: wc.c
> ===
> RCS file: /cvs/src/usr.bin/wc/wc.c,v
> retrieving revision 1.20
> diff -u -p -u -p -r1.20 wc.c
> --- wc.c  8 Dec 2015 01:00:45 -   1.20
> +++ wc.c  8 May 2016 21:51:28 -
> @@ -34,7 +34,6 @@
> #include 
> #include 
> #include 
> -#include 
> #include 
> #include 
> #include 
> @@ -45,12 +44,12 @@
> 
> int64_t   tlinect, twordct, tcharct;
> int   doline, doword, dochar, humanchar, multibyte;
> -int  rval;
> +int  rval;
> extern char *__progname;
> 
> -void print_counts(int64_t, int64_t, int64_t, char *);
> -void format_and_print(long long);
> -void cnt(char *);
> +static void print_counts(int64_t, int64_t, int64_t, char *);
> +static void format_and_print(int64_t);
> +static void cnt(char *);
> 
> int
> main(int argc, char *argv[])
> @@ -82,10 +81,10 @@ main(int argc, char *argv[])
>   break;
>   case '?':
>   default:
> - (void)fprintf(stderr,
> + fprintf(stderr,
>   "usage: %s [-c | -m] [-hlw] [file ...]\n",
>   __progname);
> - exit(1);
> + return 1;
>   }
>   argv += optind;
>   argc -= optind;
> @@ -99,7 +98,7 @@ main(int argc, char *argv[])
>   doline = doword = dochar = 1;
> 
>   if (!*argv) {
> - cnt((char *)NULL);
> + cnt(NULL);
>   } else {
>   int dototal = (argc > 1);
> 
> @@ -111,14 +110,14 @@ main(int argc, char *argv[])
>   print_counts(tlinect, twordct, tcharct, "total");
>   }
> 
> - exit(rval);
> + return rval;
> }
> 
> -void
> +static void
> cnt(char *file)
> {
>   static char *buf;
> - static ssize_t bufsz;
> + static size_t bufsz;
> 
>   FILE *stream;
>   char *C;
> @@ -213,7 +212,7 @@ cnt(char *file)
>   ++charct;
>   len = mbtowc(, C, MB_CUR_MAX);
>   if (len == -1) {
> - (void)mbtowc(NULL, NULL,
> + mbtowc(NULL, NULL,
>   MB_CUR_MAX);
>   len = 1;
>   wc = L' ';
> @@ -263,31 +262,31 @@ cnt(char *file)
>   }
> }
> 
> -void 
> -format_and_print(long long v)
> +static void
> +format_and_print(int64_t v)
> {
>   if (humanchar) {
>   char result[FMT_SCALED_STRSIZE];
> 
> - (void)fmt_scaled(v, result);
> - (void)printf("%7s", result);
> + fmt_scaled((long long)v, result);
> + printf("%7s", result);
>   } else {
> - (void)printf(" %7lld", v);
> + printf(" %7lld", v);
>   }
> }
> 
> -void
> +static void
> print_counts(int64_t lines, int64_t words, int64_t chars, char *name)
> {
>   if (doline)
> - format_and_print((long long)lines);
> + format_and_print(lines);
>   if (doword)
> - format_and_print((long long)words);
> + format_and_print(words);
>   if (dochar)
> - format_and_print((long long)chars);
> + format_and_print(chars);
> 
>   if (name)
> - (void)printf(" %s\n", name);
> + printf(" %s\n", name);
>   else
> - (void)printf("\n");
> + printf("\n");
> }
> 



Some cleanups and tweaks for wc(1)

2016-09-03 Thread Frederic Cambus
Hi tech@,

Some cleanups and tweaks for wc(1):

- Removed unnecessary string.h include
- Changed 'format_and_print' argument type to int64_t and casting
  inside the function
- Declaring 'print_counts', 'format_and_print', and 'cnt' as static
- Remove unnecessary cast for NULL, and (void) casts from printfs,
  'mbtowc' and 'format_and_print' calls
- In 'cnt', change bufsz type from ssize_t to size_t to avoid
  converting between pointers to integer types with different sign
  when calling getline (catched when compiling with Clang)
- Use return instead of exit in main

Index: wc.c
===
RCS file: /cvs/src/usr.bin/wc/wc.c,v
retrieving revision 1.20
diff -u -p -u -p -r1.20 wc.c
--- wc.c8 Dec 2015 01:00:45 -   1.20
+++ wc.c8 May 2016 21:51:28 -
@@ -34,7 +34,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -45,12 +44,12 @@
 
 int64_ttlinect, twordct, tcharct;
 intdoline, doword, dochar, humanchar, multibyte;
-intrval;
+intrval;
 extern char *__progname;
 
-void   print_counts(int64_t, int64_t, int64_t, char *);
-void   format_and_print(long long);
-void   cnt(char *);
+static void print_counts(int64_t, int64_t, int64_t, char *);
+static void format_and_print(int64_t);
+static void cnt(char *);
 
 int
 main(int argc, char *argv[])
@@ -82,10 +81,10 @@ main(int argc, char *argv[])
break;
case '?':
default:
-   (void)fprintf(stderr,
+   fprintf(stderr,
"usage: %s [-c | -m] [-hlw] [file ...]\n",
__progname);
-   exit(1);
+   return 1;
}
argv += optind;
argc -= optind;
@@ -99,7 +98,7 @@ main(int argc, char *argv[])
doline = doword = dochar = 1;
 
if (!*argv) {
-   cnt((char *)NULL);
+   cnt(NULL);
} else {
int dototal = (argc > 1);
 
@@ -111,14 +110,14 @@ main(int argc, char *argv[])
print_counts(tlinect, twordct, tcharct, "total");
}
 
-   exit(rval);
+   return rval;
 }
 
-void
+static void
 cnt(char *file)
 {
static char *buf;
-   static ssize_t bufsz;
+   static size_t bufsz;
 
FILE *stream;
char *C;
@@ -213,7 +212,7 @@ cnt(char *file)
++charct;
len = mbtowc(, C, MB_CUR_MAX);
if (len == -1) {
-   (void)mbtowc(NULL, NULL,
+   mbtowc(NULL, NULL,
MB_CUR_MAX);
len = 1;
wc = L' ';
@@ -263,31 +262,31 @@ cnt(char *file)
}
 }
 
-void 
-format_and_print(long long v)
+static void
+format_and_print(int64_t v)
 {
if (humanchar) {
char result[FMT_SCALED_STRSIZE];
 
-   (void)fmt_scaled(v, result);
-   (void)printf("%7s", result);
+   fmt_scaled((long long)v, result);
+   printf("%7s", result);
} else {
-   (void)printf(" %7lld", v);
+   printf(" %7lld", v);
}
 }
 
-void
+static void
 print_counts(int64_t lines, int64_t words, int64_t chars, char *name)
 {
if (doline)
-   format_and_print((long long)lines);
+   format_and_print(lines);
if (doword)
-   format_and_print((long long)words);
+   format_and_print(words);
if (dochar)
-   format_and_print((long long)chars);
+   format_and_print(chars);
 
if (name)
-   (void)printf(" %s\n", name);
+   printf(" %s\n", name);
else
-   (void)printf("\n");
+   printf("\n");
 }



Some cleanups and tweaks for wc.1

2016-05-08 Thread Frederic Cambus
Hi tech@,

Some cleanups and tweaks for wc.1:

- Removed unnecessary string.h include
- Changed 'format_and_print' argument type to int64_t and casting
  inside the function
- Declaring 'print_counts', 'format_and_print', and 'cnt' as static
- Remove unnecessary cast for NULL, and (void) casts from printfs,
  'mbtowc' and 'format_and_print' calls
- In 'cnt', change bufsz type from ssize_t to size_t to avoid
  converting between pointers to integer types with different sign
  when calling getline (catched when compiling with Clang)
- Use return instead of exit in main

Index: wc.c
===
RCS file: /cvs/src/usr.bin/wc/wc.c,v
retrieving revision 1.20
diff -u -p -u -p -r1.20 wc.c
--- wc.c8 Dec 2015 01:00:45 -   1.20
+++ wc.c8 May 2016 21:51:28 -
@@ -34,7 +34,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -45,12 +44,12 @@
 
 int64_ttlinect, twordct, tcharct;
 intdoline, doword, dochar, humanchar, multibyte;
-intrval;
+intrval;
 extern char *__progname;
 
-void   print_counts(int64_t, int64_t, int64_t, char *);
-void   format_and_print(long long);
-void   cnt(char *);
+static void print_counts(int64_t, int64_t, int64_t, char *);
+static void format_and_print(int64_t);
+static void cnt(char *);
 
 int
 main(int argc, char *argv[])
@@ -82,10 +81,10 @@ main(int argc, char *argv[])
break;
case '?':
default:
-   (void)fprintf(stderr,
+   fprintf(stderr,
"usage: %s [-c | -m] [-hlw] [file ...]\n",
__progname);
-   exit(1);
+   return 1;
}
argv += optind;
argc -= optind;
@@ -99,7 +98,7 @@ main(int argc, char *argv[])
doline = doword = dochar = 1;
 
if (!*argv) {
-   cnt((char *)NULL);
+   cnt(NULL);
} else {
int dototal = (argc > 1);
 
@@ -111,14 +110,14 @@ main(int argc, char *argv[])
print_counts(tlinect, twordct, tcharct, "total");
}
 
-   exit(rval);
+   return rval;
 }
 
-void
+static void
 cnt(char *file)
 {
static char *buf;
-   static ssize_t bufsz;
+   static size_t bufsz;
 
FILE *stream;
char *C;
@@ -213,7 +212,7 @@ cnt(char *file)
++charct;
len = mbtowc(, C, MB_CUR_MAX);
if (len == -1) {
-   (void)mbtowc(NULL, NULL,
+   mbtowc(NULL, NULL,
MB_CUR_MAX);
len = 1;
wc = L' ';
@@ -263,31 +262,31 @@ cnt(char *file)
}
 }
 
-void 
-format_and_print(long long v)
+static void
+format_and_print(int64_t v)
 {
if (humanchar) {
char result[FMT_SCALED_STRSIZE];
 
-   (void)fmt_scaled(v, result);
-   (void)printf("%7s", result);
+   fmt_scaled((long long)v, result);
+   printf("%7s", result);
} else {
-   (void)printf(" %7lld", v);
+   printf(" %7lld", v);
}
 }
 
-void
+static void
 print_counts(int64_t lines, int64_t words, int64_t chars, char *name)
 {
if (doline)
-   format_and_print((long long)lines);
+   format_and_print(lines);
if (doword)
-   format_and_print((long long)words);
+   format_and_print(words);
if (dochar)
-   format_and_print((long long)chars);
+   format_and_print(chars);
 
if (name)
-   (void)printf(" %s\n", name);
+   printf(" %s\n", name);
else
-   (void)printf("\n");
+   printf("\n");
 }