ccache/args.c

104 lines
2.5 KiB
C
Raw Normal View History

2002-03-26 23:58:31 +00:00
/*
2010-04-22 20:47:05 +00:00
* Copyright (C) 2002 Andrew Tridgell
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2002-03-26 23:58:31 +00:00
2002-03-26 14:46:43 +00:00
#include "ccache.h"
2010-02-17 19:53:05 +00:00
#include <stdlib.h>
#include <string.h>
2003-02-16 02:28:35 +00:00
ARGS *args_init(int init_argc, char **init_args)
2002-03-26 14:46:43 +00:00
{
ARGS *args;
2003-02-16 02:28:35 +00:00
int i;
args = (ARGS *)x_malloc(sizeof(ARGS));
2002-03-26 14:46:43 +00:00
args->argc = 0;
args->argv = (char **)x_malloc(sizeof(char *));
2002-03-26 14:46:43 +00:00
args->argv[0] = NULL;
2003-02-16 02:28:35 +00:00
for (i=0;i<init_argc;i++) {
args_add(args, init_args[i]);
}
2002-03-26 14:46:43 +00:00
return args;
}
void args_free(ARGS *args)
{
int i;
for (i = 0; i < args->argc; ++i) {
if (args->argv[i]) {
free(args->argv[i]);
}
}
free(args->argv);
free(args);
}
2002-03-26 14:46:43 +00:00
void args_add(ARGS *args, const char *s)
{
args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
args->argv[args->argc] = x_strdup(s);
2002-03-26 14:46:43 +00:00
args->argc++;
args->argv[args->argc] = NULL;
}
2003-02-16 02:28:35 +00:00
/* pop the last element off the args list */
void args_pop(ARGS *args, int n)
{
2002-04-01 03:58:57 +00:00
while (n--) {
args->argc--;
free(args->argv[args->argc]);
args->argv[args->argc] = NULL;
}
}
2003-02-16 02:28:35 +00:00
/* remove the first element of the argument list */
void args_remove_first(ARGS *args)
{
free(args->argv[0]);
2009-11-07 08:09:39 +00:00
memmove(&args->argv[0],
2003-02-16 02:28:35 +00:00
&args->argv[1],
args->argc * sizeof(args->argv[0]));
args->argc--;
}
/* add an argument into the front of the argument list */
void args_add_prefix(ARGS *args, const char *s)
{
args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
2009-11-07 08:09:39 +00:00
memmove(&args->argv[1], &args->argv[0],
2003-02-16 02:28:35 +00:00
(args->argc+1) * sizeof(args->argv[0]));
args->argv[0] = x_strdup(s);
2003-02-16 02:28:35 +00:00
args->argc++;
}
/* strip any arguments beginning with the specified prefix */
void args_strip(ARGS *args, const char *prefix)
{
int i;
for (i=0; i<args->argc; ) {
if (strncmp(args->argv[i], prefix, strlen(prefix)) == 0) {
2003-02-16 02:28:35 +00:00
free(args->argv[i]);
2009-11-07 08:09:39 +00:00
memmove(&args->argv[i],
&args->argv[i+1],
2003-02-16 02:28:35 +00:00
args->argc * sizeof(args->argv[i]));
args->argc--;
} else {
i++;
}
}
}