aboutsummaryrefslogtreecommitdiff
path: root/util-linux/fdisk.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/fdisk.c')
-rw-r--r--util-linux/fdisk.c90
1 files changed, 44 insertions, 46 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index e47bc7324..296716575 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -426,7 +426,7 @@ struct globals {
426 unsigned sector_offset; // = 1; 426 unsigned sector_offset; // = 1;
427 unsigned g_heads, g_sectors, g_cylinders; 427 unsigned g_heads, g_sectors, g_cylinders;
428 smallint /* enum label_type */ current_label_type; 428 smallint /* enum label_type */ current_label_type;
429 smallint display_in_cyl_units; // = 1; 429 smallint display_in_cyl_units;
430#if ENABLE_FEATURE_OSF_LABEL 430#if ENABLE_FEATURE_OSF_LABEL
431 smallint possibly_osf_label; 431 smallint possibly_osf_label;
432#endif 432#endif
@@ -488,7 +488,6 @@ struct globals {
488 sector_size = DEFAULT_SECTOR_SIZE; \ 488 sector_size = DEFAULT_SECTOR_SIZE; \
489 sector_offset = 1; \ 489 sector_offset = 1; \
490 g_partitions = 4; \ 490 g_partitions = 4; \
491 display_in_cyl_units = 1; \
492 units_per_sector = 1; \ 491 units_per_sector = 1; \
493 dos_compatible_flag = 1; \ 492 dos_compatible_flag = 1; \
494} while (0) 493} while (0)
@@ -639,25 +638,6 @@ seek_sector(sector_t secno)
639} 638}
640 639
641#if ENABLE_FEATURE_FDISK_WRITABLE 640#if ENABLE_FEATURE_FDISK_WRITABLE
642/* Read line; return 0 or first printable char */
643static int
644read_line(const char *prompt)
645{
646 int sz;
647
648 sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer));
649 if (sz <= 0)
650 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
651
652 if (line_buffer[sz-1] == '\n')
653 line_buffer[--sz] = '\0';
654
655 line_ptr = line_buffer;
656 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
657 line_ptr++;
658 return *line_ptr;
659}
660
661static void 641static void
662set_all_unchanged(void) 642set_all_unchanged(void)
663{ 643{
@@ -680,6 +660,25 @@ write_part_table_flag(char *b)
680 b[511] = 0xaa; 660 b[511] = 0xaa;
681} 661}
682 662
663/* Read line; return 0 or first printable non-space char */
664static int
665read_line(const char *prompt)
666{
667 int sz;
668
669 sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer));
670 if (sz <= 0)
671 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
672
673 if (line_buffer[sz-1] == '\n')
674 line_buffer[--sz] = '\0';
675
676 line_ptr = line_buffer;
677 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
678 line_ptr++;
679 return *line_ptr;
680}
681
683static char 682static char
684read_nonempty(const char *mesg) 683read_nonempty(const char *mesg)
685{ 684{
@@ -1614,7 +1613,7 @@ read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *
1614 1613
1615 if (*line_ptr == '+' || *line_ptr == '-') { 1614 if (*line_ptr == '+' || *line_ptr == '-') {
1616 int minus = (*line_ptr == '-'); 1615 int minus = (*line_ptr == '-');
1617 int absolute = 0; 1616 unsigned scale_shift;
1618 1617
1619 if (sizeof(value) <= sizeof(long)) 1618 if (sizeof(value) <= sizeof(long))
1620 value = strtoul(line_ptr + 1, NULL, 10); 1619 value = strtoul(line_ptr + 1, NULL, 10);
@@ -1622,48 +1621,46 @@ read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *
1622 value = strtoull(line_ptr + 1, NULL, 10); 1621 value = strtoull(line_ptr + 1, NULL, 10);
1623 1622
1624 /* (1) if 2nd char is digit, use_default = 0. 1623 /* (1) if 2nd char is digit, use_default = 0.
1625 * (2) move line_ptr to first non-digit. */ 1624 * (2) move line_ptr to first non-digit.
1625 */
1626 while (isdigit(*++line_ptr)) 1626 while (isdigit(*++line_ptr))
1627 use_default = 0; 1627 use_default = 0;
1628 1628
1629 switch (*line_ptr) { 1629 scale_shift = 0;
1630 case 'c': 1630 switch (*line_ptr | 0x20) {
1631 case 'C':
1632 if (!display_in_cyl_units)
1633 value *= g_heads * g_sectors;
1634 break;
1635 case 'K':
1636 absolute = 1024;
1637 break;
1638 case 'k': 1631 case 'k':
1639 absolute = 1000; 1632 scale_shift = 10; /* 1024 */
1640 break; 1633 break;
1641 case 'm': 1634 case 'm':
1642 case 'M': 1635 scale_shift = 20; /* 1024*1024 */
1643 absolute = 1000000;
1644 break; 1636 break;
1645 case 'g': 1637 case 'g':
1646 case 'G': 1638 scale_shift = 30; /* 1024*1024*1024 */
1647 absolute = 1000000000; 1639 break;
1640 case 't':
1641 scale_shift = 40; /* 1024*1024*1024*1024 */
1648 break; 1642 break;
1649 default: 1643 default:
1650 break; 1644 break;
1651 } 1645 }
1652 if (absolute) { 1646 if (scale_shift) {
1653 ullong bytes; 1647 ullong bytes;
1654 unsigned long unit; 1648 unsigned long unit;
1655 1649
1656 bytes = (ullong) value * absolute; 1650 bytes = (ullong) value << scale_shift;
1657 unit = sector_size * units_per_sector; 1651 unit = sector_size * units_per_sector;
1658 bytes += unit/2; /* round */ 1652 bytes += unit/2; /* round */
1659 bytes /= unit; 1653 bytes /= unit;
1660 value = bytes; 1654 value = (bytes != 0 ? bytes - 1 : 0);
1661 } 1655 }
1662 if (minus) 1656 if (minus)
1663 value = -value; 1657 value = -value;
1664 value += base; 1658 value += base;
1665 } else { 1659 } else {
1666 value = atoi(line_ptr); 1660 if (sizeof(value) <= sizeof(long))
1661 value = strtoul(line_ptr, NULL, 10);
1662 else
1663 value = strtoull(line_ptr, NULL, 10);
1667 while (isdigit(*line_ptr)) { 1664 while (isdigit(*line_ptr)) {
1668 line_ptr++; 1665 line_ptr++;
1669 use_default = 0; 1666 use_default = 0;
@@ -2529,8 +2526,9 @@ add_partition(int n, int sys)
2529 stop = limit; 2526 stop = limit;
2530 } else { 2527 } else {
2531 snprintf(mesg, sizeof(mesg), 2528 snprintf(mesg, sizeof(mesg),
2532 "Last %s or +size or +sizeM or +sizeK", 2529 "Last %s or +size{,K,M,G,T}",
2533 str_units(SINGULAR)); 2530 str_units(SINGULAR)
2531 );
2534 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg); 2532 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
2535 if (display_in_cyl_units) { 2533 if (display_in_cyl_units) {
2536 stop = stop * units_per_sector - 1; 2534 stop = stop * units_per_sector - 1;
@@ -2614,9 +2612,9 @@ new_partition(void)
2614 } else { 2612 } else {
2615 char c, line[80]; 2613 char c, line[80];
2616 snprintf(line, sizeof(line), 2614 snprintf(line, sizeof(line),
2617 "Command action\n" 2615 "Partition type\n"
2618 " %s\n" 2616 " p primary partition (1-4)\n"
2619 " p primary partition (1-4)\n", 2617 " %s\n",
2620 (extended_offset ? 2618 (extended_offset ?
2621 "l logical (5 or over)" : "e extended")); 2619 "l logical (5 or over)" : "e extended"));
2622 while (1) { 2620 while (1) {