aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <dbphillipsnz@gmail.com>2016-04-04 18:26:40 +1200
committerDavid Phillips <dbphillipsnz@gmail.com>2016-04-04 18:26:40 +1200
commit88fc89479b339b01bee77c0467cefe05017f4909 (patch)
treec0fff8049fb1bd9a8b13d4b592b8a20567aae2ae
parentfaebfd17d5eaff9f7a3007ea808ec060c69b8ae9 (diff)
downloadfractal-gen-88fc89479b339b01bee77c0467cefe05017f4909.tar.xz
Use getopt(3) to parse arguments instead
-rw-r--r--fractal-gen.c66
-rw-r--r--fractal-gen.h4
2 files changed, 44 insertions, 26 deletions
diff --git a/fractal-gen.c b/fractal-gen.c
index 25b70c1..56163ea 100644
--- a/fractal-gen.c
+++ b/fractal-gen.c
@@ -50,7 +50,7 @@ static struct section_generator generators[] = {
/* FIXME put into header */
double thread_mult = 0.f; /* number to multiply available cores by to get thread count */
-
+char *argv0 = NULL;
int main(int argc, char **argv)
{
@@ -63,6 +63,9 @@ int main(int argc, char **argv)
data_section* sections = NULL;
generator_func generator = NULL;
+ /* who are we? */
+ argv0 = argv[0];
+
/* Select correct generator for the fractal type */
bname = basename(argv[0]);
generator = select_generator(bname);
@@ -73,8 +76,11 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
- if (!args_parse_okay(argc, (const char**)argv))
+ if (!args_parse_okay(argc, argv))
+ {
+ show_help();
return EXIT_FAILURE;
+ }
/* Allocate memory for sections */
if ((sections = malloc(sizeof(data_section)*cores)) == NULL)
@@ -162,31 +168,35 @@ int main(int argc, char **argv)
}
-/* FIXME use getopt */
-bool args_parse_okay(const int argc, const char **argv)
+bool args_parse_okay(int argc, char **argv)
{
- if (argc < 4 || argc > 7)
- {
- fprintf(stderr,
- "%s size iterat power [threads]\n"
- "%s size iterat power thread_multiplier cluster-id cluster-total\n",
- argv[0], argv[0]);
- return false;
- }
+ char opt = '\0';
- size = atoi(argv[1]);
- iterat = atoi(argv[2]);
- power = atof(argv[3]);
+ /* first things first: preload default values */
+ cores = sysconf(_SC_NPROCESSORS_ONLN);
+ thread_mult = 1;
+ clust_id = 0;
+ clust_total = 1;
- /* Fetch or use defaults for
- * - num cores available
- * - our ID in cluster
- * - total members in cluster
- * FIXME this stuff is horrible, will disappear with getopt */
- cores = (argc == 5)? atoi(argv[4]) : sysconf(_SC_NPROCESSORS_ONLN);
- thread_mult = argc == 7? atof(argv[4]) : 1.f;
- clust_id = argc == 7? atoi(argv[5]) : 0;
- clust_total = argc == 7? atoi(argv[6]) : 1;
+ while ( (opt = getopt(argc, argv, "s:i:e:c:t:N:T")) != -1 )
+ {
+ switch (opt)
+ {
+ case 's': size = atoi(optarg); break;
+ case 'i': iterat = atoi(optarg); break;
+ case 'e': power = atoi(optarg); break;
+ case 'c': cores = atoi(optarg); break;
+ case 't': thread_mult = atoi(optarg); break;
+ case 'N': clust_id = atoi(optarg); break;
+ case 'T': clust_total = atoi(optarg); break;
+ /* redundant case for '?', but explicitness is best */
+ case '?':
+ default:
+ show_help();
+ return false;
+ break;
+ }
+ }
/* Extend number of threads to multiplier value */
cores *= thread_mult;
@@ -234,3 +244,11 @@ generator_func select_generator(const char* name)
return NULL;
}
+
+void show_help()
+{
+ fprintf(stderr,
+ "%s size iterat power [threads]\n"
+ "%s size iterat power thread_multiplier cluster-id cluster-total\n",
+ argv0, argv0);
+}
diff --git a/fractal-gen.h b/fractal-gen.h
index e0b69a8..a25063f 100644
--- a/fractal-gen.h
+++ b/fractal-gen.h
@@ -50,9 +50,9 @@ double power;
typedef void* (*generator_func)(void *);
-bool args_parse_okay(const int argc, const char **argv);
+bool args_parse_okay(int argc, char **argv);
generator_func select_generator(const char* name);
-
+void show_help();
#include "algorithms.h"