PatchworkOS
Loading...
Searching...
No Matches
argsplit.c
Go to the documentation of this file.
1#include "common/argsplit.h"
2
3const char** argsplit(const char* str, uint64_t maxLen, uint64_t* count)
4{
5 uint64_t skipped = 0;
6 while (isspace(*str) && (maxLen == 0 || skipped < maxLen))
7 {
8 str++;
9 skipped++;
10 }
11 maxLen = (maxLen == 0) ? 0 : (maxLen > skipped ? maxLen - skipped : 0);
12
13 uint64_t argc;
14 uint64_t totalChars;
15 if (_argsplit_count_chars_and_args(str, &argc, &totalChars, maxLen) == UINT64_MAX)
16 {
17 return NULL;
18 }
19
20 uint64_t argvSize = sizeof(char*) * (argc + 1);
21 uint64_t stringsSize = totalChars + argc;
22
23 const char** argv = malloc(argvSize + stringsSize);
24 if (argv == NULL)
25 {
26 return NULL;
27 }
28 if (count != NULL)
29 {
30 *count = argc;
31 }
32 if (argc == 0)
33 {
34 argv[0] = NULL;
35 return argv;
36 }
37
38 return _argsplit_backend(argv, str, argc, maxLen);
39}
uint64_t _argsplit_count_chars_and_args(const char *str, uint64_t *argc, uint64_t *totalChars, uint64_t maxLen)
Definition argsplit.c:72
const char ** _argsplit_backend(const char **argv, const char *str, uint64_t argc, uint64_t maxLen)
Definition argsplit.c:100
_PUBLIC int isspace(int c)
Definition isspace.c:5
const char ** argsplit(const char *str, uint64_t maxLen, uint64_t *count)
Standardized argument parsing function.
Definition argsplit.c:3
#define NULL
Pointer error value.
Definition NULL.h:23
static atomic_long count
Definition main.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define UINT64_MAX
Definition stdint.h:74
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5