aboutsummaryrefslogtreecommitdiff
path: root/sfdisk.c
diff options
context:
space:
mode:
Diffstat (limited to 'sfdisk.c')
-rw-r--r--sfdisk.c99
1 files changed, 38 insertions, 61 deletions
diff --git a/sfdisk.c b/sfdisk.c
index 116a019ab..f23eb5611 100644
--- a/sfdisk.c
+++ b/sfdisk.c
@@ -219,29 +219,6 @@ static void warn(char *s, ...)
219 va_end(p); 219 va_end(p);
220} 220}
221 221
222static void error(char *s, ...)
223{
224 va_list p;
225
226 va_start(p, s);
227 fflush(stdout);
228 fprintf(stderr, "\n" PROGNAME ": ");
229 vfprintf(stderr, s, p);
230 va_end(p);
231}
232
233static void fatal(char *s, ...)
234{
235 va_list p;
236
237 va_start(p, s);
238 fflush(stdout);
239 fprintf(stderr, "\n" PROGNAME ": ");
240 vfprintf(stderr, s, p);
241 va_end(p);
242 exit(1);
243}
244
245/* 222/*
246 * A. About seeking 223 * A. About seeking
247 */ 224 */
@@ -273,12 +250,12 @@ static int sseek(char *dev, unsigned int fd, unsigned long s)
273 if ((out = lseek(fd, in, SEEK_SET)) != in) { 250 if ((out = lseek(fd, in, SEEK_SET)) != in) {
274#endif 251#endif
275 perror("llseek"); 252 perror("llseek");
276 error("seek error on %s - cannot seek to %lu\n", dev, s); 253 errorMsg("seek error on %s - cannot seek to %lu\n", dev, s, FALSE);
277 return 0; 254 return 0;
278 } 255 }
279 256
280 if (in != out) { 257 if (in != out) {
281 error("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n", 258 errorMsg("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n",
282 (uint) (in >> 32), (uint) (in & 0xffffffff), 259 (uint) (in >> 32), (uint) (in & 0xffffffff),
283 (uint) (out >> 32), (uint) (out & 0xffffffff)); 260 (uint) (out >> 32), (uint) (out & 0xffffffff));
284 return 0; 261 return 0;
@@ -324,11 +301,11 @@ static struct sector *get_sector(char *dev, int fd, unsigned long sno)
324 return 0; 301 return 0;
325 302
326 if (!(s = (struct sector *) malloc(sizeof(struct sector)))) 303 if (!(s = (struct sector *) malloc(sizeof(struct sector))))
327 fatal("out of memory - giving up\n"); 304 fatalError("out of memory - giving up\n");
328 305
329 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) { 306 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
330 perror("read"); 307 perror("read");
331 error("read error on %s - cannot read sector %lu\n", dev, sno); 308 errorMsg("read error on %s - cannot read sector %lu\n", dev, sno);
332 free(s); 309 free(s);
333 return 0; 310 return 0;
334 } 311 }
@@ -344,7 +321,7 @@ static struct sector *get_sector(char *dev, int fd, unsigned long sno)
344static int msdos_signature(struct sector *s) 321static int msdos_signature(struct sector *s)
345{ 322{
346 if (*(unsigned short *) (s->data + 0x1fe) != 0xaa55) { 323 if (*(unsigned short *) (s->data + 0x1fe) != 0xaa55) {
347 error("ERROR: sector %lu does not have an msdos signature\n", 324 errorMsg("ERROR: sector %lu does not have an msdos signature\n",
348 s->sectornumber); 325 s->sectornumber);
349 return 0; 326 return 0;
350 } 327 }
@@ -361,7 +338,7 @@ static int write_sectors(char *dev, int fd)
361 return 0; 338 return 0;
362 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) { 339 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
363 perror("write"); 340 perror("write");
364 error("write error on %s - cannot write sector %lu\n", 341 errorMsg("write error on %s - cannot write sector %lu\n",
365 dev, s->sectornumber); 342 dev, s->sectornumber);
366 return 0; 343 return 0;
367 } 344 }
@@ -399,7 +376,7 @@ static int save_sectors(char *dev, int fdin)
399 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444); 376 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
400 if (fdout < 0) { 377 if (fdout < 0) {
401 perror(save_sector_file); 378 perror(save_sector_file);
402 error("cannot open partition sector save file (%s)\n", 379 errorMsg("cannot open partition sector save file (%s)\n",
403 save_sector_file); 380 save_sector_file);
404 return 0; 381 return 0;
405 } 382 }
@@ -411,13 +388,13 @@ static int save_sectors(char *dev, int fdin)
411 return 0; 388 return 0;
412 if (read(fdin, ss + 4, 512) != 512) { 389 if (read(fdin, ss + 4, 512) != 512) {
413 perror("read"); 390 perror("read");
414 error("read error on %s - cannot read sector %lu\n", 391 errorMsg("read error on %s - cannot read sector %lu\n",
415 dev, s->sectornumber); 392 dev, s->sectornumber);
416 return 0; 393 return 0;
417 } 394 }
418 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) { 395 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
419 perror("write"); 396 perror("write");
420 error("write error on %s\n"), save_sector_file; 397 errorMsg("write error on %s\n"), save_sector_file;
421 return 0; 398 return 0;
422 } 399 }
423 } 400 }
@@ -435,35 +412,35 @@ static int restore_sectors(char *dev)
435 412
436 if (stat(restore_sector_file, &statbuf) < 0) { 413 if (stat(restore_sector_file, &statbuf) < 0) {
437 perror(restore_sector_file); 414 perror(restore_sector_file);
438 error("cannot stat partition restore file (%s)\n", 415 errorMsg("cannot stat partition restore file (%s)\n",
439 restore_sector_file); 416 restore_sector_file);
440 return 0; 417 return 0;
441 } 418 }
442 if (statbuf.st_size % 516) { 419 if (statbuf.st_size % 516) {
443 error("partition restore file has wrong size - not restoring\n"); 420 errorMsg("partition restore file has wrong size - not restoring\n");
444 return 0; 421 return 0;
445 } 422 }
446 if (!(ss = (char *) malloc(statbuf.st_size))) { 423 if (!(ss = (char *) malloc(statbuf.st_size))) {
447 error("out of memory?\n"); 424 errorMsg("out of memory?\n");
448 return 0; 425 return 0;
449 } 426 }
450 fdin = open(restore_sector_file, O_RDONLY); 427 fdin = open(restore_sector_file, O_RDONLY);
451 if (fdin < 0) { 428 if (fdin < 0) {
452 perror(restore_sector_file); 429 perror(restore_sector_file);
453 error("cannot open partition restore file (%s)\n", 430 errorMsg("cannot open partition restore file (%s)\n",
454 restore_sector_file); 431 restore_sector_file);
455 return 0; 432 return 0;
456 } 433 }
457 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) { 434 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
458 perror("read"); 435 perror("read");
459 error("error reading %s\n"), restore_sector_file; 436 errorMsg("error reading %s\n"), restore_sector_file;
460 return 0; 437 return 0;
461 } 438 }
462 439
463 fdout = open(dev, O_WRONLY); 440 fdout = open(dev, O_WRONLY);
464 if (fdout < 0) { 441 if (fdout < 0) {
465 perror(dev); 442 perror(dev);
466 error("cannot open device %s for writing\n"), dev; 443 errorMsg("cannot open device %s for writing\n"), dev;
467 return 0; 444 return 0;
468 } 445 }
469 446
@@ -475,7 +452,7 @@ static int restore_sectors(char *dev)
475 return 0; 452 return 0;
476 if (write(fdout, ss + 4, 512) != 512) { 453 if (write(fdout, ss + 4, 512) != 512) {
477 perror(dev); 454 perror(dev);
478 error("error writing sector %lu on %s\n", sno, dev); 455 errorMsg("error writing sector %lu on %s\n", sno, dev);
479 return 0; 456 return 0;
480 } 457 }
481 ss += 516; 458 ss += 516;
@@ -905,7 +882,7 @@ static int asc_to_index(char *pnam, struct disk_desc *z)
905 pno = linux_to_index(pnum, z); 882 pno = linux_to_index(pnum, z);
906 } 883 }
907 if (!(pno >= 0 && pno < z->partno)) 884 if (!(pno >= 0 && pno < z->partno))
908 fatal("%s: no such partition\n"), pnam; 885 fatalError("%s: no such partition\n"), pnam;
909 return pno; 886 return pno;
910} 887}
911 888
@@ -1233,9 +1210,9 @@ static int partitions_ok(struct disk_desc *z)
1233 /* Have at least 4 partitions been defined? */ 1210 /* Have at least 4 partitions been defined? */
1234 if (partno < 4) { 1211 if (partno < 4) {
1235 if (!partno) 1212 if (!partno)
1236 fatal("no partition table present.\n"); 1213 fatalError("no partition table present.\n");
1237 else 1214 else
1238 fatal("strange, only %d partitions defined.\n"), partno; 1215 fatalError("strange, only %d partitions defined.\n"), partno;
1239 return 0; 1216 return 0;
1240 } 1217 }
1241 1218
@@ -1680,12 +1657,12 @@ static int write_partitions(char *dev, int fd, struct disk_desc *z)
1680 } 1657 }
1681 if (save_sector_file) { 1658 if (save_sector_file) {
1682 if (!save_sectors(dev, fd)) { 1659 if (!save_sectors(dev, fd)) {
1683 fatal("Failed saving the old sectors - aborting\n"); 1660 fatalError("Failed saving the old sectors - aborting\n");
1684 return 0; 1661 return 0;
1685 } 1662 }
1686 } 1663 }
1687 if (!write_sectors(dev, fd)) { 1664 if (!write_sectors(dev, fd)) {
1688 error("Failed writing the partition on %s\n"), dev; 1665 errorMsg("Failed writing the partition on %s\n"), dev;
1689 return 0; 1666 return 0;
1690 } 1667 }
1691 return 1; 1668 return 1;
@@ -1765,7 +1742,7 @@ read_stdin(unsigned char **fields, unsigned char *line, int fieldssize,
1765 return RD_EOF; 1742 return RD_EOF;
1766 } 1743 }
1767 if (!(lp = index(lp, '\n'))) 1744 if (!(lp = index(lp, '\n')))
1768 fatal("long or incomplete input line - quitting\n"); 1745 fatalError("long or incomplete input line - quitting\n");
1769 *lp = 0; 1746 *lp = 0;
1770 1747
1771 /* remove comments, if any */ 1748 /* remove comments, if any */
@@ -1801,21 +1778,21 @@ read_stdin(unsigned char **fields, unsigned char *line, int fieldssize,
1801 while (isalnum(*ip)) /* 0x07FF */ 1778 while (isalnum(*ip)) /* 0x07FF */
1802 ip++; 1779 ip++;
1803 } else 1780 } else
1804 fatal("input error: `=' expected after %s field\n", 1781 fatalError("input error: `=' expected after %s field\n",
1805 d->fldname); 1782 d->fldname);
1806 if (fno <= d->fldno) 1783 if (fno <= d->fldno)
1807 fno = d->fldno + 1; 1784 fno = d->fldno + 1;
1808 if (*ip == 0) 1785 if (*ip == 0)
1809 return fno; 1786 return fno;
1810 if (*ip != ',' && *ip != ';') 1787 if (*ip != ',' && *ip != ';')
1811 fatal 1788 fatalError
1812 ("input error: unexpected character %c after %s field\n", 1789 ("input error: unexpected character %c after %s field\n",
1813 *ip, d->fldname); 1790 *ip, d->fldname);
1814 *ip = 0; 1791 *ip = 0;
1815 goto nxtfld; 1792 goto nxtfld;
1816 } 1793 }
1817 } 1794 }
1818 fatal("unrecognized input: %s\n"), ip; 1795 fatalError("unrecognized input: %s\n"), ip;
1819 } 1796 }
1820 1797
1821 /* split line into fields */ 1798 /* split line into fields */
@@ -2251,7 +2228,7 @@ read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
2251 2228
2252 while (!(i = read_line(pno, ep, dev, interactive, z))) 2229 while (!(i = read_line(pno, ep, dev, interactive, z)))
2253 if (!interactive) 2230 if (!interactive)
2254 fatal("bad input\n"); 2231 fatalError("bad input\n");
2255 if (i < 0) { 2232 if (i < 0) {
2256 p->ep = ep; 2233 p->ep = ep;
2257 return 0; 2234 return 0;
@@ -2612,19 +2589,19 @@ extern int sfdisk_main(int argc, char **argv)
2612 } 2589 }
2613 if (do_id) { 2590 if (do_id) {
2614 if ((do_id & PRINT_ID) != 0 && optind != argc - 2) 2591 if ((do_id & PRINT_ID) != 0 && optind != argc - 2)
2615 fatal("usage: sfdisk --print-id device partition-number\n"); 2592 fatalError("usage: sfdisk --print-id device partition-number\n");
2616 else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3) 2593 else if ((do_id & CHANGE_ID) != 0 && optind != argc - 3)
2617 fatal 2594 fatalError
2618 ("usage: sfdisk --change-id device partition-number Id\n"); 2595 ("usage: sfdisk --change-id device partition-number Id\n");
2619 else if (optind != argc - 3 && optind != argc - 2) 2596 else if (optind != argc - 3 && optind != argc - 2)
2620 fatal("usage: sfdisk --id device partition-number [Id]\n"); 2597 fatalError("usage: sfdisk --id device partition-number [Id]\n");
2621 do_change_id(argv[optind], argv[optind + 1], 2598 do_change_id(argv[optind], argv[optind + 1],
2622 (optind == argc - 2) ? 0 : argv[optind + 2]); 2599 (optind == argc - 2) ? 0 : argv[optind + 2]);
2623 exit(exit_status); 2600 exit(exit_status);
2624 } 2601 }
2625 2602
2626 if (optind != argc - 1) 2603 if (optind != argc - 1)
2627 fatal("can specify only one device (except with -l or -s)\n"); 2604 fatalError("can specify only one device (except with -l or -s)\n");
2628 dev = argv[optind]; 2605 dev = argv[optind];
2629 2606
2630 if (opt_reread) 2607 if (opt_reread)
@@ -2649,7 +2626,7 @@ static int my_open(char *dev, int rw, int silent)
2649 fd = open(dev, mode); 2626 fd = open(dev, mode);
2650 if (fd < 0 && !silent) { 2627 if (fd < 0 && !silent) {
2651 perror(dev); 2628 perror(dev);
2652 fatal("cannot open %s %s\n", dev, 2629 fatalError("cannot open %s %s\n", dev,
2653 rw ? "read-write" : "for reading"); 2630 rw ? "read-write" : "for reading");
2654 } 2631 }
2655 return fd; 2632 return fd;
@@ -2711,7 +2688,7 @@ static void do_size(char *dev, int silent)
2711 if (ioctl(fd, BLKGETSIZE, &size)) { 2688 if (ioctl(fd, BLKGETSIZE, &size)) {
2712 if (!silent) { 2689 if (!silent) {
2713 perror(dev); 2690 perror(dev);
2714 fatal("BLKGETSIZE ioctl failed for %s\n"), dev; 2691 fatalError("BLKGETSIZE ioctl failed for %s\n"), dev;
2715 } 2692 }
2716 return; 2693 return;
2717 } 2694 }
@@ -2831,7 +2808,7 @@ static void set_unhidden(struct disk_desc *z, char *pnam)
2831 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17) 2808 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
2832 id -= 0x10; 2809 id -= 0x10;
2833 else 2810 else
2834 fatal("partition %s has id %x and is not hidden\n", pnam, id); 2811 fatalError("partition %s has id %x and is not hidden\n", pnam, id);
2835 z->partitions[pno].p.sys_type = id; 2812 z->partitions[pno].p.sys_type = id;
2836} 2813}
2837 2814
@@ -2889,7 +2866,7 @@ static void do_change_id(char *dev, char *pnam, char *id)
2889 } 2866 }
2890 i = strtoul(id, NULL, 16); 2867 i = strtoul(id, NULL, 16);
2891 if (i > 255) 2868 if (i > 255)
2892 fatal("Bad Id %x\n"), i; 2869 fatalError("Bad Id %x\n"), i;
2893 z->partitions[pno].p.sys_type = i; 2870 z->partitions[pno].p.sys_type = i;
2894 2871
2895 if (write_partitions(dev, fd, z)) 2872 if (write_partitions(dev, fd, z))
@@ -2921,7 +2898,7 @@ static void do_fdisk(char *dev)
2921 2898
2922 if (stat(dev, &statbuf) < 0) { 2899 if (stat(dev, &statbuf) < 0) {
2923 perror(dev); 2900 perror(dev);
2924 fatal("Fatal error: cannot find %s\n"), dev; 2901 fatalError("Fatal error: cannot find %s\n"), dev;
2925 } 2902 }
2926 if (!S_ISBLK(statbuf.st_mode)) { 2903 if (!S_ISBLK(statbuf.st_mode)) {
2927 warn("Warning: %s is not a block device\n"), dev; 2904 warn("Warning: %s is not a block device\n"), dev;
@@ -2954,7 +2931,7 @@ static void do_fdisk(char *dev)
2954 out_partitions(dev, z); 2931 out_partitions(dev, z);
2955 2932
2956 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0) 2933 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
2957 fatal("Partition %d does not exist, cannot change it\n"), one_only; 2934 fatalError("Partition %d does not exist, cannot change it\n"), one_only;
2958 2935
2959 z = &newp; 2936 z = &newp;
2960 2937
@@ -2967,7 +2944,7 @@ static void do_fdisk(char *dev)
2967 2944
2968 if (!partitions_ok(z) && !force) { 2945 if (!partitions_ok(z) && !force) {
2969 if (!interactive) 2946 if (!interactive)
2970 fatal("I don't like these partitions - nothing changed.\n" 2947 fatalError("I don't like these partitions - nothing changed.\n"
2971 "(If you really want this, use the --force option.)\n"); 2948 "(If you really want this, use the --force option.)\n");
2972 else 2949 else
2973 printf 2950 printf
@@ -2985,7 +2962,7 @@ static void do_fdisk(char *dev)
2985 if (c == EOF) 2962 if (c == EOF)
2986 printf("\nsfdisk: premature end of input\n"); 2963 printf("\nsfdisk: premature end of input\n");
2987 if (c == EOF || answer == 'q' || answer == 'Q') { 2964 if (c == EOF || answer == 'q' || answer == 'Q') {
2988 fatal("Quitting - nothing changed\n"); 2965 fatalError("Quitting - nothing changed\n");
2989 } else if (answer == 'n' || answer == 'N') { 2966 } else if (answer == 'n' || answer == 'N') {
2990 continue; 2967 continue;
2991 } else if (answer == 'y' || answer == 'Y') { 2968 } else if (answer == 'y' || answer == 'Y') {