1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-08-04 15:49:42 +02:00

(have_read_stdin, bsd_sum_file, sysv_sum_file,

main): Use bool for booleans.
(main): Don't dump core if invoked with argv[0]==NULL.
This commit is contained in:
Paul Eggert
2004-08-03 20:06:17 +00:00
parent f7f7ae46ea
commit 734248fa5b
+25 -29
View File
@@ -37,8 +37,8 @@
/* 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[] =
{
@@ -80,10 +80,10 @@ With no FILE, or when FILE is -, read standard input.\n\
/* 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;
@@ -95,7 +95,7 @@ bsd_sum_file (const char *file, int print_name)
if (STREQ (file, "-"))
{
fp = stdin;
have_read_stdin = 1;
have_read_stdin = true;
}
else
{
@@ -103,7 +103,7 @@ bsd_sum_file (const char *file, int print_name)
if (fp == NULL)
{
error (0, errno, "%s", file);
return -1;
return false;
}
}
/* Need binary I/O, or else byte counts and checksums are incorrect. */
@@ -122,13 +122,13 @@ bsd_sum_file (const char *file, int print_name)
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,
@@ -137,15 +137,15 @@ bsd_sum_file (const char *file, int print_name)
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;
@@ -161,7 +161,7 @@ sysv_sum_file (const char *file, int print_name)
if (STREQ (file, "-"))
{
fd = 0;
have_read_stdin = 1;
have_read_stdin = true;
}
else
{
@@ -169,7 +169,7 @@ sysv_sum_file (const char *file, int print_name)
if (fd == -1)
{
error (0, errno, "%s", file);
return -1;
return false;
}
}
/* Need binary I/O, or else byte counts and checksums are incorrect. */
@@ -188,7 +188,7 @@ sysv_sum_file (const char *file, int print_name)
error (0, errno, "%s", file);
if (!STREQ (file, "-"))
close (fd);
return -1;
return false;
}
for (i = 0; i < bytes_read; i++)
@@ -199,7 +199,7 @@ sysv_sum_file (const char *file, int print_name)
if (!STREQ (file, "-") && close (fd) == -1)
{
error (0, errno, "%s", file);
return -1;
return false;
}
r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
@@ -211,16 +211,16 @@ sysv_sum_file (const char *file, int print_name)
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];
@@ -230,7 +230,7 @@ main (int argc, char **argv)
atexit (close_stdout);
have_read_stdin = 0;
have_read_stdin = false;
while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
{
@@ -257,17 +257,13 @@ main (int argc, char **argv)
}
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);
}