From 0285d67ae37bed601c127490b3d03625b0791c4a Mon Sep 17 00:00:00 2001 From: Yidi Lin Date: Thu, 2 Nov 2023 12:29:04 +0800 Subject: [PATCH] libpayload/libc/getopt_long: Use common GCD TEST=emerge-geralt libpayload Change-Id: Ib9dd1d2f658d4411c36d0198774819690686a393 Signed-off-by: Yidi Lin Reviewed-on: https://review.coreboot.org/c/coreboot/+/78887 Reviewed-by: Yu-Ping Wu Reviewed-by: Julius Werner Tested-by: build bot (Jenkins) --- payloads/libpayload/libc/Makefile.inc | 1 + payloads/libpayload/libc/getopt_long.c | 22 ++-------------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/payloads/libpayload/libc/Makefile.inc b/payloads/libpayload/libc/Makefile.inc index bc706ae5a7..2d277da3b5 100644 --- a/payloads/libpayload/libc/Makefile.inc +++ b/payloads/libpayload/libc/Makefile.inc @@ -46,4 +46,5 @@ endif ifeq ($(CONFIG_LP_LIBC),y) libc-srcs += $(coreboottop)/src/commonlib/bsd/elog.c +libc-srcs += $(coreboottop)/src/commonlib/bsd/gcd.c endif diff --git a/payloads/libpayload/libc/getopt_long.c b/payloads/libpayload/libc/getopt_long.c index 365bc4a85a..822ce96904 100644 --- a/payloads/libpayload/libc/getopt_long.c +++ b/payloads/libpayload/libc/getopt_long.c @@ -54,6 +54,7 @@ #include #include */ +#include #include #include #define warnx(x...) printf(x) @@ -88,7 +89,6 @@ static int getopt_internal(int, char * const *, const char *, const struct option *, int *, int); static int parse_long_options(char * const *, const char *, const struct option *, int *, int); -static int gcd(int, int); static void permute_args(int, int, int, char * const *); static char *place = EMSG; /* option letter processing */ @@ -105,24 +105,6 @@ static const char noarg[] = "option doesn't take an argument -- %.*s"; static const char illoptchar[] = "unknown option -- %c"; static const char illoptstring[] = "unknown option -- %s"; -/* - * Compute the greatest common divisor of a and b. - */ -static int -gcd(int a, int b) -{ - int c; - - c = a % b; - while (c != 0) { - a = b; - b = c; - c = a % b; - } - - return (b); -} - /* * Exchange the block from nonopt_start to nonopt_end with the block * from nonopt_end to opt_end (keeping the same order of arguments @@ -140,7 +122,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end, */ nnonopts = panonopt_end - panonopt_start; nopts = opt_end - panonopt_end; - ncycle = gcd(nnonopts, nopts); + ncycle = gcd32(nnonopts, nopts); cyclelen = (opt_end - panonopt_start) / ncycle; for (i = 0; i < ncycle; i++) {