sfdisk: remove fatal

Since fatal() exists with a standard 1 code, it can easily be replaced by errx(3).
The xalloc lib is also introduced in this patch.

[kzak@redhat,com: - fix coding style]

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Davidlohr Bueso 2011-12-20 14:42:00 +01:00 committed by Karel Zak
parent 5be926fac1
commit 109dbc4f84
1 changed files with 26 additions and 39 deletions

View File

@ -50,6 +50,7 @@
#include "c.h" #include "c.h"
#include "nls.h" #include "nls.h"
#include "xalloc.h"
#include "blkdev.h" #include "blkdev.h"
#include "linux_version.h" #include "linux_version.h"
#include "common.h" #include "common.h"
@ -125,19 +126,6 @@ error(char *s, ...) {
va_end(p); va_end(p);
} }
static void
fatal(char *s, ...) {
va_list p;
va_start(p, s);
fflush(stdout);
fprintf(stderr, "\n" PROGNAME ": ");
vfprintf(stderr, s, p);
fflush(stderr);
va_end(p);
exit(1);
}
/* /*
* arm needs PACKED - use it everywhere? * arm needs PACKED - use it everywhere?
*/ */
@ -215,8 +203,7 @@ get_sector(char *dev, int fd, unsigned long long sno) {
if (!sseek(dev, fd, sno)) if (!sseek(dev, fd, sno))
return 0; return 0;
if (!(s = (struct sector *)malloc(sizeof(struct sector)))) s = xmalloc(sizeof(struct sector));
fatal(_("out of memory - giving up\n"));
if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) { if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
if (errno) /* 0 in case we read past end-of-disk */ if (errno) /* 0 in case we read past end-of-disk */
@ -871,7 +858,7 @@ asc_to_index(char *pnam, struct disk_desc *z) {
pno = linux_to_index(pnum, z); pno = linux_to_index(pnum, z);
} }
if (!(pno >= 0 && pno < z->partno)) if (!(pno >= 0 && pno < z->partno))
fatal(_("%s: no such partition\n"), pnam); errx(EXIT_FAILURE, _("%s: no such partition\n"), pnam);
return pno; return pno;
} }
@ -1206,9 +1193,9 @@ partitions_ok(int fd, struct disk_desc *z) {
/* Have at least 4 partitions been defined? */ /* Have at least 4 partitions been defined? */
if (partno < 4) { if (partno < 4) {
if (!partno) if (!partno)
fatal(_("no partition table present.\n")); errx(EXIT_FAILURE, _("no partition table present."));
else else
fatal(_("strange, only %d partitions defined.\n"), partno); errx(EXIT_FAILURE, _("strange, only %d partitions defined."), partno);
return 0; return 0;
} }
@ -1705,7 +1692,7 @@ write_partitions(char *dev, int fd, struct disk_desc *z) {
} }
if (save_sector_file) { if (save_sector_file) {
if (!save_sectors(dev, fd)) { if (!save_sectors(dev, fd)) {
fatal(_("Failed saving the old sectors - aborting\n")); errx(EXIT_FAILURE, _("Failed saving the old sectors - aborting\n"));
return 0; return 0;
} }
} }
@ -1793,7 +1780,7 @@ read_stdin(char **fields, char *line, int fieldssize, int linesize) {
return RD_EOF; return RD_EOF;
} }
if (!(lp = strchr(lp, '\n'))) if (!(lp = strchr(lp, '\n')))
fatal(_("long or incomplete input line - quitting\n")); errx(EXIT_FAILURE, _("long or incomplete input line - quitting"));
*lp = 0; *lp = 0;
/* remove comments, if any */ /* remove comments, if any */
@ -1829,20 +1816,20 @@ read_stdin(char **fields, char *line, int fieldssize, int linesize) {
while (isalnum(*ip)) /* 0x07FF */ while (isalnum(*ip)) /* 0x07FF */
ip++; ip++;
} else } else
fatal(_("input error: `=' expected after %s field\n"), errx(EXIT_FAILURE, _("input error: `=' expected after %s field"),
d->fldname); d->fldname);
if (fno <= d->fldno) if (fno <= d->fldno)
fno = d->fldno + 1; fno = d->fldno + 1;
if (*ip == 0) if (*ip == 0)
return fno; return fno;
if (*ip != ',' && *ip != ';') if (*ip != ',' && *ip != ';')
fatal(_("input error: unexpected character %c after %s field\n"), errx(EXIT_FAILURE, _("input error: unexpected character %c after %s field"),
*ip, d->fldname); *ip, d->fldname);
*ip = 0; *ip = 0;
goto nxtfld; goto nxtfld;
} }
} }
fatal(_("unrecognized input: %s\n"), ip); errx(EXIT_FAILURE, _("unrecognized input: %s"), ip);
} }
/* split line into fields */ /* split line into fields */
@ -2327,7 +2314,7 @@ read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
while (!(i = read_line(pno, ep, dev, interactive, z))) while (!(i = read_line(pno, ep, dev, interactive, z)))
if (!interactive) if (!interactive)
fatal(_("bad input\n")); errx(EXIT_FAILURE, _("bad input"));
if (i < 0) { if (i < 0) {
p->ep = ep; p->ep = ep;
return 0; return 0;
@ -2618,7 +2605,7 @@ main(int argc, char **argv) {
textdomain(PACKAGE); textdomain(PACKAGE);
if (argc < 1) if (argc < 1)
fatal(_("no command?\n")); errx(EXIT_FAILURE, _("no command?"));
if ((progn = strrchr(argv[0], '/')) == NULL) if ((progn = strrchr(argv[0], '/')) == NULL)
progn = argv[0]; progn = argv[0];
else else
@ -2831,18 +2818,18 @@ main(int argc, char **argv) {
} }
if (do_id) { if (do_id) {
if ((do_id & PRINT_ID) != 0 && optind != argc - 2) if ((do_id & PRINT_ID) != 0 && optind != argc - 2)
fatal(_("usage: sfdisk --print-id device partition-number\n")); errx(EXIT_FAILURE, _("usage: sfdisk --print-id device partition-number"));
else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3) else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3)
fatal(_("usage: sfdisk --change-id device partition-number Id\n")); errx(EXIT_FAILURE, _("usage: sfdisk --change-id device partition-number Id"));
else if (optind != argc - 3 && optind != argc - 2) else if (optind != argc - 3 && optind != argc - 2)
fatal(_("usage: sfdisk --id device partition-number [Id]\n")); errx(EXIT_FAILURE, _("usage: sfdisk --id device partition-number [Id]"));
do_change_id(argv[optind], argv[optind + 1], do_change_id(argv[optind], argv[optind + 1],
(optind == argc - 2) ? 0 : argv[optind + 2]); (optind == argc - 2) ? 0 : argv[optind + 2]);
exit(exit_status); exit(exit_status);
} }
if (optind != argc - 1) if (optind != argc - 1)
fatal(_("can specify only one device (except with -l or -s)\n")); errx(EXIT_FAILURE, _("can specify only one device (except with -l or -s)"));
dev = argv[optind]; dev = argv[optind];
if (opt_reread) if (opt_reread)
@ -2868,9 +2855,9 @@ my_open(char *dev, int rw, int silent) {
if (fd < 0 && !silent) { if (fd < 0 && !silent) {
perror(dev); perror(dev);
if (rw) if (rw)
fatal(_("cannot open %s read-write\n"), dev); errx(EXIT_FAILURE, _("cannot open %s read-write"), dev);
else else
fatal(_("cannot open %s for reading\n"), dev); errx(EXIT_FAILURE, _("cannot open %s for reading"), dev);
} }
return fd; return fd;
} }
@ -2965,7 +2952,7 @@ do_size(char *dev, int silent) {
if (blkdev_get_sectors(fd, &size) == -1) { if (blkdev_get_sectors(fd, &size) == -1) {
if (!silent) { if (!silent) {
perror(dev); perror(dev);
fatal(_("Cannot get size of %s\n"), dev); errx(EXIT_FAILURE, _("Cannot get size of %s"), dev);
} }
return; return;
} }
@ -3090,7 +3077,7 @@ set_unhidden(struct disk_desc *z, char *pnam) {
if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17) if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
id -= 0x10; id -= 0x10;
else else
fatal(_("partition %s has id %x and is not hidden\n"), pnam, id); errx(EXIT_FAILURE, _("partition %s has id %x and is not hidden"), pnam, id);
z->partitions[pno].p.sys_type = id; z->partitions[pno].p.sys_type = id;
} }
@ -3150,7 +3137,7 @@ do_change_id(char *dev, char *pnam, char *id) {
} }
i = strtoul(id, NULL, 16); i = strtoul(id, NULL, 16);
if (i > 255) if (i > 255)
fatal(_("Bad Id %lx\n"), i); errx(EXIT_FAILURE, _("Bad Id %lx"), i);
z->partitions[pno].p.sys_type = i; z->partitions[pno].p.sys_type = i;
if (write_partitions(dev, fd, z)) if (write_partitions(dev, fd, z))
@ -3188,7 +3175,7 @@ do_fdisk(char *dev) {
if (stat(dev, &statbuf) < 0) { if (stat(dev, &statbuf) < 0) {
perror(dev); perror(dev);
fatal(_("Fatal error: cannot find %s\n"), dev); errx(EXIT_FAILURE, _("Fatal error: cannot find %s"), dev);
} }
if (!S_ISBLK(statbuf.st_mode)) { if (!S_ISBLK(statbuf.st_mode)) {
do_warn(_("Warning: %s is not a block device\n"), dev); do_warn(_("Warning: %s is not a block device\n"), dev);
@ -3220,7 +3207,7 @@ do_fdisk(char *dev) {
out_partitions(dev, z); out_partitions(dev, z);
if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0) if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
fatal(_("Partition %d does not exist, cannot change it\n"), one_only); errx(EXIT_FAILURE, _("Partition %d does not exist, cannot change it"), one_only);
z = &newp; z = &newp;
@ -3233,8 +3220,8 @@ do_fdisk(char *dev) {
if (!partitions_ok(fd, z) && !force) { if (!partitions_ok(fd, z) && !force) {
if (!interactive) if (!interactive)
fatal(_("I don't like these partitions - nothing changed.\n" errx(EXIT_FAILURE, _("I don't like these partitions - nothing changed.\n"
"(If you really want this, use the --force option.)\n")); "(If you really want this, use the --force option.)"));
else else
do_warn(_("I don't like this - probably you should answer No\n")); do_warn(_("I don't like this - probably you should answer No\n"));
} }
@ -3250,7 +3237,7 @@ do_fdisk(char *dev) {
if (c == EOF) if (c == EOF)
printf(_("\nsfdisk: premature end of input\n")); printf(_("\nsfdisk: premature end of input\n"));
if (c == EOF || answer == 'q' || answer == 'Q') { if (c == EOF || answer == 'q' || answer == 'Q') {
fatal(_("Quitting - nothing changed\n")); errx(EXIT_FAILURE, _("Quitting - nothing changed"));
} else if (answer == 'n' || answer == 'N') { } else if (answer == 'n' || answer == 'N') {
continue; continue;
} else if (answer == 'y' || answer == 'Y') { } else if (answer == 'y' || answer == 'Y') {