diff mbox series

[1/1] diskpart: refactor libfdisk undef macros

Message ID 20210514072638.837-1-james.hilliard1@gmail.com
State New
Headers show
Series [1/1] diskpart: refactor libfdisk undef macros | expand

Commit Message

James Hilliard May 14, 2021, 7:26 a.m. UTC
This style matches the internal libfdisk macros and is somewhat cleaner.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 handlers/diskpart_handler.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
diff mbox series

Patch

diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index b10ffc6..22f199c 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -25,7 +25,8 @@  void diskpart_handler(void);
 /*
  * This is taken from libfdisk to declare if a field is not set
  */
-#define LIBFDISK_INIT_UNDEF(_x)	((__typeof__(_x)) -1)
+#define LIBFDISK_INIT_UNDEF(_x)	((_x) = (__typeof__(_x)) -1)
+#define LIBFDISK_IS_UNDEF(_x)	((_x) == (__typeof__(_x)) -1)
 
 /* Linux native partition type */
  #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
@@ -110,20 +111,20 @@  static int diskpart_set_partition(struct fdisk_partition *pa,
 	fdisk_partition_unset_partno(pa);
 	fdisk_partition_unset_size(pa);
 	fdisk_partition_unset_start(pa);
-	if (part->start != LIBFDISK_INIT_UNDEF(part->start))
-		ret = fdisk_partition_set_start(pa, part->start);
-	else
+	if (LIBFDISK_IS_UNDEF(part->start))
 		ret = fdisk_partition_start_follow_default(pa, 1);
-	if (part->partno != LIBFDISK_INIT_UNDEF(part->partno))
-		ret |= fdisk_partition_set_partno(pa, part->partno);
 	else
+		ret = fdisk_partition_set_start(pa, part->start);
+	if (LIBFDISK_IS_UNDEF(part->partno))
 		ret |= fdisk_partition_partno_follow_default(pa, 1);
+	else
+		ret |= fdisk_partition_set_partno(pa, part->partno);
 	if (strlen(part->name))
 	      ret |= fdisk_partition_set_name(pa, part->name);
-	if (part->size != LIBFDISK_INIT_UNDEF(part->size))
-	      ret |= fdisk_partition_set_size(pa, part->size / sector_size);
-	else
+	if (LIBFDISK_IS_UNDEF(part->size))
 		ret |= fdisk_partition_end_follow_default(pa, 1);
+	else
+		ret |= fdisk_partition_set_size(pa, part->size / sector_size);
 
 	if (parttype)
 		ret |= fdisk_partition_set_type(pa, parttype);
@@ -228,9 +229,9 @@  static int diskpart(struct img_type *img,
 		}
 		elem = LIST_FIRST(parts);
 
-		part->partno = LIBFDISK_INIT_UNDEF(part->partno);
-		part->start = LIBFDISK_INIT_UNDEF(part->start);
-		part->size = LIBFDISK_INIT_UNDEF(part->size);
+		LIBFDISK_INIT_UNDEF(part->partno);
+		LIBFDISK_INIT_UNDEF(part->start);
+		LIBFDISK_INIT_UNDEF(part->size);
 
 		part->partno = strtoul(entry->key  + strlen("partition-"), NULL, 10);
 		while (elem) {
@@ -264,10 +265,10 @@  static int diskpart(struct img_type *img,
 		}
 
 		TRACE("partition-%zu:%s size %" PRIu64 " start %zu type %s",
-			part->partno != LIBFDISK_INIT_UNDEF(part->partno) ? part->partno : 0,
+			!LIBFDISK_IS_UNDEF(part->partno) ? part->partno : 0,
 			strlen(part->name) ? part->name : "UNDEF NAME",
-			part->size != LIBFDISK_INIT_UNDEF(part->size) ? part->size : 0,
-			part->start!= LIBFDISK_INIT_UNDEF(part->start) ? part->start : 0,
+			!LIBFDISK_IS_UNDEF(part->size) ? part->size : 0,
+			!LIBFDISK_IS_UNDEF(part->start) ? part->start : 0,
 			part->type);
 
 		/*