summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2022-11-26 17:37:18 +0200
committergramanas <anastasis.gramm2@gmail.com>2022-11-26 17:37:18 +0200
commit8ef08604932c05ebfabb041704dc6f994d7a93a7 (patch)
treecfdb23f88e854f54674eb57fa2ef14fac32a0b54
parent5aa48ee3bd2797d0f39ffc677d97384dbe0f59fb (diff)
downloadfoodtools-8ef08604932c05ebfabb041704dc6f994d7a93a7.tar.gz
foodtools-8ef08604932c05ebfabb041704dc6f994d7a93a7.tar.bz2
foodtools-8ef08604932c05ebfabb041704dc6f994d7a93a7.zip
Fix longopts
-rw-r--r--src/cook.c1
-rw-r--r--src/foodopts.c39
2 files changed, 37 insertions, 3 deletions
diff --git a/src/cook.c b/src/cook.c
index 80c7f9f..ec9ac2d 100644
--- a/src/cook.c
+++ b/src/cook.c
@@ -56,7 +56,6 @@ main(int argc, char * argv[])
c = get_foodopt (argc, argv, "ehI:n:c:t:i:s:",
long_options, &option_index);
- printf("ASDASDASDA\n");
if (c == -1)
break;
diff --git a/src/foodopts.c b/src/foodopts.c
index 338b97e..fac807d 100644
--- a/src/foodopts.c
+++ b/src/foodopts.c
@@ -1,14 +1,49 @@
#include <stdio.h>
+#include <stdlib.h>
#include "foodopts.h"
+static struct option *
+to_getopt_longopts(const struct foodoption * src)
+{
+ int l = 0;
+ while( src[l].name != 0
+ || src[l].has_arg != 0
+ || src[l].flag != 0
+ || src[l].val != 0) {
+ l++;
+ }
+
+ struct option * dst = (struct option *)malloc(sizeof(struct option) * (l + 1));
+ int i = 0;
+ while( src[i].name != 0
+ || src[i].has_arg != 0
+ || src[i].flag != 0
+ || src[i].val != 0) {
+ dst[i].name = src[i].name;
+ dst[i].has_arg = src[i].has_arg;
+ dst[i].flag = src[i].flag;
+ dst[i].val = src[i].val;
+ i++;
+ }
+ dst[i].name = 0;
+ dst[i].has_arg = 0;
+ dst[i].flag = 0;
+ dst[i].val = 0;
+
+ return dst;
+}
+
int
get_foodopt(int argc, char *const argv[],
const char *optstring,
const struct foodoption *longopts,
int *longindex)
{
- return getopt_long(argc, argv, optstring,
- (struct option *)longopts, longindex);
+ struct option * o = to_getopt_longopts(longopts);
+ int rc = getopt_long(argc, argv, optstring, o, longindex);
+
+ free(o);
+ return rc;
}
const char *