unsigned int user_cylinders, user_heads, user_sectors;
+void toggle_units(struct fdisk_context *cxt)
+{
+ fdisk_context_set_unit(cxt,
+ fdisk_context_use_cylinders(cxt) ? "sectors" :
+ "cylinders");
+ if (fdisk_context_use_cylinders(cxt))
+ fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
+ else
+ fdisk_info(cxt, _("Changing display/entry units to sectors."));
+}
+
+
static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
fprintf(out, _("Usage:\n"
/*
* Cylinders
*/
- if (!display_in_cyl_units)
+ if (fdisk_context_use_cylinders(cxt))
res *= cxt->geom.heads * cxt->geom.sectors;
} else if (*line_ptr &&
*(line_ptr + 1) == 'B' &&
unsigned long unit;
bytes = (unsigned long long) res * absolute;
- unit = cxt->sector_size * units_per_sector;
+ unit = cxt->sector_size * fdisk_context_get_units_per_sector(cxt);
bytes += unit/2; /* round */
bytes /= unit;
res = bytes;
return get_partition(cxt, warn, max);
}
-const char *
-str_units(int n)
-{
- if (display_in_cyl_units)
- return P_("cylinder", "cylinders", n);
- return P_("sector", "sectors", n);
-}
-
static void toggle_dos_compatibility_flag(struct fdisk_context *cxt)
{
struct fdisk_label *lb = fdisk_context_get_label(cxt, "dos");
printf(_("%d heads, %llu sectors/track, %llu cylinders\n"),
cxt->geom.heads, cxt->geom.sectors, cxt->geom.cylinders);
printf(_("Units = %s of %d * %ld = %ld bytes\n"),
- str_units(PLURAL),
- units_per_sector, cxt->sector_size, units_per_sector * cxt->sector_size);
+ fdisk_context_get_unit(cxt, PLURAL),
+ fdisk_context_get_units_per_sector(cxt),
+ cxt->sector_size,
+ fdisk_context_get_units_per_sector(cxt) * cxt->sector_size);
printf(_("Sector size (logical/physical): %lu bytes / %lu bytes\n"),
cxt->sector_size, cxt->phy_sector_size);
/*
* Try BSD -- TODO: move to list_table() too
*/
- update_units(cxt);
list_disk_geometry(cxt);
if (!fdisk_is_disklabel(cxt, AIX) &&
!fdisk_is_disklabel(cxt, MAC))
change_partition_type(cxt);
break;
case 'u':
- change_units(cxt);
+ toggle_units(cxt);
break;
case 'v':
verify(cxt);
opts = 1;
break;
case 'u':
- display_in_cyl_units = 0; /* default */
- if (optarg && strcmp(optarg, "=cylinders") == 0)
- display_in_cyl_units = !display_in_cyl_units;
- else if (optarg && strcmp(optarg, "=sectors"))
+ if (optarg && *optarg == '=')
+ optarg++;
+ if (fdisk_context_set_unit(cxt, optarg) != 0)
usage(stderr);
break;
case 'V':
print_welcome();
if (!fdisk_dev_has_disklabel(cxt)) {
- update_units(cxt); /* to provide compatible 'p'rint output */
fprintf(stderr,
_("Device does not contain a recognized partition table\n"));
fdisk_create_disklabel(cxt, NULL);
#define IS_EXTENDED(i) \
((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
-#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
-#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
+extern void toggle_units(struct fdisk_context *cxt);
+
+static inline unsigned long
+scround(struct fdisk_context *cxt, unsigned long num)
+{
+ unsigned long un = fdisk_context_get_units_per_sector(cxt);
+ return (num + un - 1) / un;
+}
struct partition {
unsigned char boot_ind; /* 0x80 - active */
/* prototypes for fdisk.c */
extern char *line_ptr;
-extern unsigned int display_in_cyl_units, units_per_sector;
extern void fatal(struct fdisk_context *cxt, enum failure why);
extern int get_partition(struct fdisk_context *cxt, int warn, int max);
#define PLURAL 0
#define SINGULAR 1
-extern const char * str_units(int);
extern sector_t get_nr_sects(struct partition *p);
static struct xbsd_disklabel xbsd_dlabel;
-#define bsd_cround(n) \
- (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
+#define bsd_cround(c, n) \
+ (fdisk_context_use_cylinders(c) ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
/*
* Test whether the whole disk has BSD disk label magic.
end = xbsd_dlabel.d_secperunit - 1;
#endif
- snprintf (mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
- begin = read_int(cxt, bsd_cround (begin), bsd_cround (begin), bsd_cround (end),
+ snprintf (mesg, sizeof(mesg), _("First %s"),
+ fdisk_context_get_unit(cxt, SINGULAR));
+ begin = read_int(cxt, bsd_cround (cxt, begin),
+ bsd_cround (cxt, begin),
+ bsd_cround (cxt, end),
0, mesg);
- if (display_in_cyl_units)
+ if (fdisk_context_use_cylinders(cxt))
begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
snprintf (mesg, sizeof(mesg), _("Last %s or +size or +sizeM or +sizeK"),
- str_units(SINGULAR));
- end = read_int (cxt, bsd_cround (begin), bsd_cround (end), bsd_cround (end),
- bsd_cround (begin), mesg);
+ fdisk_context_get_unit(cxt, SINGULAR));
+ end = read_int (cxt, bsd_cround (cxt, begin),
+ bsd_cround (cxt, end),
+ bsd_cround (cxt, end),
+ bsd_cround (cxt, begin), mesg);
- if (display_in_cyl_units)
+ if (fdisk_context_use_cylinders(cxt))
end = end * xbsd_dlabel.d_secpercyl - 1;
xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
xbsd_change_fstype (cxt);
break;
case 'u':
- change_units(cxt);
+ toggle_units(cxt);
break;
case 'w':
xbsd_write_disklabel (cxt);
pp = lp->d_partitions;
for (i = 0; i < lp->d_npartitions; i++, pp++) {
if (pp->p_size) {
- if (display_in_cyl_units && lp->d_secpercyl) {
+ if (fdisk_context_use_cylinders(cxt) && lp->d_secpercyl) {
fprintf(f, " %c: %8ld%c %8ld%c %8ld%c ",
'a' + i,
(long) pp->p_offset / lp->d_secpercyl + 1,
static size_t ext_index;
-unsigned int units_per_sector = 1, display_in_cyl_units = 0;
-
static int MBRbuffer_changed;
-void update_units(struct fdisk_context *cxt)
-{
- int cyl_units = cxt->geom.heads * cxt->geom.sectors;
-
- if (display_in_cyl_units && cyl_units)
- units_per_sector = cyl_units;
- else
- units_per_sector = 1; /* in sectors */
-}
-
-void change_units(struct fdisk_context *cxt)
-{
- display_in_cyl_units = !display_in_cyl_units;
- update_units(cxt);
-
- if (display_in_cyl_units)
- printf(_("Changing display/entry units to cylinders (DEPRECATED!)\n"));
- else
- printf(_("Changing display/entry units to sectors\n"));
-}
-
+#define cround(c, n) (fdisk_context_use_cylinders(c) ? \
+ ((n) / fdisk_context_get_units_per_sector(c)) + 1 : (n))
static void warn_alignment(struct fdisk_context *cxt)
{
"WARNING: DOS-compatible mode is deprecated. It's strongly recommended to\n"
" switch off the mode (with command 'c')."));
- if (display_in_cyl_units)
+ if (fdisk_context_use_cylinders(cxt))
fprintf(stderr, _("\n"
"WARNING: cylinders as display units are deprecated. Use command 'u' to\n"
" change units to sectors.\n"));
cxt->grain = cxt->sector_size; /* usually 512 */
}
- /* units_per_sector has impact to deprecated DOS stuff */
- update_units(cxt);
return 0;
}
fill_bounds(cxt, first, last);
if (n < 4) {
start = cxt->first_lba;
- if (display_in_cyl_units || !cxt->total_sectors)
+ if (fdisk_context_use_cylinders(cxt) || !cxt->total_sectors)
limit = cxt->geom.heads * cxt->geom.sectors * cxt->geom.cylinders - 1;
else
limit = cxt->total_sectors - 1;
start = extended_offset + cxt->first_lba;
limit = get_start_sect(q) + get_nr_sects(q) - 1;
}
- if (display_in_cyl_units)
+ if (fdisk_context_use_cylinders(cxt))
for (i = 0; i < cxt->label->nparts_max; i++)
- first[i] = (cround(first[i]) - 1) * units_per_sector;
+ first[i] = (cround(cxt, first[i]) - 1) * fdisk_context_get_units_per_sector(cxt);
- snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
+ snprintf(mesg, sizeof(mesg), _("First %s"), fdisk_context_get_unit(cxt, SINGULAR));
do {
sector_t dflt, aligned;
dflt = start;
if (start > limit)
break;
- if (start >= temp+units_per_sector && read) {
+ if (start >= temp+fdisk_context_get_units_per_sector(cxt) && read) {
printf(_("Sector %llu is already allocated\n"), temp);
temp = start;
read = 0;
if (!read && start == temp) {
sector_t i = start;
- start = read_int(cxt, cround(i), cround(dflt), cround(limit),
+ start = read_int(cxt, cround(cxt, i), cround(cxt, dflt),
+ cround(cxt, limit),
0, mesg);
- if (display_in_cyl_units) {
- start = (start - 1) * units_per_sector;
+ if (fdisk_context_use_cylinders(cxt)) {
+ start = (start - 1) * fdisk_context_get_units_per_sector(cxt);
if (start < i) start = i;
}
read = 1;
cxt->label->nparts_max--;
return -ENOSPC;
}
- if (cround(start) == cround(limit)) {
+ if (cround(cxt, start) == cround(cxt, limit)) {
stop = limit;
} else {
int is_suffix_used = 0;
snprintf(mesg, sizeof(mesg),
_("Last %1$s, +%2$s or +size{K,M,G}"),
- str_units(SINGULAR), str_units(PLURAL));
+ fdisk_context_get_unit(cxt, SINGULAR), fdisk_context_get_unit(cxt, PLURAL));
stop = read_int_with_suffix(cxt,
- cround(start), cround(limit), cround(limit),
- cround(start), mesg, &is_suffix_used);
- if (display_in_cyl_units) {
- stop = stop * units_per_sector - 1;
+ cround(cxt, start), cround(cxt, limit),
+ cround(cxt, limit),
+ cround(cxt, start), mesg, &is_suffix_used);
+ if (fdisk_context_use_cylinders(cxt)) {
+ stop = stop * fdisk_context_get_units_per_sector(cxt) - 1;
if (stop >limit)
stop = limit;
}
partname(cxt->dev_path, i+1, w+2),
/* boot flag */ !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG
? '*' : '?',
-/* start */ (unsigned long) cround(get_partition_start(pe)),
-/* end */ (unsigned long) cround(get_partition_start(pe) + psects
+/* start */ (unsigned long) cround(cxt, get_partition_start(pe)),
+/* end */ (unsigned long) cround(cxt, get_partition_start(pe) + psects
- (psects ? 1 : 0)),
/* odd flag on end */ (unsigned long) pblocks, podd ? '+' : ' ',
/* type id */ p->sys_ind,
extern int mbr_is_valid_magic(unsigned char *b);
-extern void change_units(struct fdisk_context *cxt);
-extern void update_units(struct fdisk_context *cxt); /* called from sunlabel too */
-
#define is_dos_compatible(_x) \
(fdisk_is_disklabel(_x, DOS) && \
fdisk_dos_is_compatible(fdisk_context_get_label(_x, NULL)))
SSWAP16(sgiparam.pcylcount),
(int) sgiparam.sparecyl, SSWAP16(sgiparam.ilfact),
(char *)sgilabel,
- str_units(PLURAL), units_per_sector,
+ fdisk_context_get_unit(cxt, PLURAL),
+ fdisk_context_get_units_per_sector(cxt),
cxt->sector_size);
} else {
printf(_("\nDisk %s (SGI disk label): "
"%d heads, %llu sectors, %llu cylinders\n"
"Units = %s of %d * %ld bytes\n\n"),
cxt->dev_path, cxt->geom.heads, cxt->geom.sectors, cxt->geom.cylinders,
- str_units(PLURAL), units_per_sector,
+ fdisk_context_get_unit(cxt, PLURAL),
+ fdisk_context_get_units_per_sector(cxt),
cxt->sector_size);
}
printf(_("----- partitions -----\n"
/* device */ partname(cxt->dev_path, kpi, w+2),
/* flags */ (sgi_get_swappartition(cxt) == (int) i) ? "swap" :
/* flags */ (sgi_get_bootpartition(cxt) == (int) i) ? "boot" : " ",
-/* start */ (long) scround(start),
-/* end */ (long) scround(start+len)-1,
+/* start */ (long) scround(cxt, start),
+/* end */ (long) scround(cxt, start+len)-1,
/* no odd flag on end */ (long) len,
/* type id */ t->type,
/* type name */ t->name);
printf(_("You got a partition overlap on the disk. Fix it first!\n"));
return -EINVAL;
}
- snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
+ snprintf(mesg, sizeof(mesg), _("First %s"),
+ fdisk_context_get_unit(cxt, SINGULAR));
for (;;) {
if (sys == SGI_VOLUME) {
last = sgi_get_lastblock(cxt);
} else {
first = freelist[0].first;
last = freelist[0].last;
- first = read_int(cxt, scround(first), scround(first), scround(last)-1,
+ first = read_int(cxt, scround(cxt, first),
+ scround(cxt, first),
+ scround(cxt, last) - 1,
0, mesg);
}
- if (display_in_cyl_units)
- first *= units_per_sector;
+ if (fdisk_context_use_cylinders(cxt))
+ first *= fdisk_context_get_units_per_sector(cxt);
/*else
first = first; * align to cylinder if you know how ... */
if (!last)
} else
break;
}
- snprintf(mesg, sizeof(mesg), _(" Last %s"), str_units(SINGULAR));
- last = read_int(cxt, scround(first), scround(last)-1, scround(last)-1,
- scround(first), mesg)+1;
- if (display_in_cyl_units)
- last *= units_per_sector;
+ snprintf(mesg, sizeof(mesg), _(" Last %s"),
+ fdisk_context_get_unit(cxt, SINGULAR));
+ last = read_int(cxt, scround(cxt, first),
+ scround(cxt, last)-1,
+ scround(cxt, last)-1,
+ scround(cxt, first), mesg)+1;
+ if (fdisk_context_use_cylinders(cxt))
+ last *= fdisk_context_get_units_per_sector(cxt);
/*else
last = last; * align to cylinder if You know how ... */
if ((sys == SGI_VOLUME) && (first != 0 || last != sgi_get_lastblock(cxt)))
return -EINVAL;
}
}
- snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
+ snprintf(mesg, sizeof(mesg), _("First %s"),
+ fdisk_context_get_unit(cxt, SINGULAR));
for (;;) {
ask = fdisk_new_ask();
if (!ask)
fdisk_ask_number_set_default(ask, 0); /* default */
fdisk_ask_number_set_high(ask, 0); /* maximal */
} else {
- fdisk_ask_number_set_low(ask, scround(start)); /* minimal */
- fdisk_ask_number_set_default(ask, scround(start)); /* default */
- fdisk_ask_number_set_high(ask, scround(stop)); /* maximal */
+ fdisk_ask_number_set_low(ask, scround(cxt, start)); /* minimal */
+ fdisk_ask_number_set_default(ask, scround(cxt, start)); /* default */
+ fdisk_ask_number_set_high(ask, scround(cxt, stop)); /* maximal */
}
rc = fdisk_do_ask(cxt, ask);
first = fdisk_ask_number_get_result(ask);
if (rc)
return rc;
- if (display_in_cyl_units)
- first *= units_per_sector;
+ if (fdisk_context_use_cylinders(cxt))
+ first *= fdisk_context_get_units_per_sector(cxt);
else {
/* Starting sector has to be properly aligned */
int cs = cxt->geom.heads * cxt->geom.sectors;
}
snprintf(mesg, sizeof(mesg),
_("Last %s or +%s or +size{K,M,G,T,P}"),
- str_units(SINGULAR), str_units(PLURAL));
+ fdisk_context_get_unit(cxt, SINGULAR),
+ fdisk_context_get_unit(cxt, PLURAL));
ask = fdisk_new_ask();
if (!ask)
fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET);
if (whole_disk) {
- fdisk_ask_number_set_low(ask, scround(stop2)); /* minimal */
- fdisk_ask_number_set_default(ask, scround(stop2)); /* default */
- fdisk_ask_number_set_high(ask, scround(stop2)); /* maximal */
+ fdisk_ask_number_set_low(ask, scround(cxt, stop2)); /* minimal */
+ fdisk_ask_number_set_default(ask, scround(cxt, stop2)); /* default */
+ fdisk_ask_number_set_high(ask, scround(cxt, stop2)); /* maximal */
fdisk_ask_number_set_base(ask, 0);
} else if (n == 2 && !first) {
- fdisk_ask_number_set_low(ask, scround(first)); /* minimal */
- fdisk_ask_number_set_default(ask, scround(stop2)); /* default */
- fdisk_ask_number_set_high(ask, scround(stop2)); /* maximal */
- fdisk_ask_number_set_base(ask, scround(first));
+ fdisk_ask_number_set_low(ask, scround(cxt, first)); /* minimal */
+ fdisk_ask_number_set_default(ask, scround(cxt, stop2)); /* default */
+ fdisk_ask_number_set_high(ask, scround(cxt, stop2)); /* maximal */
+ fdisk_ask_number_set_base(ask, scround(cxt, first));
} else {
- fdisk_ask_number_set_low(ask, scround(first)); /* minimal */
- fdisk_ask_number_set_default(ask, scround(stop)); /* default */
- fdisk_ask_number_set_high(ask, scround(stop)); /* maximal */
- fdisk_ask_number_set_base(ask, scround(first));
+ fdisk_ask_number_set_low(ask, scround(cxt, first)); /* minimal */
+ fdisk_ask_number_set_default(ask, scround(cxt, stop)); /* default */
+ fdisk_ask_number_set_high(ask, scround(cxt, stop)); /* maximal */
+ fdisk_ask_number_set_base(ask, scround(cxt, first));
}
- if (display_in_cyl_units)
+ if (fdisk_context_use_cylinders(cxt))
fdisk_ask_number_set_unit(ask,
- cxt->sector_size * units_per_sector);
+ cxt->sector_size *
+ fdisk_context_get_units_per_sector(cxt));
else
fdisk_ask_number_set_unit(ask, cxt->sector_size);
_("You haven't covered the whole disk with the 3rd partition, but your value\n"
"%d %s covers some other partition. Your entry has been changed\n"
"to %d %s"),
- scround(last), str_units(SINGULAR),
- scround(stop), str_units(SINGULAR));
+ scround(cxt, last), fdisk_context_get_unit(cxt, SINGULAR),
+ scround(cxt, stop), fdisk_context_get_unit(cxt, SINGULAR));
last = stop;
}
} else if (!whole_disk && last > stop)
be16_to_cpu(sunlabel->intrlv),
sunlabel->label_id,
sunlabel->vtoc.volume_id,
- str_units(PLURAL), units_per_sector);
+ fdisk_context_get_unit(cxt, PLURAL),
+ fdisk_context_get_units_per_sector(cxt));
else
printf(
_("\nDisk %s (Sun disk label): %u heads, %llu sectors, %llu cylinders\n"
"Units = %s of %d * 512 bytes\n\n"),
cxt->dev_path, cxt->geom.heads, cxt->geom.sectors, cxt->geom.cylinders,
- str_units(PLURAL), units_per_sector);
+ fdisk_context_get_unit(cxt, PLURAL),
+ fdisk_context_get_units_per_sector(cxt));
printf(_("%*s Flag Start End Blocks Id System\n"),
w + 1, _("Device"));
/* device */ partname(cxt->dev_path, i+1, w),
/* flags */ be16_to_cpu(info->flags) & SUN_FLAG_UNMNT ? 'u' : ' ',
be16_to_cpu(info->flags) & SUN_FLAG_RONLY ? 'r' : ' ',
-/* start */ (unsigned long) scround(start),
-/* end */ (unsigned long) scround(start+len),
+/* start */ (unsigned long) scround(cxt, start),
+/* end */ (unsigned long) scround(cxt, start+len),
/* odd flag on end */ (unsigned long) len / 2, len & 1 ? '+' : ' ',
/* type id */ t->type,
/* type name */ t->name);
}
-static int sun_reset_alignment(struct fdisk_context *cxt)
+static int sun_reset_alignment(struct fdisk_context *cxt __attribute__((__unused__)))
{
- /* this is shared with DOS ... */
- update_units(cxt);
return 0;
}