summaryrefslogtreecommitdiff
path: root/src/internal/slibtool_txtline_impl.c
blob: 4c8f2addb5fb9dab51710c1c0ae77030fd4f9932 (plain)
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
/*******************************************************************/
/*  slibtool: a strong libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <string.h>
#include <stdlib.h>

#include <slibtool/slibtool.h>
#include "slibtool_visibility_impl.h"

slbt_hidden int slbt_txtline_to_string_vector(const char * txtline, char *** pargv)
{
	int             argc;
	char **         argv;
	const char *    ch;
	const char *    mark;

	if (!(ch = txtline))
		return 0;

	argc = 1;

	for (; *ch == ' '; )
		ch++;

	for (; *ch; ) {
		if (*ch++ == ' ') {
			argc++;

			for (; (*ch == ' '); )
				ch++;
		}
	}

	if (argc == 1)
		return 0;

	if (!(*pargv = calloc(++argc,sizeof(char *))))
		return -1;

	for (ch=txtline; (*ch == ' '); ch++)
		(void)0;

	argv = *pargv;
	mark = ch;

	for (; *ch; ) {
		if (*ch == ' ') {
			if (!(*argv++ = strndup(mark,ch-mark)))
				return -1;

			for (; (*ch == ' '); )
				ch++;

			mark = ch;
		} else {
			ch++;
		}
	}

	if (!(*argv++ = strndup(mark,ch-mark)))
		return -1;

	return 0;
}