diff options
author | Karel Zak | 2014-09-05 10:35:29 +0200 |
---|---|---|
committer | Karel Zak | 2014-10-07 14:55:31 +0200 |
commit | d464e2f0f9fc4c8688eb4b9a020c0e02fe71b4cc (patch) | |
tree | c16a5fa60139756e6eda5d1f162647f20fd5acb0 /disk-utils/fdisk-list.c | |
parent | sfdisk: update usage() (diff) | |
download | kernel-qcow2-util-linux-d464e2f0f9fc4c8688eb4b9a020c0e02fe71b4cc.tar.gz kernel-qcow2-util-linux-d464e2f0f9fc4c8688eb4b9a020c0e02fe71b4cc.tar.xz kernel-qcow2-util-linux-d464e2f0f9fc4c8688eb4b9a020c0e02fe71b4cc.zip |
fdisk: refactor /proc/partitions usage
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils/fdisk-list.c')
-rw-r--r-- | disk-utils/fdisk-list.c | 79 |
1 files changed, 46 insertions, 33 deletions
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); } |