diff options
| author | johannes <johannes.herman@gmail.com> | 2024-03-03 16:55:50 +0100 |
|---|---|---|
| committer | johannes <johannes.herman@gmail.com> | 2024-03-03 16:55:50 +0100 |
| commit | 150790c84f1773cfc82b13953d7894cd25474a83 (patch) | |
| tree | e48ea734979e4e0111a4c26172887e4afeb232b2 | |
| parent | 9a14ce4b99dea312fe508153d6d8ccc56c27c69a (diff) | |
added freeargs
| -rw-r--r-- | inc/args.h | 1 | ||||
| -rw-r--r-- | src/args.c | 29 | ||||
| -rw-r--r-- | src/main.c | 7 |
3 files changed, 34 insertions, 3 deletions
@@ -30,6 +30,7 @@ int addarg(char *value, char *desc, void (*runarg)(struct arg *)); int addopt(char *flag, char *full, int numopt, char *value, char *desc, void (*runarg)(struct arg *)); int parseargs(int argc, char *args[]); +void freeargs(); void runargs(); arg *getarg(int pos); @@ -157,14 +157,35 @@ int parseargs(int argc, char *args[]) } else { - argerr = 2; - errflag = NULL; + if (a[0] != '-') + { + argerr = 2; + errflag = NULL; + } + else + { + argerr = 3; + errflag = args[i]; + } return 0; } } return 1; } +void freeargs() +{ + for (int i = 0; i < ARGC; i++) + { + if (ARGS[i].opts) + free(ARGS[i].opts); + } + if (ARGS) + free(ARGS); + if (PARGS) + free(PARGS); +} + void runargs() { for (int i = 0; i < ARGC; i++) @@ -197,6 +218,7 @@ arg *getopt(char *flag, int pos) } return NULL; } + void printhelp() { char inctype[ARGTYPELEN] = {0, 0, 0}; @@ -264,6 +286,9 @@ void paerror(char *str) case 2: errstr = "Too many positional arguments"; break; + case 3: + errstr = "Unknown flag"; + break; default: errstr = "Unknown error"; break; @@ -50,7 +50,10 @@ void setdemo(arg *argument) { demofile = fopen(argument->opts[0], "r"); if (!demofile) - exitperror("Error: failed to open demofile, reason"); + { + fprintf(stderr, "Error: failed to open demofile '%s', ", argument->opts[0]); + exitperror("reason"); + } if (readdemo(demofile, &DEMO) < 0) { fprintf(stderr, "Error: failed to parse demo\n"); @@ -207,5 +210,7 @@ int main(int argc, char *argv[]) if (outarg) runoutput(outarg); + freeargs(); + exit(EXIT_SUCCESS); } |
