summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-05-10 10:14:08 -0400
committerJakob Kaivo <jkk@ung.org>2019-05-10 10:14:08 -0400
commit799b196ca8ce28d3e2b51b8e95cf7fde0f4817d3 (patch)
treeb3820a162bd4d1a29d94b0025a23829dcb10b86e
parent8050d258f3105e50920392653dffc84e7c612092 (diff)
formatting
-rw-r--r--wc.c215
1 files changed, 114 insertions, 101 deletions
diff --git a/wc.c b/wc.c
index ae1ae03..5040ec6 100644
--- a/wc.c
+++ b/wc.c
@@ -1,7 +1,7 @@
/*
* UNG's Not GNU
*
- * Copyright (c) 2011, Jakob Kaivo <jakob@kaivo.net>
+ * Copyright (c) 2011-2019, Jakob Kaivo <jkk@ung.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -17,123 +17,136 @@
*
*/
+#define _XOPEN_SOURCE 700
+#include <inttypes.h>
#include <stdio.h>
#include <ctype.h>
-#include <getopt.h>
-
-const char *wc_desc = "word, line, and byte or character count";
-const char *wc_inv = "wc [-c|-m] [-lw] [file...]";
+#include <unistd.h>
#define BYTES 1 << 0
#define CHARS 1 << 1
#define LINES 1 << 2
#define WORDS 1 << 3
-static unsigned long total_n = 0;
-static unsigned long total_w = 0;
-static unsigned long total_c = 0;
+static uintmax_t total_n = 0;
+static uintmax_t total_w = 0;
+static uintmax_t total_c = 0;
-int flagprint (unsigned long n, unsigned long w, unsigned long c, char *f,
- int flags)
+void flagprint (uintmax_t n, uintmax_t w, uintmax_t c, char *f, int flags)
{
- if (flags == 0) flags = LINES | WORDS | CHARS;
-
- if (flags & LINES)
- printf ("%lu%s", n, flags ^ LINES ? " " : "");
- if (flags & WORDS)
- printf ("%lu%s", w, flags ^ (LINES | WORDS) ? " " : "");
- if (flags & CHARS || flags & BYTES)
- printf ("%lu", c);
- // FIXME: What an ass-pain
- if (f != NULL)
- printf ("%s%s", flags & CHARS || flags & BYTES ? " " : "", f);
- printf ("\n");
+ if (flags == 0) {
+ flags = LINES | WORDS | CHARS;
+ }
+
+ if (flags & LINES) {
+ printf("%ju%s", n, flags ^ LINES ? " " : "");
+ }
+
+ if (flags & WORDS) {
+ printf ("%ju%s", w, flags ^ (LINES | WORDS) ? " " : "");
+ }
+
+ if (flags & CHARS || flags & BYTES) {
+ printf ("%lu", c);
+ }
+
+ // FIXME: What an ass-pain
+ if (f != NULL) {
+ printf ("%s%s", flags & CHARS || flags & BYTES ? " " : "", f);
+ }
+
+ printf ("\n");
}
-int wc (FILE *f, int flags, char *name)
+int wc(FILE *f, int flags, char *name)
{
- unsigned long newlines = 0;
- unsigned long words = 0;
- unsigned long charbytes = 0;
- int wasword = 0;
- int n, i;
- char buf[BUFSIZ];
-
- while (!feof(f)) {
- n = fread (buf, sizeof(char), BUFSIZ, f);
-
- if (flags & CHARS) {
- charbytes += n; // FIXME: wrong, convert to wc_type or mb_char
- } else {
- charbytes += n;
- }
-
- for (i = 0; i < n; i++) {
- if (buf[i] == '\n') {
- newlines++;
- if (wasword == 0) {
- wasword = 1;
- words++;
- }
- } else if (wasword == 0 && isspace(buf[i])) {
- wasword = 1;
- words++;
- } else {
- wasword = 0;
- }
- }
- }
-
- total_n += newlines;
- total_w += words;
- total_c += charbytes;
-
- flagprint (newlines, words, charbytes, name, flags);
+ uintmax_t newlines = 0;
+ uintmax_t words = 0;
+ uintmax_t charbytes = 0;
+ int wasword = 0;
+ int n, i;
+ char buf[BUFSIZ];
+
+ while (!feof(f)) {
+ n = fread (buf, sizeof(char), BUFSIZ, f);
+
+ if (flags & CHARS) {
+ charbytes += n; // FIXME: wrong, convert to wc_type or mb_char
+ } else {
+ charbytes += n;
+ }
+
+ for (i = 0; i < n; i++) {
+ if (buf[i] == '\n') {
+ newlines++;
+ if (wasword == 0) {
+ wasword = 1;
+ words++;
+ }
+ } else if (wasword == 0 && isspace(buf[i])) {
+ wasword = 1;
+ words++;
+ } else {
+ wasword = 0;
+ }
+ }
+ }
+
+ total_n += newlines;
+ total_w += words;
+ total_c += charbytes;
+
+ flagprint (newlines, words, charbytes, name, flags);
+ return 0;
}
-int
-main(int argc, char **argv)
+int main(int argc, char *argv[])
{
- int flags = 0;
- int total = 0;
+ int flags = 0;
+ int total = 0;
- int c;
- while ((c = getopt (argc, argv, ":cmlw")) != -1) {
- switch (c) {
- case 'c':
- if (flags & CHARS) { return 1; }
- flags |= BYTES;
- break;
- case 'm':
- if (flags & BYTES) { return 1; }
- flags |= CHARS;
- break;
- case 'l':
- flags |= LINES;
- break;
- case 'w':
- flags |= WORDS;
- break;
- default:
- return 1;
- }
- }
-
- if (argc - optind > 1)
- total = 1;
-
- if (optind >= argc) {
- wc (stdin, flags, NULL);
- } else while (optind < argc) {
- FILE *in = fopen (argv[optind], "r");
- wc (in, flags, argv[optind]);
- fclose (in);
- optind++;
- }
+ int c;
+ while ((c = getopt(argc, argv, ":cmlw")) != -1) {
+ switch (c) {
+ case 'c':
+ if (flags & CHARS) { return 1; }
+ flags |= BYTES;
+ break;
+
+ case 'm':
+ if (flags & BYTES) { return 1; }
+ flags |= CHARS;
+ break;
+
+ case 'l':
+ flags |= LINES;
+ break;
+
+ case 'w':
+ flags |= WORDS;
+ break;
+
+ default:
+ return 1;
+ }
+ }
+
+ if (argc - optind > 1) {
+ total = 1;
+ }
+
+ if (optind >= argc) {
+ wc(stdin, flags, NULL);
+ } else while (optind < argc) {
+ FILE *in = fopen(argv[optind], "r");
+ wc(in, flags, argv[optind]);
+ fclose(in);
+ optind++;
+ }
- if (total) {
- flagprint (total_n, total_w, total_c, "total", flags);
- }
+ if (total) {
+ flagprint(total_n, total_w, total_c, "total", flags);
+ }
- return 0;
+ return 0;
}