summaryrefslogtreecommitdiffstats
path: root/lib/extra_opts.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-09-15 14:06:55 +0200
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-09-15 14:06:55 +0200
commitf855c5b5bbbc6d5436741fd8108be64825a3c76b (patch)
tree0ee1242bc21dc75a12fae03a5bb211fe10c89625 /lib/extra_opts.c
parent8ef825d85fb4d09c32ca44c545d6eb8d995ddea4 (diff)
downloadmonitoring-plugins-f855c5b5bbbc6d5436741fd8108be64825a3c76b.tar.gz
general refactorin in lib, more local variables, real booleans
Diffstat (limited to 'lib/extra_opts.c')
-rw-r--r--lib/extra_opts.c39
1 files changed, 23 insertions, 16 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 857b34b4..3fe69014 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -29,26 +29,30 @@
29bool is_option2(char *str) { 29bool is_option2(char *str) {
30 if (!str) { 30 if (!str) {
31 return false; 31 return false;
32 } else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { 32 }
33
34 if (strspn(str, "-") == 1 || strspn(str, "-") == 2) {
33 return true; 35 return true;
34 } else {
35 return false;
36 } 36 }
37
38 return false;
37} 39}
38 40
39/* this is the externally visible function used by plugins */ 41/* this is the externally visible function used by plugins */
40char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { 42char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
41 np_arg_list *extra_args = NULL, *ea1 = NULL, *ea_tmp = NULL;
42 char **argv_new = NULL;
43 char *argptr = NULL;
44 int i, j, optfound, argc_new, ea_num = *argc;
45
46 if (*argc < 2) { 43 if (*argc < 2) {
47 /* No arguments provided */ 44 /* No arguments provided */
48 return argv; 45 return argv;
49 } 46 }
50 47
51 for (i = 1; i < *argc; i++) { 48 np_arg_list *extra_args = NULL;
49 np_arg_list *ea1 = NULL;
50 np_arg_list *ea_tmp = NULL;
51 char *argptr = NULL;
52 int optfound;
53 size_t ea_num = (size_t)*argc;
54
55 for (int i = 1; i < *argc; i++) {
52 argptr = NULL; 56 argptr = NULL;
53 optfound = 0; 57 optfound = 0;
54 58
@@ -57,9 +61,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
57 /* It is a single argument with value */ 61 /* It is a single argument with value */
58 argptr = argv[i] + 13; 62 argptr = argv[i] + 13;
59 /* Delete the extra opts argument */ 63 /* Delete the extra opts argument */
60 for (j = i; j < *argc; j++) { 64 for (int j = i; j < *argc; j++) {
61 argv[j] = argv[j + 1]; 65 argv[j] = argv[j + 1];
62 } 66 }
67
63 i--; 68 i--;
64 *argc -= 1; 69 *argc -= 1;
65 } else if (strcmp(argv[i], "--extra-opts") == 0) { 70 } else if (strcmp(argv[i], "--extra-opts") == 0) {
@@ -67,9 +72,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
67 /* It is a argument with separate value */ 72 /* It is a argument with separate value */
68 argptr = argv[i + 1]; 73 argptr = argv[i + 1];
69 /* Delete the extra-opts argument/value */ 74 /* Delete the extra-opts argument/value */
70 for (j = i; j < *argc - 1; j++) { 75 for (int j = i; j < *argc - 1; j++) {
71 argv[j] = argv[j + 2]; 76 argv[j] = argv[j + 2];
72 } 77 }
78
73 i -= 2; 79 i -= 2;
74 *argc -= 2; 80 *argc -= 2;
75 ea_num--; 81 ea_num--;
@@ -77,9 +83,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
77 /* It has no value */ 83 /* It has no value */
78 optfound = 1; 84 optfound = 1;
79 /* Delete the extra opts argument */ 85 /* Delete the extra opts argument */
80 for (j = i; j < *argc; j++) { 86 for (int j = i; j < *argc; j++) {
81 argv[j] = argv[j + 1]; 87 argv[j] = argv[j + 1];
82 } 88 }
89
83 i--; 90 i--;
84 *argc -= 1; 91 *argc -= 1;
85 } 92 }
@@ -115,20 +122,20 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
115 } 122 }
116 } /* lather, rince, repeat */ 123 } /* lather, rince, repeat */
117 124
118 if (ea_num == *argc && extra_args == NULL) { 125 if (ea_num == (size_t)*argc && extra_args == NULL) {
119 /* No extra-opts */ 126 /* No extra-opts */
120 return argv; 127 return argv;
121 } 128 }
122 129
123 /* done processing arguments. now create a new argv array... */ 130 /* done processing arguments. now create a new argv array... */
124 argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); 131 char **argv_new = (char **)malloc((ea_num + 1) * sizeof(char **));
125 if (argv_new == NULL) { 132 if (argv_new == NULL) {
126 die(STATE_UNKNOWN, _("malloc() failed!\n")); 133 die(STATE_UNKNOWN, _("malloc() failed!\n"));
127 } 134 }
128 135
129 /* starting with program name */ 136 /* starting with program name */
130 argv_new[0] = argv[0]; 137 argv_new[0] = argv[0];
131 argc_new = 1; 138 int argc_new = 1;
132 /* then parsed ini opts (frying them up in the same run) */ 139 /* then parsed ini opts (frying them up in the same run) */
133 while (extra_args) { 140 while (extra_args) {
134 argv_new[argc_new++] = extra_args->arg; 141 argv_new[argc_new++] = extra_args->arg;
@@ -137,7 +144,7 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) {
137 free(ea1); 144 free(ea1);
138 } 145 }
139 /* finally the rest of the argv array */ 146 /* finally the rest of the argv array */
140 for (i = 1; i < *argc; i++) { 147 for (int i = 1; i < *argc; i++) {
141 argv_new[argc_new++] = argv[i]; 148 argv_new[argc_new++] = argv[i];
142 } 149 }
143 *argc = argc_new; 150 *argc = argc_new;