-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargs.c
51 lines (45 loc) · 1 KB
/
args.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
#include <string.h>
#include "args.h"
#include "utils.h"
void InitArgs(ArgList *args)
{
args->args = malloc(sizeof(char *) * INIT_SIZE);
args->size = 0;
args->max = INIT_SIZE;
if (args->args == NULL) {
throw_fatal_error();
}
args->args[0] = NULL;
}
void AddArg(ArgList *args, char *arg)
{
if (args->size == args->max) {
args->max *= 2;
args->args = realloc(args->args, sizeof(char *) * args->max);
if (args->args == NULL) {
throw_fatal_error();
}
}
if (arg == NULL)
{
args->args[args->size] = NULL;
args->size++;
return;
}
char* new_arg = malloc(sizeof(char) * (strlen(arg) + 1));
if (new_arg == NULL) {
throw_fatal_error();
}
strcpy(new_arg, arg);
args->args[args->size] = new_arg;
args->size++;
}
void FreeArgs(ArgList *args)
{
for (int i = 0; i < args->size; i++) {
free(args->args[i]);
}
free(args->args);
free(args);
}