diff mbox series

[01/13] kconfig: split randconfig setup code into set_randconfig_seed()

Message ID 20210313194836.372585-1-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series [01/13] kconfig: split randconfig setup code into set_randconfig_seed() | expand

Commit Message

Masahiro Yamada March 13, 2021, 7:48 p.m. UTC
This code is too big to be placed in the switch statement.

Move the code into a new helper function. I slightly refactor the code
without changing the behavior.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/kconfig/conf.c | 54 ++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 23 deletions(-)

Comments

Masahiro Yamada March 25, 2021, 4:47 a.m. UTC | #1
On Sun, Mar 14, 2021 at 4:48 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> This code is too big to be placed in the switch statement.
>
> Move the code into a new helper function. I slightly refactor the code
> without changing the behavior.
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---

All applied to linux-kbuild/kconfig.




>  scripts/kconfig/conf.c | 54 ++++++++++++++++++++++++------------------
>  1 file changed, 31 insertions(+), 23 deletions(-)
>
> diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
> index 957d2a0832f7..063c9e7a34c1 100644
> --- a/scripts/kconfig/conf.c
> +++ b/scripts/kconfig/conf.c
> @@ -82,6 +82,36 @@ static void xfgets(char *str, int size, FILE *in)
>                 printf("%s", str);
>  }
>
> +static void set_randconfig_seed(void)
> +{
> +       unsigned int seed;
> +       char *env;
> +       bool seed_set = false;
> +
> +       env = getenv("KCONFIG_SEED");
> +       if (env && *env) {
> +               char *endp;
> +
> +               seed = strtol(env, &endp, 0);
> +               if (*endp == '\0')
> +                       seed_set = true;
> +       }
> +
> +       if (!seed_set) {
> +               struct timeval now;
> +
> +               /*
> +                * Use microseconds derived seed, compensate for systems where it may
> +                * be zero.
> +                */
> +               gettimeofday(&now, NULL);
> +               seed = (now.tv_sec + 1) * (now.tv_usec + 1);
> +       }
> +
> +       printf("KCONFIG_SEED=0x%X\n", seed);
> +       srand(seed);
> +}
> +
>  static int conf_askvalue(struct symbol *sym, const char *def)
>  {
>         if (!sym_has_value(sym))
> @@ -515,30 +545,8 @@ int main(int ac, char **av)
>                         defconfig_file = optarg;
>                         break;
>                 case randconfig:
> -               {
> -                       struct timeval now;
> -                       unsigned int seed;
> -                       char *seed_env;
> -
> -                       /*
> -                        * Use microseconds derived seed,
> -                        * compensate for systems where it may be zero
> -                        */
> -                       gettimeofday(&now, NULL);
> -                       seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
> -
> -                       seed_env = getenv("KCONFIG_SEED");
> -                       if( seed_env && *seed_env ) {
> -                               char *endp;
> -                               int tmp = (int)strtol(seed_env, &endp, 0);
> -                               if (*endp == '\0') {
> -                                       seed = tmp;
> -                               }
> -                       }
> -                       fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
> -                       srand(seed);
> +                       set_randconfig_seed();
>                         break;
> -               }
>                 case oldaskconfig:
>                 case oldconfig:
>                 case allnoconfig:
> --
> 2.27.0
>
diff mbox series

Patch

diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
index 957d2a0832f7..063c9e7a34c1 100644
--- a/scripts/kconfig/conf.c
+++ b/scripts/kconfig/conf.c
@@ -82,6 +82,36 @@  static void xfgets(char *str, int size, FILE *in)
 		printf("%s", str);
 }
 
+static void set_randconfig_seed(void)
+{
+	unsigned int seed;
+	char *env;
+	bool seed_set = false;
+
+	env = getenv("KCONFIG_SEED");
+	if (env && *env) {
+		char *endp;
+
+		seed = strtol(env, &endp, 0);
+		if (*endp == '\0')
+			seed_set = true;
+	}
+
+	if (!seed_set) {
+		struct timeval now;
+
+		/*
+		 * Use microseconds derived seed, compensate for systems where it may
+		 * be zero.
+		 */
+		gettimeofday(&now, NULL);
+		seed = (now.tv_sec + 1) * (now.tv_usec + 1);
+	}
+
+	printf("KCONFIG_SEED=0x%X\n", seed);
+	srand(seed);
+}
+
 static int conf_askvalue(struct symbol *sym, const char *def)
 {
 	if (!sym_has_value(sym))
@@ -515,30 +545,8 @@  int main(int ac, char **av)
 			defconfig_file = optarg;
 			break;
 		case randconfig:
-		{
-			struct timeval now;
-			unsigned int seed;
-			char *seed_env;
-
-			/*
-			 * Use microseconds derived seed,
-			 * compensate for systems where it may be zero
-			 */
-			gettimeofday(&now, NULL);
-			seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
-
-			seed_env = getenv("KCONFIG_SEED");
-			if( seed_env && *seed_env ) {
-				char *endp;
-				int tmp = (int)strtol(seed_env, &endp, 0);
-				if (*endp == '\0') {
-					seed = tmp;
-				}
-			}
-			fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
-			srand(seed);
+			set_randconfig_seed();
 			break;
-		}
 		case oldaskconfig:
 		case oldconfig:
 		case allnoconfig: