summaryrefslogtreecommitdiff
path: root/isascii.c
diff options
context:
space:
mode:
authorLars Wirzenius <liw@iki.fi>2007-12-09 18:44:03 +0200
committerLars Wirzenius <liw@iki.fi>2007-12-09 18:44:03 +0200
commit581da61ff674f9d3199e14cb71996631d92d6405 (patch)
treea647f236a7a2d6356e28ae5a1a1bd4c08f1f79d8 /isascii.c
downloadextrautils-581da61ff674f9d3199e14cb71996631d92d6405.tar.gz
Added isascii.
Diffstat (limited to 'isascii.c')
-rw-r--r--isascii.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/isascii.c b/isascii.c
new file mode 100644
index 0000000..6294c01
--- /dev/null
+++ b/isascii.c
@@ -0,0 +1,141 @@
+/*
+ * isascii - verify that input files consist of ASCII characters only
+ * Copyright (C) 2007 Lars Wirzenius <liw@iki.fi>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#define _BSD_SOURCE /* For isascii(3). */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <getopt.h>
+
+#include <ctype.h>
+
+
+#define VERSION "1.0"
+
+
+/*
+ * Code to indicate an invalid UTF8 character.
+ */
+enum { INVALID_CHAR = 0xffffffff };
+
+
+/*
+ * Determine if the contents of an open file form a valid ASCII byte stream.
+ */
+static int is_ascii_byte_stream(FILE *file, char *filename, int quiet) {
+ int c;
+ unsigned long line, col;
+
+ line = 1;
+ col = 1;
+
+ for (;;) {
+ c = getc(file);
+
+ if (c == EOF)
+ break;
+ else if (c == '\n') {
+ ++line;
+ col = 1;
+ } else if (isascii(c)) {
+ ++col;
+ } else
+ goto error;
+ }
+
+ return 1;
+
+error:
+ if (!quiet) {
+ printf("%s: line %lu, char %lu: invalid ASCII code: 0x%02x\n",
+ filename, line, col, c);
+ }
+ return 0;
+}
+
+
+static void usage(const char *program_name) {
+ printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n",
+ program_name);
+ printf("Check whether input files are valid ASCII.\n");
+ printf("This is version %s.\n", VERSION);
+}
+
+
+int main(int argc, char **argv) {
+ int i, ok;
+ FILE *file;
+
+ int quiet;
+ struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "quiet", no_argument, &quiet, 1 },
+ };
+ int opt;
+
+ quiet = 0;
+
+ while ((opt = getopt_long(argc, argv, "hq", options, NULL)) != -1) {
+ switch (opt) {
+ case 0:
+ break;
+
+ case 'h':
+ usage(argv[0]);
+ exit(0);
+ break;
+
+ case 'q':
+ quiet = 1;
+ break;
+
+ case '?':
+ exit(EXIT_FAILURE);
+
+ default:
+ abort();
+ }
+ }
+
+ if (optind == argc)
+ ok = is_ascii_byte_stream(stdin, "stdin", quiet);
+ else {
+ ok = 1;
+ for (i = optind; i < argc; ++i) {
+ file = fopen(argv[i], "r");
+ if (file == NULL) {
+ fprintf(stderr, "isascii: %s: error %d: %s\n",
+ argv[i], errno,
+ strerror(errno));
+ ok = 0;
+ } else {
+ if (!is_ascii_byte_stream(file, argv[i], quiet))
+ ok = 0;
+ (void) fclose(file);
+ }
+ }
+ }
+
+ if (ok)
+ exit(0);
+ exit(EXIT_FAILURE);
+}