diff options
author | Karel Zak | 2013-05-02 13:39:01 +0200 |
---|---|---|
committer | Karel Zak | 2013-09-16 16:46:54 +0200 |
commit | bb8a40366a425c785bd5ad2a695891fee5f9ec7e (patch) | |
tree | 45edfd9d5c9c6fec2dae98c430ac397eb74570e4 | |
parent | fdisk: (sun) use tt.c to list disk label (diff) | |
download | kernel-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>
-rw-r--r-- | libfdisk/src/Makemodule.am | 8 | ||||
-rw-r--r-- | libfdisk/src/fdiskP.h | 1 | ||||
-rw-r--r-- | libfdisk/src/utils.c | 78 |
3 files changed, 86 insertions, 1 deletions
diff --git a/libfdisk/src/Makemodule.am b/libfdisk/src/Makemodule.am index fbfb1b4f2..3858e7b09 100644 --- a/libfdisk/src/Makemodule.am +++ b/libfdisk/src/Makemodule.am @@ -42,7 +42,8 @@ libfdisk_la_DEPENDENCIES = $(libfdisk_la_LIBADD) check_PROGRAMS += \ - test_fdisk_ask + test_fdisk_ask \ + test_fdisk_utils libfdisk_tests_cflags = -DTEST_PROGRAM $(libfdisk_la_CFLAGS) libfdisk_tests_ldflags = -static @@ -60,3 +61,8 @@ test_fdisk_ask_SOURCES = libfdisk/src/ask.c test_fdisk_ask_CFLAGS = $(libfdisk_tests_cflags) test_fdisk_ask_LDFLAGS = $(libfdisk_tests_ldflags) test_fdisk_ask_LDADD = $(libfdisk_tests_ldadd) + +test_fdisk_utils_SOURCES = libfdisk/src/utils.c +test_fdisk_utils_CFLAGS = $(libfdisk_tests_cflags) +test_fdisk_utils_LDFLAGS = $(libfdisk_tests_ldflags) +test_fdisk_utils_LDADD = $(libfdisk_tests_ldadd) diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index ff4c31bd2..ac490be0f 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -318,6 +318,7 @@ extern int fdisk_discover_topology(struct fdisk_context *cxt); /* utils.c */ extern void fdisk_zeroize_firstsector(struct fdisk_context *cxt); extern int fdisk_read_firstsector(struct fdisk_context *cxt); +extern char *fdisk_partname(const char *dev, size_t partno); /* label.c */ extern int fdisk_probe_labels(struct fdisk_context *cxt); 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 |