[LTP] [PATCH v4 3/5] shell: add kconfig parse api

xuyang2018.jy@fujitsu.com xuyang2018.jy@fujitsu.com
Tue Jan 11 09:37:54 CET 2022


Hi Li
> On Tue, Jan 11, 2022 at 2:10 PM Yang Xu<xuyang2018.jy@fujitsu.com>  wrote:
>
>> +++ b/testcases/lib/tst_check_kconfigs.c
>> @@ -0,0 +1,46 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/* Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.*/
>> +
>> +#include<stdio.h>
>> +#include<string.h>
>> +#include<stdlib.h>
>> +#include "tst_kconfig.h"
>> +
>> +int main(int argc, char *argv[])
>> +{
>> +       char *str = argv[1];
>> +       char *delim = argv[2];
>> +       unsigned int i, cnt = 1;
>> +       int ret = 0;
>> +
>> +       if (argc == 2 || strlen(delim) == 0) {
>
> I doubt that this syntax really works here.
>
> How can we get the strlen(delim) equal to 0? if the argc is
> not 2, why the length of delimi is zero? but if we change ||
> to&&, then get a segment fault. I don't understand this.

Sorry, this is ugly code. please see the lastest code.
>
>> +               delim = ",";
>> +       } else if (argc == 3) {
>> +               if (strlen(delim)>  1) {
>> +                       fprintf(stderr, "The delim must be a single character\n");
>> +                       return 1;
>> +               }
>> +       } else {
>> +               fprintf(stderr, "Please provide kernel kconfig list and delim "
>> +                               "(optinal, default value is ',')\n");
>> +               return 1;
>> +       }
>> +
>> +       for (i = 0; str[i]; i++) {
>> +               if (str[i] == delim[0])
>> +                       cnt++;
>> +       }
>> +
>> +       char **kconfigs = malloc(++i * sizeof(char *));
>
> Shouldn't this be malloc(++cnt * sizeof(char*)) ?
Oh, yes. Sorry for this typo.
>
>> +
>> +       for (i = j0; i<  cnt; i++)
>> +               kconfigs[i] = strtok_r(str, delim,&str);
>> +
>> +       kconfigs[i] = NULL;
This is also useless.

The lastest code should be as below:

int main(int argc, char *argv[])
{
         char *str = argv[1];
         char *delim = argv[2];
         unsigned int i, cnt = 1;
         int ret = 0;

         if (argc == 2) {
                 delim = ",";
         } else if (argc == 3) {
                 if (strlen(delim) > 1) {
                         fprintf(stderr, "The delim must be a single 
character\n");
                         return 1;
                 }
         } else {
                 fprintf(stderr, "Please provide kernel kconfig list and 
delim "
                                 "(optinal, default value is ',')\n");
                 return 1;
         }

         for (i = 0; str[i]; i++) {
                 if (str[i] == delim[0])
                         cnt++;
         }

         char **kconfigs = malloc(++cnt * sizeof(char *));

         for (i = 0; i < cnt; i++)
                 kconfigs[i] = strtok_r(str, delim, &str);

         if (tst_kconfig_check((const char * const*)kconfigs))
                 ret = 1;

         free(kconfigs);
         return ret;
}


Best Regards
Yang Xu
>> +
>> +       if (tst_kconfig_check((const char * const*)kconfigs))
>> +               ret = 1;
>> +
>> +       free(kconfigs);
>> +       return ret;
>> +}
>


More information about the ltp mailing list