]> diplodocus.org Git - nmh/blob - sbr/getarguments.c
mhlsbr.c: Don't strchr(3) non-string NUL-less buffer.
[nmh] / sbr / getarguments.c
1 /* getarguments.c -- Get the argument vector ready to go.
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10
11 char **
12 getarguments (char *invo_name, int argc, char **argv, int check_context)
13 {
14 char *cp = NULL, **ap = NULL, **bp = NULL, **arguments = NULL;
15 int n = 0;
16
17 /*
18 * Check if profile/context specifies any arguments
19 */
20 if (check_context && (cp = context_find (invo_name))) {
21 cp = mh_xstrdup(cp); /* make copy */
22 ap = brkstring (cp, " ", "\n"); /* split string */
23
24 /* Count number of arguments split */
25 bp = ap;
26 while (*bp++)
27 n++;
28 }
29
30 arguments = (char **) mh_xmalloc ((argc + n) * sizeof(*arguments));
31 bp = arguments;
32
33 /* Copy any arguments from profile/context */
34 if (ap != NULL && n > 0) {
35 while (*ap)
36 *bp++ = *ap++;
37 }
38
39 /* Copy arguments from command line */
40 argv++;
41 while (*argv)
42 *bp++ = *argv++;
43
44 /* Now NULL terminate the array */
45 *bp = NULL;
46
47 return arguments;
48 }