diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-01-20 10:55:18 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-01-20 10:55:18 +0000 |
commit | 70e9163c9c18e995515598085cb824e554eb7ae7 (patch) | |
tree | a42dc8b2a6c031354bf31472de888bfc8a060132 /src/cksum.c | |
parent | cbf5993c43f49281173f185863577d86bfac6eae (diff) | |
download | coreutils-tarball-master.tar.gz |
coreutils-8.25HEADcoreutils-8.25master
Diffstat (limited to 'src/cksum.c')
-rw-r--r-- | src/cksum.c | 82 |
1 files changed, 42 insertions, 40 deletions
diff --git a/src/cksum.c b/src/cksum.c index d93877f..0c47243 100644 --- a/src/cksum.c +++ b/src/cksum.c @@ -1,10 +1,10 @@ /* cksum -- calculate and print POSIX checksums and sizes of files - Copyright (C) 92, 1995-2006 Free Software Foundation, Inc. + Copyright (C) 1992-2016 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + 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 2, or (at your option) - any later version. + 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 @@ -12,9 +12,8 @@ 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, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + /* Written by Q. Frank Xia, qx@math.columbia.edu. Cosmetic changes and reorganization by David MacKenzie, djm@gnu.ai.mit.edu. @@ -29,21 +28,23 @@ crctab > crctab.h This software is compatible with neither the System V nor the BSD - `sum' program. It is supposed to conform to POSIX, except perhaps + 'sum' program. It is supposed to conform to POSIX, except perhaps for foreign language support. Any inconsistency with the standard (other than foreign language support) is a bug. */ #include <config.h> -/* The official name of this program (e.g., no `g' prefix). */ +/* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "cksum" -#define AUTHORS "Q. Frank Xia" +#define AUTHORS proper_name ("Q. Frank Xia") #include <stdio.h> #include <sys/types.h> #include <stdint.h> #include "system.h" +#include "fadvise.h" +#include "xfreopen.h" #ifdef CRCTAB @@ -58,8 +59,8 @@ The i bit in GEN is set if X^i is a summand of G(X) except X^32. */ # define GEN (BIT (26) | BIT (23) | BIT (22) | BIT (16) | BIT (12) \ - | BIT (11) | BIT (10) | BIT (8) | BIT (7) | BIT (5) \ - | BIT (4) | BIT (2) | BIT (1) | BIT (0)) + | BIT (11) | BIT (10) | BIT (8) | BIT (7) | BIT (5) \ + | BIT (4) | BIT (2) | BIT (1) | BIT (0)) static uint_fast32_t r[8]; @@ -96,12 +97,12 @@ main (void) for (i = 0; i < 51; i++) { printf (",\n 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x", - crc_remainder (i * 5 + 1), crc_remainder (i * 5 + 2), - crc_remainder (i * 5 + 3), crc_remainder (i * 5 + 4), - crc_remainder (i * 5 + 5)); + crc_remainder (i * 5 + 1), crc_remainder (i * 5 + 2), + crc_remainder (i * 5 + 3), crc_remainder (i * 5 + 4), + crc_remainder (i * 5 + 5)); } printf ("\n};\n"); - exit (EXIT_SUCCESS); + return EXIT_SUCCESS; } #else /* !CRCTAB */ @@ -109,14 +110,10 @@ main (void) # include <getopt.h> # include "long-options.h" # include "error.h" -# include "inttostr.h" /* Number of bytes to read at once. */ # define BUFLEN (1 << 16) -/* The name this program was run with. */ -char *program_name; - static uint_fast32_t const crctab[256] = { 0x00000000, @@ -197,42 +194,44 @@ cksum (const char *file, bool print_name) fp = stdin; have_read_stdin = true; if (O_BINARY && ! isatty (STDIN_FILENO)) - freopen (NULL, "rb", stdin); + xfreopen (NULL, "rb", stdin); } else { fp = fopen (file, (O_BINARY ? "rb" : "r")); if (fp == NULL) - { - error (0, errno, "%s", file); - return false; - } + { + error (0, errno, "%s", quotef (file)); + return false; + } } + fadvise (fp, FADVISE_SEQUENTIAL); + while ((bytes_read = fread (buf, 1, BUFLEN, fp)) > 0) { unsigned char *cp = buf; if (length + bytes_read < length) - error (EXIT_FAILURE, 0, _("%s: file too long"), file); + error (EXIT_FAILURE, 0, _("%s: file too long"), quotef (file)); length += bytes_read; while (bytes_read--) - crc = (crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF]; + crc = (crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF]; if (feof (fp)) - break; + break; } if (ferror (fp)) { - error (0, errno, "%s", file); + error (0, errno, "%s", quotef (file)); if (!STREQ (file, "-")) - fclose (fp); + fclose (fp); return false; } if (!STREQ (file, "-") && fclose (fp) == EOF) { - error (0, errno, "%s", file); + error (0, errno, "%s", quotef (file)); return false; } @@ -258,22 +257,21 @@ void usage (int status) { if (status != EXIT_SUCCESS) - fprintf (stderr, _("Try `%s --help' for more information.\n"), - program_name); + emit_try_help (); else { printf (_("\ Usage: %s [FILE]...\n\ or: %s [OPTION]\n\ "), - program_name, program_name); + program_name, program_name); fputs (_("\ Print CRC checksum and byte counts of each FILE.\n\ \n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); - printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); + emit_ancillary_info (PROGRAM_NAME); } exit (status); } @@ -285,15 +283,19 @@ main (int argc, char **argv) bool ok; initialize_main (&argc, &argv); - program_name = argv[0]; + set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION, - usage, AUTHORS, (char const *) NULL); + /* Line buffer stdout to ensure lines are written atomically and immediately + so that processes running in parallel do not intersperse their output. */ + setvbuf (stdout, NULL, _IOLBF, 0); + + parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version, + usage, AUTHORS, (char const *) NULL); if (getopt_long (argc, argv, "", NULL, NULL) != -1) usage (EXIT_FAILURE); @@ -305,12 +307,12 @@ main (int argc, char **argv) { ok = true; for (i = optind; i < argc; i++) - ok &= cksum (argv[i], true); + ok &= cksum (argv[i], true); } if (have_read_stdin && fclose (stdin) == EOF) error (EXIT_FAILURE, errno, "-"); - exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); + return ok ? EXIT_SUCCESS : EXIT_FAILURE; } #endif /* !CRCTAB */ |