#include <linux/hdreg.h> /* HDIO_GETGEO */
#include <linux/fs.h> /* BLKGETSIZE */
+#define BB_DECLARE_EXTERN
+#define bb_need_memory_exhausted
+#include "messages.c"
static const char sfdisk_usage[] =
"sfdisk [options] device ...\n"
- "device: something like /dev/hda or /dev/sda\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+ "\ndevice: something like /dev/hda or /dev/sda\n"
"useful options:\n"
" -s [or --show-size]: list size of a partition\n"
" -c [or --id]: print or change partition Id\n"
" -S# [or --sectors #]: set the number of sectors to use\n"
"You can disable all consistency checking with:\n"
- " -f [or --force]: do what I say, even if it is stupid\n";
+ " -f [or --force]: do what I say, even if it is stupid\n"
+#endif
+ ;
static char *save_sector_file = NULL;
static char *restore_sector_file = NULL;
+static void warn(char *s, ...) __attribute__ ((format (printf, 1, 2)));
static void warn(char *s, ...)
{
va_list p;
va_end(p);
}
-static void error(char *s, ...)
-{
- va_list p;
-
- va_start(p, s);
- fflush(stdout);
- fprintf(stderr, "\n" PROGNAME ": ");
- vfprintf(stderr, s, 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);
- va_end(p);
- exit(1);
-}
-
/*
* A. About seeking
*/
if ((out = lseek(fd, in, SEEK_SET)) != in) {
#endif
perror("llseek");
- error("seek error on %s - cannot seek to %lu\n", dev, s);
+ errorMsg("seek error on %s - cannot seek to %lu\n", dev, s);
return 0;
}
if (in != out) {
- error("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n",
+ errorMsg("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n",
(uint) (in >> 32), (uint) (in & 0xffffffff),
(uint) (out >> 32), (uint) (out & 0xffffffff));
return 0;
if (!sseek(dev, fd, sno))
return 0;
- if (!(s = (struct sector *) malloc(sizeof(struct sector))))
- fatal("out of memory - giving up\n");
+ s = (struct sector *) xmalloc(sizeof(struct sector));
if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
perror("read");
- error("read error on %s - cannot read sector %lu\n", dev, sno);
+ errorMsg("read error on %s - cannot read sector %lu\n", dev, sno);
free(s);
return 0;
}
static int msdos_signature(struct sector *s)
{
if (*(unsigned short *) (s->data + 0x1fe) != 0xaa55) {
- error("ERROR: sector %lu does not have an msdos signature\n",
+ errorMsg("ERROR: sector %lu does not have an msdos signature\n",
s->sectornumber);
return 0;
}
return 0;
if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
perror("write");
- error("write error on %s - cannot write sector %lu\n",
+ errorMsg("write error on %s - cannot write sector %lu\n",
dev, s->sectornumber);
return 0;
}
fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
if (fdout < 0) {
perror(save_sector_file);
- error("cannot open partition sector save file (%s)\n",
+ errorMsg("cannot open partition sector save file (%s)\n",
save_sector_file);
return 0;
}
return 0;
if (read(fdin, ss + 4, 512) != 512) {
perror("read");
- error("read error on %s - cannot read sector %lu\n",
+ errorMsg("read error on %s - cannot read sector %lu\n",
dev, s->sectornumber);
return 0;
}
if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
perror("write");
- error("write error on %s\n"), save_sector_file;
+ errorMsg("write error on %s\n", save_sector_file);
return 0;
}
}
if (stat(restore_sector_file, &statbuf) < 0) {
perror(restore_sector_file);
- error("cannot stat partition restore file (%s)\n",
+ errorMsg("cannot stat partition restore file (%s)\n",
restore_sector_file);
return 0;
}
if (statbuf.st_size % 516) {
- error("partition restore file has wrong size - not restoring\n");
+ errorMsg("partition restore file has wrong size - not restoring\n");
return 0;
}
if (!(ss = (char *) malloc(statbuf.st_size))) {
- error("out of memory?\n");
+ errorMsg(memory_exhausted, "sfdisk");
return 0;
}
fdin = open(restore_sector_file, O_RDONLY);
if (fdin < 0) {
perror(restore_sector_file);
- error("cannot open partition restore file (%s)\n",
+ errorMsg("cannot open partition restore file (%s)\n",
restore_sector_file);
return 0;
}
if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
perror("read");
- error("error reading %s\n"), restore_sector_file;
+ errorMsg("error reading %s\n", restore_sector_file);
return 0;
}
fdout = open(dev, O_WRONLY);
if (fdout < 0) {
perror(dev);
- error("cannot open device %s for writing\n"), dev;
+ errorMsg("cannot open device %s for writing\n", dev);
return 0;
}
return 0;
if (write(fdout, ss + 4, 512) != 512) {
perror(dev);
- error("error writing sector %lu on %s\n", sno, dev);
+ errorMsg("error writing sector %lu on %s\n", sno, dev);
return 0;
}
ss += 516;
if (ioctl_ok) {
if (g.start && !force) {
warn
- ("Warning: start=%d - this looks like a partition rather than\n"
+ ("Warning: start=%ld - this looks like a partition rather than\n"
"the entire disk. Using fdisk on it is probably meaningless.\n"
"[Use the --force option if you really want this]\n",
g.start);
printf("Disk %s: cannot get geometry\n", dev);
if (B.sectors > 63)
warn
- ("Warning: unlikely number of sectors (%d - usually at most 63\n"
+ ("Warning: unlikely number of sectors (%ld - usually at most 63\n"
"This will give problems with all software that uses C/H/S addressing.\n",
B.sectors);
if (!silent)
return 1;
if (B.heads && aa.h >= B.heads) {
warn("%s of partition %s has impossible value for head: "
- "%d (should be in 0-%d)\n", w, v, aa.h, B.heads - 1);
+ "%ld (should be in 0-%ld)\n", w, v, aa.h, B.heads - 1);
ret = 0;
}
if (B.sectors && (aa.s == 0 || aa.s > B.sectors)) {
warn("%s of partition %s has impossible value for sector: "
- "%d (should be in 1-%d)\n", w, v, aa.s, B.sectors);
+ "%ld (should be in 1-%ld)\n", w, v, aa.s, B.sectors);
ret = 0;
}
if (B.cylinders && aa.c >= B.cylinders) {
warn("%s of partition %s has impossible value for cylinders: "
- "%d (should be in 0-%d)\n", w, v, aa.c, B.cylinders - 1);
+ "%ld (should be in 0-%ld)\n", w, v, aa.c, B.cylinders - 1);
ret = 0;
}
return ret;
pno = linux_to_index(pnum, z);
}
if (!(pno >= 0 && pno < z->partno))
- fatal("%s: no such partition\n"), pnam;
+ fatalError("%s: no such partition\n", pnam);
return pno;
}
/* Have at least 4 partitions been defined? */
if (partno < 4) {
if (!partno)
- fatal("no partition table present.\n");
+ fatalError("no partition table present.\n");
else
- fatal("strange, only %d partitions defined.\n"), partno;
+ fatalError("strange, only %d partitions defined.\n", partno);
return 0;
}
q = p->ep;
if (p->start < q->start
|| p->start + p->size > q->start + q->size) {
- warn("Warning: partition %s "), PNO(p);
- warn("is not contained in partition %s\n"), PNO(q);
+ warn("Warning: partition %s ", PNO(p));
+ warn("is not contained in partition %s\n", PNO(q));
return 0;
}
}
for (q = p + 1; q < partitions + partno; q++)
if (q->size && !is_extended(q->p.sys_type))
if (!((p->start > q->start) ? disj(q, p) : disj(p, q))) {
- warn("Warning: partitions %s "), PNO(p);
- warn("and %s overlap\n"), PNO(q);
+ warn("Warning: partitions %s ", PNO(p));
+ warn("and %s overlap\n", PNO(q));
return 0;
}
}
if (save_sector_file) {
if (!save_sectors(dev, fd)) {
- fatal("Failed saving the old sectors - aborting\n");
+ fatalError("Failed saving the old sectors - aborting\n");
return 0;
}
}
if (!write_sectors(dev, fd)) {
- error("Failed writing the partition on %s\n"), dev;
+ errorMsg("Failed writing the partition on %s\n", dev);
return 0;
}
return 1;
return RD_EOF;
}
if (!(lp = index(lp, '\n')))
- fatal("long or incomplete input line - quitting\n");
+ fatalError("long or incomplete input line - quitting\n");
*lp = 0;
/* remove comments, if any */
while (isalnum(*ip)) /* 0x07FF */
ip++;
} else
- fatal("input error: `=' expected after %s field\n",
+ fatalError("input error: `=' expected after %s field\n",
d->fldname);
if (fno <= d->fldno)
fno = d->fldno + 1;
if (*ip == 0)
return fno;
if (*ip != ',' && *ip != ';')
- fatal
+ fatalError
("input error: unexpected character %c after %s field\n",
*ip, d->fldname);
*ip = 0;
goto nxtfld;
}
}
- fatal("unrecognized input: %s\n"), ip;
+ fatalError("unrecognized input: %s\n", ip);
}
/* split line into fields */
while (!(i = read_line(pno, ep, dev, interactive, z)))
if (!interactive)
- fatal("bad input\n");
+ fatalError("bad input\n");
if (i < 0) {
p->ep = ep;
return 0;
}
if (do_id) {
if ((do_id & PRINT_ID) != 0 && optind != argc - 2)
- fatal("usage: sfdisk --print-id device partition-number\n");
+ fatalError("usage: sfdisk --print-id device partition-number\n");
else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3)
- fatal
+ fatalError
("usage: sfdisk --change-id device partition-number Id\n");
else if (optind != argc - 3 && optind != argc - 2)
- fatal("usage: sfdisk --id device partition-number [Id]\n");
+ fatalError("usage: sfdisk --id device partition-number [Id]\n");
do_change_id(argv[optind], argv[optind + 1],
(optind == argc - 2) ? 0 : argv[optind + 2]);
exit(exit_status);
}
if (optind != argc - 1)
- fatal("can specify only one device (except with -l or -s)\n");
+ fatalError("can specify only one device (except with -l or -s)\n");
dev = argv[optind];
if (opt_reread)
fd = open(dev, mode);
if (fd < 0 && !silent) {
perror(dev);
- fatal("cannot open %s %s\n", dev,
+ fatalError("cannot open %s %s\n", dev,
rw ? "read-write" : "for reading");
}
return fd;
if (verify) {
if (partitions_ok(z))
- warn("%s: OK\n"), dev;
+ warn("%s: OK\n", dev);
else
exit_status = 1;
}
if (ioctl(fd, BLKGETSIZE, &size)) {
if (!silent) {
perror(dev);
- fatal("BLKGETSIZE ioctl failed for %s\n"), dev;
+ fatalError("BLKGETSIZE ioctl failed for %s\n", dev);
}
return;
}
if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
id -= 0x10;
else
- fatal("partition %s has id %x and is not hidden\n", pnam, id);
+ fatalError("partition %s has id %x and is not hidden\n", pnam, id);
z->partitions[pno].p.sys_type = id;
}
}
i = strtoul(id, NULL, 16);
if (i > 255)
- fatal("Bad Id %x\n"), i;
+ fatalError("Bad Id %lx\n", i);
z->partitions[pno].p.sys_type = i;
if (write_partitions(dev, fd, z))
if (stat(dev, &statbuf) < 0) {
perror(dev);
- fatal("Fatal error: cannot find %s\n"), dev;
+ fatalError("Fatal error: cannot find %s\n", dev);
}
if (!S_ISBLK(statbuf.st_mode)) {
- warn("Warning: %s is not a block device\n"), dev;
+ warn("Warning: %s is not a block device\n", dev);
no_reread = 1;
}
fd = my_open(dev, !no_write, 0);
out_partitions(dev, z);
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;
+ fatalError("Partition %d does not exist, cannot change it\n", one_only);
z = &newp;
if (!partitions_ok(z) && !force) {
if (!interactive)
- fatal("I don't like these partitions - nothing changed.\n"
+ fatalError("I don't like these partitions - nothing changed.\n"
"(If you really want this, use the --force option.)\n");
else
printf
if (c == EOF)
printf("\nsfdisk: premature end of input\n");
if (c == EOF || answer == 'q' || answer == 'Q') {
- fatal("Quitting - nothing changed\n");
+ fatalError("Quitting - nothing changed\n");
} else if (answer == 'n' || answer == 'N') {
continue;
} else if (answer == 'y' || answer == 'Y') {