/* The name this program was run with. */
char *program_name;
-/* Nonzero if any of the files read were the standard input. */
-static int have_read_stdin;
+/* True if any of the files read were the standard input. */
+static bool have_read_stdin;
static struct option const longopts[] =
{
/* Calculate and print the rotated checksum and the size in 1K blocks
of file FILE, or of the standard input if FILE is "-".
If PRINT_NAME is >1, print FILE next to the checksum and size.
- The checksum varies depending on sizeof(int).
- Return 0 if successful, -1 if an error occurs. */
+ The checksum varies depending on sizeof (int).
+ Return true if successful. */
-static int
+static bool
bsd_sum_file (const char *file, int print_name)
{
register FILE *fp;
if (STREQ (file, "-"))
{
fp = stdin;
- have_read_stdin = 1;
+ have_read_stdin = true;
}
else
{
if (fp == NULL)
{
error (0, errno, "%s", file);
- return -1;
+ return false;
}
}
/* Need binary I/O, or else byte counts and checksums are incorrect. */
error (0, errno, "%s", file);
if (!STREQ (file, "-"))
fclose (fp);
- return -1;
+ return false;
}
if (!STREQ (file, "-") && fclose (fp) == EOF)
{
error (0, errno, "%s", file);
- return -1;
+ return false;
}
printf ("%05d %5s", checksum,
printf (" %s", file);
putchar ('\n');
- return 0;
+ return true;
}
/* Calculate and print the checksum and the size in 512-byte blocks
of file FILE, or of the standard input if FILE is "-".
If PRINT_NAME is >0, print FILE next to the checksum and size.
- Return 0 if successful, -1 if an error occurs. */
+ Return true if successful. */
-static int
+static bool
sysv_sum_file (const char *file, int print_name)
{
int fd;
if (STREQ (file, "-"))
{
fd = 0;
- have_read_stdin = 1;
+ have_read_stdin = true;
}
else
{
if (fd == -1)
{
error (0, errno, "%s", file);
- return -1;
+ return false;
}
}
/* Need binary I/O, or else byte counts and checksums are incorrect. */
error (0, errno, "%s", file);
if (!STREQ (file, "-"))
close (fd);
- return -1;
+ return false;
}
for (i = 0; i < bytes_read; i++)
if (!STREQ (file, "-") && close (fd) == -1)
{
error (0, errno, "%s", file);
- return -1;
+ return false;
}
r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
printf (" %s", file);
putchar ('\n');
- return 0;
+ return true;
}
int
main (int argc, char **argv)
{
- int errors = 0;
+ bool ok;
int optc;
int files_given;
- int (*sum_func) (const char *, int) = bsd_sum_file;
+ bool (*sum_func) (const char *, int) = bsd_sum_file;
initialize_main (&argc, &argv);
program_name = argv[0];
atexit (close_stdout);
- have_read_stdin = 0;
+ have_read_stdin = false;
while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
{
}
files_given = argc - optind;
- if (files_given == 0)
- {
- if ((*sum_func) ("-", files_given) < 0)
- errors = 1;
- }
+ if (files_given <= 0)
+ ok = sum_func ("-", files_given);
else
- for (; optind < argc; optind++)
- if ((*sum_func) (argv[optind], files_given) < 0)
- errors = 1;
+ for (ok = true; optind < argc; optind++)
+ ok &= sum_func (argv[optind], files_given);
if (have_read_stdin && fclose (stdin) == EOF)
error (EXIT_FAILURE, errno, "-");
- exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+ exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}