2
convenient routines for argument list handling
4
Copyright (C) Andrew Tridgell 2002
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
ARGS *args_init(int init_argc, char **init_args)
27
args = (ARGS *)x_malloc(sizeof(ARGS));
29
args->argv = (char **)x_malloc(sizeof(char *));
31
for (i=0;i<init_argc;i++) {
32
args_add(args, init_args[i]);
38
void args_add(ARGS *args, const char *s)
40
args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
41
args->argv[args->argc] = x_strdup(s);
43
args->argv[args->argc] = NULL;
46
/* pop the last element off the args list */
47
void args_pop(ARGS *args, int n)
51
free(args->argv[args->argc]);
52
args->argv[args->argc] = NULL;
56
/* remove the first element of the argument list */
57
void args_remove_first(ARGS *args)
60
memmove(&args->argv[0],
62
args->argc * sizeof(args->argv[0]));
66
/* add an argument into the front of the argument list */
67
void args_add_prefix(ARGS *args, const char *s)
69
args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
70
memmove(&args->argv[1], &args->argv[0],
71
(args->argc+1) * sizeof(args->argv[0]));
72
args->argv[0] = x_strdup(s);
76
/* strip any arguments beginning with the specified prefix */
77
void args_strip(ARGS *args, const char *prefix)
80
for (i=0; i<args->argc; ) {
81
if (strncmp(args->argv[i], prefix, strlen(prefix)) == 0) {
83
memmove(&args->argv[i],
85
args->argc * sizeof(args->argv[i]));