-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.h
88 lines (77 loc) · 1.51 KB
/
args.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Argv-List
*
* This Works is placed under the terms of the Copyright Less License,
* see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY.
*/
#include "memswap.h"
struct args
{
int n, max;
char **args;
};
/* This adds a string to the given args,
* without allocating!
* So it can be manipulated later on.
*/
static struct args *
args_add(struct args *a, char *s)
{
if (a->max <= a->n+1)
{
a->max += a->max + a->n + 2;
a->args = re_alloc(a->args, a->max * sizeof *a->args);
}
a->args[a->n] = s;
a->args[++a->n] = 0;
return a;
}
/* This adds a printf formatted string to args,
* with allocating!
*/
static struct args *
args_addf(struct args *a, const char *s, ...)
{
char *buf;
int n, k;
buf = 0;
for (n=BUFSIZ;; n+=k)
{
va_list list;
buf = re_alloc(buf, n);
va_start(list, s);
k = vsnprintf(buf, n, s, list);
va_end(list);
if (k<n)
break;
}
k = strlen(buf);
if (k+1<n)
buf = re_alloc(buf, k+1);
return args_add(a, buf);
}
static struct args *
args_prepend(struct args *a, ...)
{
va_list list;
char *s;
int n;
n = a->n;
va_start(list, a);
while ((s = va_arg(list, char *))!=0)
args_add(a, s);
va_end(list);
memswap(a->args, n*sizeof *a->args, a->n*sizeof *a->args);
return a;
}
static struct args *
args_pop(struct args *a, int n)
{
if (n >= a->n)
{
a->n = 0;
return a;
}
a->n -= n;
memmove(a->args, &a->args[n], a->n*sizeof *a->args);
return a;
}