Use 'error' function for fprintf followed by exit.

Like with the previous commit, this replaces instances of:

  if (something_bad) {
    fprintf (stderr, "%s: error message\n", guestfs_int_program_name);
    exit (EXIT_FAILURE);
  }

with:

  if (something_bad)
    error (EXIT_FAILURE, 0, "error message");

(except in a few cases were errno was incorrectly being ignored, in
which case I have fixed that).

It's slightly more complex than the previous commit because we must be
careful to:

 - Remove the program name (since error(3) prints it).

 - Remove any trailing \n character from the message.

Candidates for replacement were found using:

  pcregrep --buffer-size 10M -M '\bfprintf\b.*\n.*\bexit\b' `git ls-files`
This commit is contained in:
Richard W.M. Jones
2016-04-04 11:31:00 +01:00
parent 129e4938ba
commit fdfedcb4ef
51 changed files with 574 additions and 1004 deletions

View File

@@ -190,16 +190,12 @@ main (int argc, char *argv[])
struct tree *tree1, *tree2;
g = guestfs_create ();
if (g == NULL) {
fprintf (stderr, _("guestfs_create: failed to create handle\n"));
exit (EXIT_FAILURE);
}
if (g == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
g2 = guestfs_create ();
if (g2 == NULL) {
fprintf (stderr, _("guestfs_create: failed to create handle\n"));
exit (EXIT_FAILURE);
}
if (g2 == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
for (;;) {
c = getopt_long (argc, argv, options, long_options, &option_index);
@@ -260,12 +256,10 @@ main (int argc, char *argv[])
} else if (STREQ (long_options[option_index].name, "xattr") ||
STREQ (long_options[option_index].name, "xattrs")) {
enable_xattrs = 1;
} else {
fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
guestfs_int_program_name,
long_options[option_index].name, option_index);
exit (EXIT_FAILURE);
}
} else
error (EXIT_FAILURE, 0,
_("unknown long option: %s (%d)"),
long_options[option_index].name, option_index);
break;
case 'a':
@@ -327,17 +321,11 @@ main (int argc, char *argv[])
/* CSV && human is unsafe because spreadsheets fail to parse these
* fields correctly. (RHBZ#600977).
*/
if (human && csv) {
fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"),
guestfs_int_program_name);
exit (EXIT_FAILURE);
}
if (human && csv)
error (EXIT_FAILURE, 0, _("you cannot use -h and --csv options together."));
if (optind != argc) {
fprintf (stderr, _("%s: extra arguments on the command line\n"),
guestfs_int_program_name);
usage (EXIT_FAILURE);
}
if (optind != argc)
error (EXIT_FAILURE, 0, _("extra arguments on the command line"));
/* These are really constants, but they have to be variables for the
* options parsing code. Assert here that they have known-good