From d464e2f0f9fc4c8688eb4b9a020c0e02fe71b4cc Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 5 Sep 2014 10:35:29 +0200 Subject: fdisk: refactor /proc/partitions usage Signed-off-by: Karel Zak --- disk-utils/fdisk-list.c | 79 ++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 33 deletions(-) (limited to 'disk-utils/fdisk-list.c') diff --git a/disk-utils/fdisk-list.c b/disk-utils/fdisk-list.c index e3bd113a9..3d26f8b7a 100644 --- a/disk-utils/fdisk-list.c +++ b/disk-utils/fdisk-list.c @@ -177,36 +177,19 @@ done: fdisk_free_iter(itr); } -int print_device_pt(struct fdisk_context *cxt, char *device, int warnme) +char *next_proc_partition(FILE **f) { - if (fdisk_assign_device(cxt, device, 1) != 0) { /* read-only */ - if (warnme || errno == EACCES) - warn(_("cannot open %s"), device); - return -1; - } - - list_disk_geometry(cxt); - - if (fdisk_has_label(cxt)) - list_disklabel(cxt); - - fdisk_deassign_device(cxt, 1); - return 0; -} - -void print_all_devices_pt(struct fdisk_context *cxt) -{ - FILE *f; char line[128 + 1]; - int ct = 0; - f = fopen(_PATH_PROC_PARTITIONS, "r"); - if (!f) { - warn(_("cannot open %s"), _PATH_PROC_PARTITIONS); - return; + if (!*f) { + *f = fopen(_PATH_PROC_PARTITIONS, "r"); + if (!*f) { + warn(_("cannot open %s"), _PATH_PROC_PARTITIONS); + return NULL; + } } - while (fgets(line, sizeof(line), f)) { + while (fgets(line, sizeof(line), *f)) { char buf[PATH_MAX], *cn; dev_t devno; @@ -228,14 +211,44 @@ void print_all_devices_pt(struct fdisk_context *cxt) if (!cn) continue; - if (!is_ide_cdrom_or_tape(cn)) { - if (ct) - fputs("\n\n", stdout); - if (print_device_pt(cxt, cn, 0) == 0) - ct++; - } - free(cn); + if (!is_ide_cdrom_or_tape(cn)) + return cn; + } + fclose(*f); + *f = NULL; + + return NULL; +} + +int print_device_pt(struct fdisk_context *cxt, char *device, int warnme) +{ + if (fdisk_assign_device(cxt, device, 1) != 0) { /* read-only */ + if (warnme || errno == EACCES) + warn(_("cannot open %s"), device); + return -1; + } + + list_disk_geometry(cxt); + + if (fdisk_has_label(cxt)) + list_disklabel(cxt); + + fdisk_deassign_device(cxt, 1); + return 0; +} + +void print_all_devices_pt(struct fdisk_context *cxt) +{ + FILE *f = NULL; + int ct = 0; + char *dev; + + while ((dev = next_proc_partition(&f))) { + if (ct) + fputs("\n\n", stdout); + if (print_device_pt(cxt, dev, 0) == 0) + ct++; + free(dev); } - fclose(f); } -- cgit v1.2.3-55-g7522