summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/utils.c
diff options
context:
space:
mode:
authorKarel Zak2013-05-02 13:39:01 +0200
committerKarel Zak2013-09-16 16:46:54 +0200
commitbb8a40366a425c785bd5ad2a695891fee5f9ec7e (patch)
tree45edfd9d5c9c6fec2dae98c430ac397eb74570e4 /libfdisk/src/utils.c
parentfdisk: (sun) use tt.c to list disk label (diff)
downloadkernel-qcow2-util-linux-bb8a40366a425c785bd5ad2a695891fee5f9ec7e.tar.gz
kernel-qcow2-util-linux-bb8a40366a425c785bd5ad2a695891fee5f9ec7e.tar.xz
kernel-qcow2-util-linux-bb8a40366a425c785bd5ad2a695891fee5f9ec7e.zip
libfdisk: add fdisk_partname()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/utils.c')
-rw-r--r--libfdisk/src/utils.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/libfdisk/src/utils.c b/libfdisk/src/utils.c
index 1360e8467..9195b2b19 100644
--- a/libfdisk/src/utils.c
+++ b/libfdisk/src/utils.c
@@ -1,5 +1,9 @@
#include "fdiskP.h"
+#include "pathnames.h"
+
+#include <ctype.h>
+
/*
* Zeros in-memory first sector buffer
@@ -41,3 +45,77 @@ int fdisk_read_firstsector(struct fdisk_context *cxt)
return 0;
}
+
+
+/*
+ * Return allocated buffer with partition name
+ */
+char *fdisk_partname(const char *dev, size_t partno)
+{
+ char *res = NULL;
+ const char *p = "";
+ int w = 0;
+
+ if (!dev || !*dev) {
+ if (asprintf(&res, "%zd", partno) > 0)
+ return res;
+ return NULL;
+ }
+
+ w = strlen(dev);
+ if (isdigit(dev[w - 1]))
+ p = "p";
+
+ /* devfs kludge - note: fdisk partition names are not supposed
+ to equal kernel names, so there is no reason to do this */
+ if (strcmp(dev + w - 4, "disc") == 0) {
+ w -= 4;
+ p = "part";
+ }
+
+ /* udev names partitions by appending -partN
+ e.g. ata-SAMSUNG_SV8004H_0357J1FT712448-part1 */
+ if ((strncmp(dev, _PATH_DEV_BYID, sizeof(_PATH_DEV_BYID) - 1) == 0) ||
+ strncmp(dev, _PATH_DEV_BYPATH, sizeof(_PATH_DEV_BYPATH) - 1) == 0) {
+ p = "-part";
+ }
+
+ if (asprintf(&res, "%.*s%s%zu", w, dev, p, partno) > 0)
+ return res;
+
+ return NULL;
+}
+
+#ifdef TEST_PROGRAM
+struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_mac_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_sun_label(struct fdisk_context *cxt) { return NULL; }
+
+int test_partnames(struct fdisk_test *ts, int argc, char *argv[])
+{
+ size_t i;
+ const char *disk = argv[1];
+
+ for (i = 0; i < 5; i++) {
+ char *p = fdisk_partname(disk, i + 1);
+ if (p)
+ printf("%zu: '%s'\n", i + 1, p);
+ free(p);
+ }
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct fdisk_test tss[] = {
+ { "--partnames", test_partnames, "<diskname>" },
+ { NULL }
+ };
+
+ return fdisk_run_test(tss, argc, argv);
+}
+
+#endif