From 581da61ff674f9d3199e14cb71996631d92d6405 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 9 Dec 2007 18:44:03 +0200 Subject: Added isascii. --- Makefile | 3 ++ isascii.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 Makefile create mode 100644 isascii.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a3d753f --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +progs = isascii + +all: $(progs) 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 + * + * 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 . + */ + + +#define _BSD_SOURCE /* For isascii(3). */ + +#include +#include +#include +#include +#include +#include + +#include + + +#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); +} -- cgit v1.2.1