aboutsummaryrefslogtreecommitdiff
path: root/helper/threads.c
diff options
context:
space:
mode:
authorPetri Savolainen <petri.savolainen@linaro.org>2018-05-14 13:10:01 +0300
committerMaxim Uvarov <maxim.uvarov@linaro.org>2018-05-14 18:47:02 +0300
commit0ee8255c2555ed68721ea5e7679f26a2e53bd8b8 (patch)
treee3e4b5b2c8836c6a4e07b69f6146e86bf2b67c17 /helper/threads.c
parentad3417a6fd2ba93616b937f9a2d8642eceeb0bf8 (diff)
helper: thread: don't use getopt library
Don't use getopt library calls for helper options. Getopt library reset is not portable in practice. Library interface includes global variables and different internal state variables depending on POSIX version. There's no need for helper to use getopt calls or set getopt global variables at all. It's much more simple (and portable) to remove helper options from argv[] and return new value for argc. Signed-off-by: Petri Savolainen <petri.savolainen@linaro.org> Reviewed-by: Dmitry Eremin-Solenikov <dmitry.ereminsolenikov@linaro.org> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
Diffstat (limited to 'helper/threads.c')
-rw-r--r--helper/threads.c131
1 files changed, 14 insertions, 117 deletions
diff --git a/helper/threads.c b/helper/threads.c
index f9216c7ed..86d6bf7be 100644
--- a/helper/threads.c
+++ b/helper/threads.c
@@ -330,129 +330,26 @@ int odph_odpthread_getaffinity(void)
return -1;
}
-/*
- * return the number of elements in an array of getopt options, excluding the
- * terminating {0,0,0,0}
- */
-static int get_getopt_options_length(const struct option *longopts)
+int odph_parse_options(int argc, char *argv[])
{
- int l = 0;
-
- if (!longopts)
- return 0;
-
- while (longopts[l].name)
- l++;
-
- return l;
-}
-
-/* Merge getopt options */
-int odph_merge_getopt_options(const char *shortopts1,
- const char *shortopts2,
- const struct option *longopts1,
- const struct option *longopts2,
- char **shortopts,
- struct option **longopts)
-{
- int shortopts1_len;
- int shortopts2_len;
- int longopts1_len;
- int longopts2_len;
- int index;
- int res_index = 0;
- struct option termination = {0, 0, 0, 0};
-
- /* merge short options: */
- if (shortopts) {
- shortopts1_len = (shortopts1) ? strlen(shortopts1) : 0;
- shortopts2_len = (shortopts2) ? strlen(shortopts2) : 0;
- *shortopts = malloc(shortopts1_len + shortopts2_len + 1);
- if (!*shortopts)
- return -1;
-
- (*shortopts)[0] = 0;
-
- if (shortopts1)
- strcpy((*shortopts), shortopts1);
- if (shortopts2)
- strcat((*shortopts), shortopts2);
- }
-
- /* merge long options */
- if (!longopts)
- return 0;
-
- longopts1_len = get_getopt_options_length(longopts1);
- longopts2_len = get_getopt_options_length(longopts2);
- *longopts = malloc(sizeof(struct option) *
- (longopts1_len + longopts2_len + 1));
- if (!*longopts) {
- if (shortopts)
- free(*shortopts);
- return -1;
- }
+ int i, j;
- for (index = 0; (longopts1) && (longopts1[index].name); index++)
- (*longopts)[res_index++] = longopts1[index];
+ helper_options.proc = 0;
- for (index = 0; (longopts2) && (longopts2[index].name); index++)
- (*longopts)[res_index++] = longopts2[index];
+ /* Find and remove option */
+ for (i = 0; i < argc;) {
+ if (strcmp(argv[i], "--odph_proc") == 0) {
+ helper_options.proc = 1;
- (*longopts)[res_index] = termination;
+ for (j = i; j < argc - 1; j++)
+ argv[j] = argv[j + 1];
- return 0;
-}
-
-/*
- * Parse command line options to extract options affecting helpers.
- */
-int odph_parse_options(int argc, char *argv[],
- const char *caller_shortopts,
- const struct option *caller_longopts)
-{
- int c;
- char *shortopts;
- struct option *longopts;
- int res = 0;
-
- static struct option helper_long_options[] = {
- /* These options set a flag. */
- {"odph_proc", no_argument, &helper_options.proc, 1},
- {0, 0, 0, 0}
- };
-
- static const char *helper_short_options = "";
-
- /* defaults: */
- helper_options.proc = false;
-
- /* merge caller's command line options descriptions with helper's: */
- if (odph_merge_getopt_options(caller_shortopts, helper_short_options,
- caller_longopts, helper_long_options,
- &shortopts, &longopts) < 0)
- return -1;
-
- while (1) {
- /* getopt_long stores the option index here. */
- int option_index = 0;
-
- c = getopt_long (argc, argv,
- shortopts, longopts, &option_index);
-
- /* Detect the end of the options. */
- if (c == -1)
- break;
+ argc--;
+ continue;
+ }
- /* check for unknown options or missing arguments */
- if (c == '?' || c == ':')
- res = -1;
+ i++;
}
- optind = 0; /* caller expects this to be zero if it parses too*/
-
- free(shortopts);
- free(longopts);
-
- return res;
+ return argc;
}