From cb7ce873a5c01fdd14df2ffd8e56103ecc8dd557 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 11 Feb 2013 14:48:18 +0100 Subject: [PATCH] libfdisk: add info about display units to context Signed-off-by: Karel Zak --- libfdisk/src/context.c | 55 +++++++++++++++++++++++++++++++++++++++++ libfdisk/src/fdiskP.h | 2 ++ libfdisk/src/libfdisk.h | 5 ++++ 3 files changed, 62 insertions(+) diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index 4a2faf97d..352f03ac3 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -201,3 +201,58 @@ int fdisk_context_set_ask(struct fdisk_context *cxt, cxt->ask_data = data; return 0; } + + +/* + * @str: "cylinder" or "sector". + * + * This is pure shit, unfortunately for example Sun addresses begin of the + * partition by cylinders... + */ +int fdisk_context_set_unit(struct fdisk_context *cxt, const char *str) +{ + assert(cxt); + + cxt->display_in_cyl_units = 0; + + if (!str) + return 0; + + if (strcmp(str, "cylinder") == 0 || strcmp(str, "cylinders") == 0) + cxt->display_in_cyl_units = 1; + + else if (strcmp(str, "sector") == 0 || strcmp(str, "sectors") == 0) + cxt->display_in_cyl_units = 0; + + DBG(CONTEXT, dbgprint("display unit: %s", fdisk_context_get_unit(cxt, 0))); + return 0; +} + +const char *fdisk_context_get_unit(struct fdisk_context *cxt, int n) +{ + assert(cxt); + + if (fdisk_context_use_cylinders(cxt)) + return P_("cylinder", "cylinders", n); + return P_("sector", "sectors", n); +} + +/* Returns 1 if user wants to display in cylinders. */ +int fdisk_context_use_cylinders(struct fdisk_context *cxt) +{ + assert(cxt); + return cxt->display_in_cyl_units == 1; +} + +/* Returns number of "units" per sector, default is 1 if display unit is sector. + */ +unsigned int fdisk_context_get_units_per_sector(struct fdisk_context *cxt) +{ + assert(cxt); + + if (fdisk_context_use_cylinders(cxt)) { + assert(cxt->geom.heads); + return cxt->geom.heads * cxt->geom.sectors; + } + return 1; +} diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index ab4174086..4c66a5a33 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -254,6 +254,8 @@ struct fdisk_context { unsigned long sector_size; /* logical size */ unsigned long alignment_offset; + int display_in_cyl_units; /* for obscure labels */ + /* alignment */ unsigned long grain; /* alignment unit */ sector_t first_lba; /* recommended begin of the first partition */ diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index f34d487d1..f60b3eb55 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -82,6 +82,11 @@ extern struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt, extern int fdisk_context_switch_label(struct fdisk_context *cxt, const char *name); +extern int fdisk_context_set_unit(struct fdisk_context *cxt, const char *str); +extern const char *fdisk_context_get_unit(struct fdisk_context *cxt, int n); +extern int fdisk_context_use_cylinders(struct fdisk_context *cxt); +extern unsigned int fdisk_context_get_units_per_sector(struct fdisk_context *cxt); + /* parttype.c */ extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt, unsigned int code);